Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker data and Network Management

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

I. data management

1. Mount the local directory to the container

[root@C7-lab1] # docker run-tid-v / data/:/data centos bash

0f1093aadf6ec85224520a303f92282d12443ecf0305bbf15eb2f3526fd55212

[root@C7-lab1 ~] # docker exec-it 0f10 bash

[root@0f1093aadf6e /] # df-h

Filesystem Size Used Avail Use% Mounted on

/ dev/mapper/docker-253:0-34406889-

Tmpfs 490M 0 490M 0% / sys/fs/cgroup

/ dev/mapper/centos-root 18G 4.8G 13G 28% / data

-v is used to specify the mount directory: the / data/ in front is the local directory, and the / data/ in the back is the directory in the container.

two。 Mount a data volume

In fact, when we mount a directory, we can specify that the container name will be defined randomly if it is not specified. For example, if we didn't specify it above, we generated a name called loving_kowalevski. You can use the command docker ps to see the rightmost column.

Docker run-itd-- volumes-from loving_kowalevski aming/centos bash

So we use the szk/centos image to create a new container and use the data volume of the loving_kowalevski container

Custom Container name

Docker run-itd-v / data/:/mnt-- name datavol aming/centos bash

3. Define a data volume container

Sometimes we need multiple containers to share data with each other, similar to the NFS in linux, so we can build a special data volume container and mount the data volume directly to other containers.

First set up a data volume container

Docker run-itd-v / data/-- name insane_hopper centos bash

# Note that the / data/ here is the container's / data directory, not the local / data/ directory. If you want to use a container that is already running, check the last line name of the container through docker ps and use-- volumes-from to specify and then let other containers mount the data volume.

[root@C7-lab1] # docker run-itd-- volumes-from insane_hopper centos bash

9c296da6550be50bbe90cce23bbebf1590245c114d572dea1ec4f9441f70dd07

[root@C7-lab1 ~] # docker run-itd-- volumes-from insane_hopper-- name web2 centos bash # can continue to create

88e24c0d19efb8c152271b0c07e3f262fbb6d03252b72b2fbc3433c30930f6eb

[root@C7-lab1 ~] # docker exec-it 88e2 bash

[root@88e24c0d19ef /] # ls / data/

Szk.txt

4. Backup and recovery of data Volum

1 backup

[root@C7-lab1 ~] # mkdir / vol_data_backup

[root@C7-lab1] # docker run-itd-- volumes-from insane_hopper-v / vol_data_backup/:/backup centos bash

Ad6c5945f7509cf952f29be51733eddbdd588295c820f01e000b3346ce8b2779

[root@C7-lab1 ~] # docker exec-it ad6c bash

[root@ad6c5945f750 /] # ls / backup/

[root@ad6c5945f750 /] # tar cvf / backup/data.tar / data

[root@C7-lab1 ~] # ls / vol_data_backup/

Data.tar

First of all, we need to use the insane_hopper data volume to open a new container, and we also need to mount the local / vol_data_backup/ directory to the container's / backup so that the new files in the container / backup directory can be seen directly in the / vol_data_backup/ directory. Then package the files under the / data/ directory into a data.tar file and put them in the / backup directory.

2 recovery

The idea is to first create a new data volume container, then build a new container and mount the data volume container, and then unpack the tar package.

Create a new data volume container docker run-itd-v / data/-- name data centos bash

Mount a new container of data volume and unpack docker run-- volumes-from testvol2-v / vol_data_backup/:/backup centos tar xvf / backup/data.tar

II. Network management

Four network modes

Host mode uses docker run when using-- net=host specifies that the network used by the docker is actually the same as the host. The network card ip seen in the container is the ip on the host.

Container mode uses-- net=container:container_id/container_name multiple containers see the same ip using a common network

None mode uses-- net=none to specify that no network will be configured in this mode

The bridge mode uses-- net=bridge to specify the default mode without specifying that the default is this network mode. This mode assigns a separate Network Namespace to each container. Similar to vmware's nat network model. All containers on the same host can communicate with each other under the same network segment.

