
Docker containers change IP this matter, really not you think so mysterious
Anyone who has worked with Docker knows that the default IP assignment is like opening a blind box, which may give you a whole 172.17.0.X network segment. If multiple containers want to form a LAN, or need a fixed IP to dock the proxy service, this random allocation can be a pitfall. Today, we will understand how to assign a random IP address to a Docker container.Customized IP address, by the way, I'll teach you how to play around with ipipgo's proxy IPs.
Understanding Docker's "Virtual NIC" mechanism first
By default, Docker creates a virtual network called bridge, which is like a separate neighborhood for containers. If you want to change your IP address, you'll have to create a new neighborhood:
Create a new slice called my-net and set the gateway to 192.168.99.1
docker network create --subnet=192.168.99.0/24 --gateway=192.168.99.1 my-net
Be careful that this network segment does not conflict with the host's, it is recommended to use a cold segment such as 192.168.99.X or 10.10.10.X. After you have built it, use thedocker network lsYou'll be able to see the my-net that was just straightened out.
The right posture for assigning a fixed IP to a container
Starting the container with the -ip parameter is done, but there are two pitfalls to be aware of:
1. Customized networks must be specified
Error Demonstration (using the default network will report an error)
docker run -it --ip 192.168.99.10 ubuntu
Correct posture
docker run -it --network=my-net --ip 192.168.99.10 ubuntu
2. IP should be within the network segment
For example, if you build a 192.168.99.0/24 segment, the available IPs are 192.168.99.1~192.168.99.254. It is recommended to make an IP allocation table:
| Container name | use | IP address |
|---|---|---|
| web01 | front-end service | 192.168.99.10 |
| proxy01 | Forwarding agent | 192.168.99.20 |
| db01 | comprehensive database | 192.168.99.30 |
Configuring proxy IPs in containers
This is the time to move out of our ipipgo, take the static residential agent as a chestnut:
Set the global proxy in the container
export http_proxy="http://用户名:密码@gateway.ipipgo.net:端口"
export https_proxy="http://用户名:密码@gateway.ipipgo.net:端口"
Test the IP to see if it works
curl ipinfo.io
If you see that the returned IP turns out to be ipipgo's node, it's solid. We recommend using theirStatic Home Package, 35 bucks a month for a fixed IP, much more reliable than dynamic.
Frequently Asked Questions QA
Q: What should I do if I can't get a ping between containers after changing the IP?
A:先查防火代理规则,再确认所有容器都在同一个自定义网络。可以用docker network inspect my-net看连接状态。
Q: What should I do if the proxy IP suddenly fails to connect?
A: First use docker exec into the container to test network connectivity. If it is ipipgo's node problem, their customer service response thief fast, the background can also self-service switch export node.
Q: How do I manage agents for multiple containers in bulk?
A: It is recommended to use ipipgo's API to dynamically obtain proxies and write a shell script to update them regularly. Their API documentation can be understood by elementary school students, support for json and txt multiple formats.
Say something from the heart.
In fact, the network configuration of this thing, it is like building blocks, try a few times to understand the door. ipipgo has a thief good use of the function--Client-side one-click configurationI don't have to memorize all those complicated commands. Especially for crawlers or data collection old man, directly choose their TK line, than tossing Docker network to save a lot of heartache. Remember, technology is for business services, do not put the cart before the horse.

