If you’re like most people, you probably have a few containers running on different IP addresses. You might have a web container on 192.168.1.10 and a development container on 192.168.1.25, or you might have a production container on 192.168.1.XXX and a development container on 192.168.1.YYY . There are many reasons why you might want to run multiple containers on different IP addresses, but here’s one reason that comes to mind: if something goes wrong with one of your containers, you can easily roll back to the last working version of that container by running it from another IP address or port! To do this, all you need is a few lines of code in your Dockerfile : FROM docker:2 # Use the latest stable release of Docker RUN apt-get update && apt-get install -y build-essential cmake gcc libssl-dev libgcc-dev \ libpng12-dev libjpeg8-dev libtiff6-dev \ libxml2-utils \ python3 RUN pip3 install -r requirements.txt # Add the new IP address for your development environment ADD 10.0.. TO my_docker_ip # Add the new IP address for your production environment ADD 172.. TO my_docker_ip
Docker makes it easy to run apps using serverless cloud solutions, but many people will prefer to manage multiple containers running on a few powerful machines. In this case, using multiple IP addresses can be a great way to manage multiple services on the same port.
How Do Network Interfaces Work?
Linux uses network interfaces to represent physical hardware, as well as creating virtual networking components like VLANs, bridging, or aliases. If you list all the devices on your system with ip addr show, you’ll find various interfaces like eth0 and eno1 which represent actual connections.
IP addresses are a separate system from network interfaces, but essentially, you can have multiple IP addresses configured on a single interface, allowing you to bind services to network sockets for each IP:PORT combination.
This works well with Docker, which lets you handle the networking at the Docker level instead of the application level. With Docker, the application inside the container can just bind to “port 80,” which gets mapped by Docker to a specific IP address on the host.
This makes it far easier to separate the application layer from the host that’s running it. For example, you could have multiple different API services all running on the same machine, without configuring the underlying containers.
It doesn’t really matter for Docker which system interface the IP address is on, as Docker’s internal networking will handle this for you, as long as you launch the containers with the correct configuration.
Running Multiple Docker Services on the Same Server
Running a container on a specific address is pretty easy, depending on how you launch it. Essentially, most Docker containers have their ports configured with the host:container format. For example, 5000:80 would take the container’s port 80 and make it available from the host’s port 5000.
However, you can actually bind sockets directly, meaning instead of 5000, you can substitute an IP:PORT pair, using three colons for the entire binding:
So, for example, you could have two NGINX containers on different IPs, like so (keep in mind, Docker needs separate container names):
If you’re using Docker Compose, the configuration is similar. In the ports section for the service, you can use the same syntax to bind to particular addresses.
In either case, you can create multiple services bound to host port 80, as long as the services don’t listen on the same IP addresses.
If you are doing this, however, you will want to make sure that no container is listening on just the port—this will be a misconfiguration, as omitting the IP address will mean that it will listen on all addresses for that interface.
How Do You Get Multiple IP Addresses?
Most servers come with only a single IP address, so you may have to set up additional ones yourself.
Setting up multiple IPs per server will depend on the host you’re using. For example, AWS has it’s “Elastic IP” service, which is free to use if you’re using one IP per machine. However, if you want to purchase additional Elastic IP addresses, you can associate them with any server. You will pay $3.65 a month for each one.
Some services will allow you to make a one-time purchase of IP addresses, like OVH, which allows purchases of blocks up to /24 in size.
If you want to purchase large, contiguous blocks of IP address for you to actually own, you can do so through various brokerages, though this generally incurs a large fee and is mostly for large organizations.
Once you have the IP addresses, it’s up to your cloud provider to point them to your address. However, it’s up to you to configure your network interface to use it, using a tool like netplan.