How Do You Create Docker Containers To Have Unique IP Addresses?

Problem scenario
How do you create Docker containers to have unique IP Addresses (but not the default 172.x.x.x type)?

Solution
(If you need help installing Docker, see this posting.)

Create new IP addresses with these commands (with sudo in front of them, preferably, or less preferably as the root user):

ip addr add 33.33.33.38/28 brd + dev eth0
ip addr add 33.33.33.39/28 brd + dev eth0
ip addr add 33.33.33.40/28 brd + dev eth0

# Replace the IP addresses and subnet masks as you desire

Then use a modified version of this command:
docker run -p 33.33.33.38:80:80 repositoryName:versionDesignation /bin/bash
# You can use the "docker images" command to find the "repositoryName" and "versionDesignation"
# The IP address and port mapping (from Linux server to Docker container) can be substituted as needed

Two or more containers on the same Docker host can use port 80 using this method.  To have the IP addresses available upon every log in, you can create a /etc/profile.d/custom.sh file.  This will give you the IP addresses on the Docker server upon logging in each time.  The custom.sh file should have the lines you entered above after a "#!/bin/bash" header.  Here is an example:

#!/bin/bash
ip addr add 33.33.33.38/28 brd + dev eth0
ip addr add 33.33.33.39/28 brd + dev eth0
ip addr add 33.33.33.40/28 brd + dev eth0 

Leave a comment

Your email address will not be published. Required fields are marked *