docker network mode

catalogue

1, docker network

1. Network pattern classification

2. Command

How to transfer files from the host to the container

There is no systemctl command solution entering the container

2, docker data volume

Mount data volume

Data volume container

3, Container interconnection (using centos image)

summary

docker mainly has four network modes,

Common commands

1, docker network

1. Network pattern classification

Host: share the network namespace / network protocol stack with the host (use different ports by sharing the ip of the host)

Container: multiple containers share a network namespace

None: self closing space

Bridge: the default mode is to connect the container and dockero bridge through veth. The bridge is assigned to the container IP. Meanwhile, docker0 acts as the gateway of the "Lan" content container, and finally communicates with the host network card

2. Command

View a list of network modes
docker network ls

View container information
docker inspect container ID

Custom network fixed ip(You need to define it first network Type, second name and ip (cannot be repeated)
docker network create --subnet=172.18.0.0/16 ly
docker run -itd --name test2 --net ly --ip 172.18.0.10 nginx:latest /bin/bash

#Exposed port
-p Custom port(Host port:Port in container)
-P Random port(-P 49153 Start (49153 to 65535)

Exposing port 8081 is equivalent to mapping port 80 of the container to port 8081 on the host
docker run -itd -p 8081:80 nginx /bin/bash ( docker 0)
docker run -itd -P nginx /bin/bash

Execute the command in the container in the host environment, enter the container and start nginx  #-c: Execute in container commands in the host environment
docker exec -it container ID /bin/ bash -c " nginx'
docker exec container ID/container name Commands executed

 

How to transfer files from the host to the container

docker cp Source file path container ID:Destination file path
docker cp /opt/1.txt container ID:/home

There is no systemctl command solution entering the container

docker run -itd --name centos7 --privileged=true centos:7 /sbin/init
# /sbin/init is the first process actively called when the kernel starts
#You can use docker inspect container ID

After right raising(It can be used after entering systemctl Yes)
docker exec -it container id /bin/bash

yum install net-tools # After installation, you can use ifconfig
yum install vim -y #After installation, you can use the vim editor

2, docker data volume

A data volume is a special directory that provides container usage

We have used nfs before. In fact, the effect is almost the same. When we need to modify the internal files of docker many times, we use exec to enter the changes many times, which is equivalent to frequent ssh password free login. There is a great risk factor

Mount data volume

Format: docker run -v Host Directory: the directory in the mount container

#example
docker run -it -v /var/www/html:/data1 --name test01 centos:7 /bin/bash

#At this time, open another terminal to create an abc in the host computer
cd /var/www/html
touch abc

#If you go back to the previous terminal and check the / data1 directory, an abc will be added

Data volume container

Principle: let two containers share data

1.Create a data volume container with the name web100,Two volumes:/data1 and /data2
docker run -it --name web100 -v /data1 -v /data2 centos:7 /bin/bash

2.Start another terminal and mount the data volume container to centos:7 On the container
docker run -it --name db1 --volumes-from web100 centos:7 /bin/bash
#At this point, you will find that there are / data1 and / data2 directories

3, Container interconnection (using centos image)

docker run -itd -P --name web1 centos:7 /bin/bash		//Create and run container named web1, port number random mapping
docker exec -it container ID /bin/bash

#Start another terminal (link web1: web1 can also communicate)
docker run -itd -P --name web2 --link web1:web2 centos:7 /bin/bash	//Create and run a container named web2, link to web1 and communicate with it
#--link: open the tunnel
docker exec -it container ID /bin/bash

#Both containers are downloaded
yum -y install net-tools	//Tools for downloading ifconfig
 verification:
enter web2 container ping web1

summary

docker mainly has four network modes,

The first is host, which shares the ip of the host, but different ports;

The second is container. Multiple containers share the ip of a specified container;

Third, none is nothing, nothing;

The fourth is the bridge. When creating a new container through veth, there will be a docker0 with a port on it, which will be converted from the port on the container;

Common commands

View network mode: docker network ls

Viewing container information: docker inspect container id

Custom network fixed ip: docker network create --subnet=172.18.0.0/16 ly; Define the network segment type first;

docker exec -itd --name test --net ly --ip 172.18.0.10 nginx:latest /bin/bash, specify the ip and enter the container

Exposed port: docker run -itd -p 8081:80 nginx /bin/bash

Start Nginx: docker exec -it container ID /bin/ bash -c nginx on the host

Transfer the host file to the container: docker cp source file path container ID: destination file path

Some functions installed in the container: docker run -itd --name centos7 --privileged=true centos:7 /sbin/init, first mention the right; Install some features using Yum install net tools

Mount data volume: docker run -v Host Directory: container directory   -- name container   / bin/bash

Two containers share: docker run -it --name web100 -v /data1 -v /data2 centos:7 /bin/bash(

docker run -it --name db1 --volumes-from web100 centos:7 /bin/bash)

Container interconnection: docker run -itd -P --name web1 centos:7 /bin/bash

Tags: Database Docker Redis Cache

Posted on Mon, 29 Nov 2021 03:44:27 -0500 by Mew151