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

The method of how to fix IP setting by Docker

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Due to the needs of work, need to set up a fixed IP for Docker, look up some information on the Internet, try to practice it, and keep a note.

Often use Docker to simulate the deployment of a project in a production environment, often need to open several Docker containers at the same time, and sometimes install software that needs to be bound to other containers in the Docker LAN, such as MongoDB replica set deployment, you need to bind the private network IP of other containers.

However, after each restart of Docker, the IP address of the container changes, and the data is queried. Docker supports setting a fixed IP.

Docker default network

After Docker is installed, the following three network types are created by default:

$docker network lsNETWORK ID NAME DRIVER SCOPE9781b1f585ae bridge bridge local1252da701e55 host host local237ea3d5cfbf none null local

When starting Docker, use the-- network parameter to specify the network type, such as:

~ docker run-itd-- name test1-- network bridge-- ip 172.17.0.10 centos:latest / bin/bash

Bridge: bridging network

The Docker containers started by default are all bridged networks created during bridge,Docker installation. Each time the Docker container is restarted, the corresponding IP address will be obtained sequentially. This will cause the IP address of Docker to change under restart.

None: no specified network

Using-- network=none, the docker container will not allocate the IP of the LAN

Host: host network

Using-- network=host, at this point, the network of the Docker container is attached to the host, and the two are interconnected.

For example, if you run a Web service in a container and listen on port 8080, the host's port 8080 is automatically mapped to the container.

Create a custom network: (set fixed IP)

When starting the Docker container, using the default network does not support assigning a fixed IP, as follows:

~ docker run-itd-net bridge-- ip 172.17.0.10 centos:latest / bin/bash6eb1f228cf308d1c60db30093c126acbfd0cb21d76cb448c678bab0f1a7c0df6docker: Error response from daemon: User specified IP address is supported on user defined networks only.

Therefore, you need to create a custom network, and here are the specific steps:

Step 1: create a custom network

Create a custom network and specify the network segment: 172.18.0.0Universe 16

➜~ docker network create-- subnet=172.18.0.0/16 mynetwork➜ ~ docker network lsNETWORK ID NAME DRIVER SCOPE9781b1f585ae bridge bridge local1252da701e55 host host local4f11ae9c85de mynetwork bridge local237ea3d5cfbf none null local

Step 2: create a Docker container

The copy code is as follows:

➜~ docker run-itd-- name networkTest1-- net mynetwork-- ip 172.18.0.2 centos:latest / bin/bash

At this point, the created Docker container will hold the IP of 172.18.0.2.

[root@ec8e31938fe7 /] # ifconfigeth0 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: 1PG 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.0b) TX bytes:0 (0.0b)

Link

Docker Container Networking

Specify the fixed IP/ static IP address of the custom network segment for the Docker container

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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

Servers

Wechat

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

12
Report