The Docker container specifies the fixed IP and static IP address for a custom segment
- 2020-05-30 21:38:47
- OfStack
The Docker container specifies the fixed IP/ static IP addresses for the custom network segment
Step 1: create a custom network
Note: the 172.172.0.0 segment is selected, and any other free segment can be specified
docker network create --subnet=172.172.0.0/16 docker-ice
Note: docker-ice is the name of the custom bridge.
Step 2: select any IP address from your custom segment as the static IP address for the container you want to launch
Note: here, 172.172.0.10 is selected as the static IP address in the web segment created in step 2. Here's an example of starting docker-ice.
docker run -d --net docker-ice --ip 172.172.0.10 ubuntu:16.04
other
Note 1: here is a continuation of an application scenario with a fixed IP address, which can be ignored for record only.
Note 2: if you need to change the source address of a request from a container with an IP address to another routable IP address on the host, iptables can be used. For example, static
If the source address of the outgoing request is changed to public IP104.232.36.109 (provided that the IP address exists on the machine), the following command can be executed:
iptables -t nat -I POSTROUTING -o eth0 -d 0.0.0.0/0 -s 172.18.0.10 -j SNAT --to-source 104.232.36.109
Thank you for reading, I hope to help you, thank you for your support of this site!