Docker makes it easy to run applications 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, in addition to creating virtual network components such as VLANs, bridges, or aliases. If you list all the devices on your system with
ip addr showYou will find several interfaces like
eno1 that represent real 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.
This works well with Docker, which allows you to handle networking at the Docker level rather than the application level. With Docker, the application inside the container can bind to “port 80”, which Docker maps to a specific IP address on the host.
This makes it much easier to separate the application layer from the host that runs it. For example, you could have several different API services running on the same machine, without configuring the underlying containers.
It doesn’t really matter to Docker which system interface the IP address is on, as Docker’s internal network will take care of this for you, as long as you start the containers with the correct configuration.
Running Multiple Dockers Services on the same server
Running a container at a specific address is quite easy, depending on how you start it. Essentially, most Docker containers have their ports configured with the
host:container Format. For example,
5000:80 would take port 80 of the container and make it available from port 5000 of the host.
However, you can actually bind sockets directly, i.e. instead of
5000can substitute a
IP:PORT par, using three colons for the entire binding:
docker run -it -d ipaddress:hostport:containerport --name web nginx
So, for example, you could have two NGINX containers on different IPs, like this (remember, Docker needs separate container names):
docker run -it -d 22.214.171.124:80:80 --name web nginx docker run -it -d 126.96.36.199:80:80 --name web2 nginx
If you’re using Docker Compose, the setup is similar. In the
ports section for the service, you can use the same syntax to bind to particular addresses.
version: "3" services: nginx: image: nginx restart: always ports: - "188.8.131.52:80:80"
In either case, you can create multiple services bound to host port 80, as long as the services do not listen on the same IP addresses.
However, if you’re doing this, you’ll want to make sure no container is listening single 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 one IP address, so you may need to configure others yourself.
Configuring multiple IP addresses per server will depend on the host you are using. For example, AWS has their “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’ll pay $3.65 a month for each.
Some services will allow you to make a one-time purchase of IP addresses, such as OVH, which allows block purchases up to /24 in size.
If you want to buy large, contiguous blocks of IP addresses for yourself to actually own, you can do so through a number of brokers, although this usually incurs a high 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 is up to you to configure your network interface to use it, using a tool like