How to Run Multiple Docker Containers on Different IP Addresses


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:

docker run -it -d ipaddress:hostport:containerport --name web nginx

So, for example, you could have two NGINX containers on different IPs, like so (keep in mind, Docker needs separate container names):

docker run -it -d 123.0.0.1:80:80 --name web nginx
docker run -it -d 123.0.0.2:80:80 --name web2 nginx

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.

version: "3"
services:
  nginx:
    image: nginx
    restart: always  
    ports: 
      - "123.0.0.1:80:80"

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.





Source link

Previous articleSri Lanka’s Central Bank Warns About Crypto Amid Severe Economic, Political Crisis – Regulation Bitcoin News
Next articleBitcoin hits lowest since December ’20 amid ‘throes of crypto winter’