Hi everyone, I'm exploring the idea of assigning a single external IP address to a Docker network instead of assigning individual IPs to each container. For example, I have two containers, 'foo' and 'bar', that are part of network 'A' and utilize ports 80 and 443 respectively. I'd like to route traffic from a specific address (let's say 'whatever') to these containers so that requests to 'whatever:80' reach 'foo' and 'whatever:443' reach 'bar'. While I know that assigning IPs directly to containers is an option, I'm interested in maintaining a logical grouping for my containers within the network. Any suggestions on how to achieve this? Thanks!
5 Answers
My advice? Just clarify what you want instead of fixating on how you think it should be solved. Sometimes the simplest approach, like using a reverse proxy, is all you need for managing container traffic effectively!
What you're asking for can be tackled nicely with a reverse proxy. You could run a third container serving as a proxy, like NGINX, and set it up to manage the routing to 'foo' and 'bar'. Assign it an IP and an FQDN you prefer. It will handle the traffic for you, maintaining a neat connection to your service containers.
It sounds like you might be getting mixed up with the way Docker handles networking. To directly answer your question, there's no straightforward way to assign an external IP to an entire network instead of to individual containers. Instead, you should consider using a reverse proxy. This way, you can route traffic from a single external IP to multiple containers based on subdomains or paths. For example, you’d have one IP and then set up rules in the proxy to direct traffic appropriately. It's a cleaner, more manageable solution!
One alternative is to create a MacVLAN network, which can give your containers their own IPs that are part of your LAN. This way, they can be addressed directly from the outside without needing extra ports, but it can get a bit complex. Or, you could set up a tunnel solution like Cloudflare that manages your container's IP connectivity through a specific route without tightly coupling it to IP addresses directly. Just some options to consider!
When you launch containers, they usually end up on Docker's default bridge network, which gives them internal IPs that are NAT'd to your host's IP. The common way to access these from outside is by forwarding ports—like mapping host port 8080 to container port 80 for 'foo' and 8081 to 80 for 'bar'. This means both containers can be accessed via the same external IP, just different ports. Is this what you're aiming for?

Totally agree! A reverse proxy lets you keep everything in one place without needing extra VM resources, and you can set everything up efficiently.