Often using Docker to simulate the deployment of a project in a production environment, it is often necessary to open several Docker containers at the same time, and sometimes the installed software needs to be bound to other containers in the Docker LAN, such as MongoDB replica set deployment, it needs to bind the private IP of other containers.
However, after each Docker restart, the IP address of the container will change, and Docker supports setting a fixed IP after querying the data.
Docker default network
Once Docker is installed, the following three network types are created by default:
NETWORK ID NAME DRIVER SCOPE 17cbf438c338 bridge bridge local 1ac50740d496 host host local e863b9972d3b none null local bridge: Bridge network
By default, the Docker containers started use bridges, the bridge network created when Docker is installed, and every time the Docker container is restarted, the corresponding IP address will be obtained in order, which will cause the Docker IP address to change under the restart
none: No network is specified
With --network=none, the docker container does not assign IP for the LAN
host: host network
With --network=host, the Docker container's network is attached to the host, and the two are interoperable. For example, if you run a web service in a container and listen to port 8080, the host's port 8080 will be automatically mapped to the container.
Create a custom network: (Set a fixed IP)
Step 1: Create a custom network
Create a custom network and specify the CIDR block: 172.18.0.0/16
Step 2: Create a Docker container
At this time, the created Docker container will hold 172.18.0.2 this IP.
|