External network access container

First create a new container using the centos image, then install the httpd service in the container and start it

[root@C7-lab1 vol_data_backup] # docker commit-m "centos_with_httpd"-a "szk" 8d5192bdf660 centos_with_httpd

216d5f8f0c57f49c311e7b7a07c2767b14a1bd78cf3a11c7f923bea9df6a7b57

[root@C7-lab1 vol_data_backup] # docker p_w_picpaths

REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE

Centos_with_httpd latest 216d5f8f0c57 11 seconds ago 337.7 MB

Then export the container to a new mirror centos-httpd) and then use the new image to create the container and specify the port mapping

Docker run-itd-p 5123 centos-httpd bash 80 / /-p can be specified for port mapping. In this case, port 80 of the container is mapped to local port 5123.

[root@C7-lab1 vol_data_backup] # docker run-itd-p 8080 itd 80 centos_with_httpd:latest

1cf3b634f537d6f2bfc8b17461e92dfa0f7370b91607f125f58cdbf2e545314b

[root@C7-lab1 vol_data_backup] # docker exec-it 1cf3 bash

[root@1cf3b634f537 /] # / usr/sbin/httpd

[root@1cf3b634f537 /] # vi / var/www/html/1.html

[root@C7-lab1 ~] # netstat-ntlup | grep 8080

Tcp6 0 0: 8080: * LISTEN 4069/docker-proxy

[root@C7-lab1 ~] # curl localhost:8080/1.html

Szk

Docker exec-it container_id bash

Start httpd httpd-k start

Edit 1.html vi / var/www/html/1.html and write something casually.

Exit the container exit

Test curl 127.0.0.1:5123/1.html

-p also supports the format of IP:port:ip:port, such as

-p 127.0.0.1 purl 8080 purl 80

You can also write ip instead of the local port, so you can assign a port at will.

-p 127.0.0.1Fran 80

Container interconnection

Download a mysql image

Docker pull mysql

Create a new container named db

Docker run-it-d-p 13306 name db mysql bash 3306

Create a new web container and connect with db

Docker run-it-d-p 12308 name web-- link db:db centos-httpd bash

Configure bridged network centos7)

In order to facilitate the communication between the machines in the local network and the Docker container, we often need to configure the Docker container to the same network segment as the host. In fact, this requirement can be easily realized. All we have to do is bridge the Docker container with the host Nic and add IP to the Docker container.

Install pipworkgit clone https://github.com/jpetazzo/pipework

Cp ~ / pipework/pipework / usr/local/bin/

Open a container docker run-itd-- net=none-- name aming123 centos / bin/bash

Pipework br0 aming123 172.7.15.201After the ip@ that is the container, the ip is the host ip

Brctl addif br0 eth0 # eth0 is the host network card. This step is to bridge br0 and eth0.

Ip addr add 172.7.15.107 ip addr add 24 br0 # binds the ip of 107 to br0

Docker exec-it aming123 / bin/bash # after entering the ifconfig, you can see the newly added ip.

Centos6:

Cd / etc/sysconfig/network-scripts/; cp ifcfg-eth0 ifcfg-br0

Vi ifcfg-eth0 / / add BRIDGE=br0 and delete IPADDR,NETMASK,GATEWAY,DNS1

Vi ifcfg-br0// changed DEVICE to br0,Type to Bridge, and set eth0's network settings here.

Service network restart

Install pipwork: git clone https://github.com/jpetazzo/pipework

Cp ~ / pipework/pipework / usr/local/bin/

Open a container: docker run-itd-- net=none-- name aming123 centos / bin/bash

Rpm-Uvh rpm-Uvh https://repos.fedorapeople.org/openstack/EOL/openstack-grizzly/epel-6/iproute-2.6.32-130.el6ost.netns.2.x86_64.rpm # is uneasy to report the wrong Object "netns" is unknown, try "ip help"

Pipework br0 aming123 172.7.15.201/24

Docker exec-it aming123 / bin/bash # after entering the ifconfig, you can see the newly added ip.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report