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

How to configure Docker Container fixed IP

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article is a detailed introduction to "Docker how to configure container fixed IP". The content is detailed, the steps are clear, and the details are properly handled. I hope this article "Docker how to configure container fixed IP" can help you solve your doubts. Let's go deeper and learn new knowledge together with the ideas of the small editor.

preface

Before using pipework to allocate static ip is temporary, after restart will be invalid, and use pipework bound ip physical machine, virtual machine, docker container ip are in the same network segment, this is very difficult in the production environment, the following use docker comes with the network to achieve fixed ip allocation, and restart will not disappear.

Environment Introduction

binding step

Start with the 192.168.1.105 virtual machine

Step 1: Create a custom network

docker network create --subnet=172.172.0.0/24 docker-br0

Note: The network segment 172.172.0.0 is selected here. You can also specify any other idle network segment. Docker-br0 is the name of the custom bridge. You can name it yourself.

Note: here subnet mask to use 255.255.255.0 is IP after the 24, because later to use iptables configuration routing table, I used 255.255.0.0 can not be configured. So it's configured to be 24.

After creating a bridge, use ifconfig to view an additional bridge, which will be displayed automatically after docker is started or restarted.

Permanently, the bridge can be removed using docker network rm docker-br0.

Step 2: Select any IP address in your custom network segment as the IP address of the container you want to start.

docker run -i -d --net docker-br0 --ip 172.172.0.10 --name nginx -v /usr/local/software/:/mnt/software/ 3bee3060bfc8 /bin/bash

Note: When creating the container, 172.172.0.10 was selected as the static IP address in the segment created in the first step. and starts with docker-br0 bridge. -v is mount, indicating which local directory needs to be mounted into the container. 3bee3060bfc8 Yes Mirror ID

Use docker exec -it nginx /bin/bash to enter the started container, use yum install net-tools to download iptables and then use ifconfig to view container ip

Step 3: Test whether the machine and container ping

Cross-host container visits

Step 4: On the 192.168.1.106 virtual machine, bind the fixed ip to the container, and follow steps 1 to 3 above.

Step 5: Visit each other in two containers, find cross-host container visits, and cannot ping.

Step 6: Configure the routing table

#Add routing rules

ip route add IP segment/subnet mask where the counterpart container is located via the counterpart virtual machine ip dev which NIC to communicate with

For example: ip route add 172.172.1.0/24 via 192.168.1.106 dev eno1677736

After the addition is complete, you can use the route command to view the added rules, or you can use ip route del 172.172.1.0/24 to remove routing rules

Add routing rules to 192.168.1.105 and 192.168.1.106 VMs, respectively!

192.168.1.105: ip route add 172.172.1.0/24 via 192.168.1.106 dev eno16777736

192.168.1.106: ip route add 172.172.0.0/24 via 192.168.1.105 dev eno16777736

Step 7: Access each other in the two containers and find that cross-host containers can ping each other.

Read here, this article "Docker how to configure container fixed IP" article has been introduced, want to master the knowledge points of this article also need to practice to understand, if you want to know more related content articles, welcome to pay attention to the industry information channel.

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

Development

Wechat

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

12
Report