Docker is often used to simulate the deployment of a project in a production environment. It often requires several Docker containers to be opened at the same time. Sometimes the installed software needs to bind to other containers in the Docker LAN, such as the intranet IP of other containers when deploying the MongoDB replica set.
However, every time Docker restarts, the IP address of the container will change. After querying the data, Docker supports setting fixed IP.
Docker default network
After Docker is installed, the following three network types are created by default:
$ docker network ls NETWORK ID NAME DRIVER SCOPE 9781b1f585ae bridge bridge local 1252da701e55 host host local 237ea3d5cfbf none null local
When starting Docker, you can specify the network type with the -- network parameter, such as:
➜ ~ docker run -itd --name test1 --network bridge --ip 172.17.0.10 centos:latest /bin/bash
bridge: bridging network
Docker containers started by default are all bridged networks created by bridge and Docker installation. Each time the Docker container restarts, the corresponding IP address will be acquired in sequence, which will lead to the restart of Docker, and the IP address of Docker will change.
none: No designated network
With -- network=none, the docker container does not assign IP to the LAN.
host: host network
With -- network=host, the network of the Docker container is attached to the host, and the two are interoperable.
For example, if you run a Web service in a container and listen on port 8080, the host's port 8080 will automatically map to the container.
Create a custom network: (Setting Fixed IP)
When starting the Docker container, using the default network does not support the assignment of fixed IP, as follows:
➜ ~ docker run -itd --net bridge --ip 172.17.0.10 centos:latest /bin/bash 6eb1f228cf308d1c60db30093c126acbfd0cb21d76cb448c678bab0f1a7c0df6 docker: Error response from daemon: User specified IP address is supported on user defined networks only.
Therefore, you need to create a custom network. Here are the specific steps:
Step 1: Create a custom network
Create a custom network and specify a segment: 172.18.0.0/16
➜ ~ docker network create --subnet=172.18.0.0/16 mynetwork ➜ ~ docker network ls NETWORK ID NAME DRIVER SCOPE 9781b1f585ae bridge bridge local 1252da701e55 host host local 4f11ae9c85de mynetwork bridge local 237ea3d5cfbf none null local
Step 2: Create a Docker container
➜ ~ docker run -itd --name networkTest1 --net mynetwork --ip 172.18.0.2 centos:latest /bin/bash
At this point, the Docker container created holds the IP of 172.18.0.2.
[root@ec8e31938fe7 /]# ifconfig eth0 Link encap:Ethernet HWaddr 02:42:AC:12:00:02 inet addr:172.18.0.2 Bcast:0.0.0.0 Mask:255.255.0.0 inet6 addr: fe80::42:acff:fe12:2/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:88 errors:0 dropped:0 overruns:0 frame:0 TX packets:14 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:4056 (3.9 KiB) TX bytes:1068 (1.0 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)