This article has participated in the third “High Yield more text” track of the Denver Creators Training Camp, check out the details:Digg project | Creator Boot Camp phase 3 is underway, “write” to make a personal impact.
1, Docker environment construction, Windows, Linux, MAC these mainstream systems support docker environment, here refers to the Linux environment construction docker environment; Just a few lines of command!
Uname -r # Y y y y y y y y y y y y y y y y y y y y y y y y y y y The other two are devicemapper drivers rely on yum - config - manager - add - 'https://download.docker.com/linux/centos/docker-ce.repo # set yum source Yum list docker - ce - showduplicates | sort - r # check all warehouse docker version yum install docker - ce - 17.12.0. Ce # choose version installed docker - ce 17.12.0. Ce is the version of x86_64 earlier than the system versionCopy the code
Docker systemctl start docker systemctl enable docker systemctl enable docker With the client and server in two parts systemctl status docker # to check the docker startup state yum list installed | grep docker # check docker service installed a package yum remove X86_64 ddocker-ce-cli.x86_64 -y # delete installation package rm -rf /var/lib/docker # delete default image directoryCopy the code
Docker common command:
Docker ps - a view all container # - a parameter information, lists all the container id - q, s check list container size docker exec - it container id/bin/sh # docker into the container (stop/start/restart/rm) docker push docker push docker push docker push docker push Docker logs container ID_name # Check container logs --tail=num Docker login # --username= XXX --password= XXX You can view the help files with each command --help, for example: Docker images --helpCopy the code
3. From the construction of docker environment to the use of common basic commands (which can be obtained from –help), the test environment of daily Docker deployment service works normally, but exceptions are not excluded, which is easy to say;
For example, docker run starts the application of the container parameters -v, -e, –add-host, -p, etc.
4, standalone Docker environment is easier to play < at present docker own default network, a single host of different Docker containers can use docker0 bridge direct communication, this is not wrong! “> < p style =” max-width: 100%; clear: both; The first question is how to solve the communication problem between docker containers of different hosts. Need to understand some network knowledge and docker bridge concept!
Analysis of scheme principle: Since it is necessary to use the IP of containers for routing, it is necessary to avoid containers on different hosts using the same IP, so we should allocate different Docker subnets for different hosts to ensure that; Describe how data communication between different host containers is done! Container A of host machine A first sends docker0, the gateway of container A, and then sends it to host machine B when looking for the route of HOST machine A's IP. Host machine B receives the data and then forwards it to docker0, the gateway of container B. Finally, the data is sent to container B. Json to add "bip":" IP /netmask", for example: {"bip":"172.17.1.1/24"} because the IP address of different host docker0 bridge is the same, in order to solve the communication between different host containers need to set different DOCker0 bridge IP address. Json: add {"bip":"172.17.2.1/24"} and restart docker0. 172.17.2.1a Host: IP route add -net 172.17.2.0 netmask 255.255.255.0 GW 192.168.145.129 # 172.17.2.0 192.168.145.129 is the IP address of host B as the routing rule host B: IP route add -net 172.17.1.0 netmask 255.255.255.0 GW 192.168.145.128 # 172.17.1.0 192.168.145.128 Rule for setting host IP address as the route iptables -t NAT -f POSTROUTING iptables -t NAT -a POSTROUTING iptables -p FORWARD ACCEPT Restart the container, and then each host can ping each other. Containers of different hosts can ping each other to ensure normal communication. Iptables -t nat-vnl can view the port forwarding rules of each host container. Tips: What if there are multiple hosts that need to communicate with each host container? Then you can only bridge between the servers that need to communicateCopy the code
5. The problem above is the communication between different host containers. One of docker’s network modes is called Bridge < default mode >, so there are still three network modes: host mode, Container mode and None mode.
6. Question: The fourth solution to how docker containers communicate between different hosts is only temporary? Init 6 After restarting the Linux server, the route disappears, the bridge between docker containers of different hosts disappears, so communication cannot be done. How to solve the problem?
There are a lot of network schemes, I will not try one, choose one of the simple way, is to add the routing rules into a file, each startup system will default to load from the network card: D /network Create the static-routes file in the /etc/sysconfig directory and add the following content: Any net 172.17.1.0 netmask 255.255.255.0 GW 192.168.2.211 Analysis The working principle of /etc/init.d/network is as follows:
# Add noninterface-specific static-routes.if [ -f /etc/sysconfig/static-routes ]; then grep "^any" /etc/sysconfig/static-routes | while read ignore args ; Do /sbin/route add -$args Grep "^ any"/etc/sysconfig/static routes - # matches found this line, any beginning | while read ignore the args and ignore the content behind the first parameter to read; Then add routing rules in a loop.Copy the code
7, this time do not worry about restarting the route failure, because it is a physical machine must consider the shutdown state. Docker doesn’t know much about this, so what are some questions?
A > How to make Dockerfile, the first line From meaning?
B > Run docker image, is the environment in the container related to the current container running system?
C > Docker bridge and communication principle?