How to deal with multiple container ports?

I have a few containers running different web apps, they all bind the same port 80 in the container to random port on the host system so they only way to tell them apart is by memorising the random ip or running docker ps and copying it from there.

Is there a “docker” way of handling this? I used to run multiple VMs before and, since each one had its own IP, I could add an entry in the hosts file pointing to that IP and not worry about it, is there a way to do something similar? As I understand you can’t bind a hostname to the same IP using a different port so it doesn’t seem as straightforward.

Suppose your machine IP is 192.160.xx.xx , docker container ip as 172.10.xx.xx.

Let us consider your case where you expose 80 port of your container… it will like
172.10.xx.xx:80 pointing 192.160.xx.xx:

This happens only when you provide to publish all port, instead if you know the number of webapps your going to use, then you can dedicate your system port to 80 port of a container webapp(publish the port yourself)
(docker run -p 0.0.0.0:5000:80 imagename) ip:hostPort:containerPort
1st container 172.10.xx.2:80 to 192.160.xx.xx:5000
2nd container 172.10.xx.3:80 to 192.160.xx.xx:6000
3rd container 172.10.xx.4:80 to 192.160.xx.xx:7000

So the IP will be your machine IP and the port is fixed separate webapps.

Hope this helps

I understand, however the problem is that I’d need to memorise the port number, in your example it looks something like:

Web App 1: 192.160.xx.xx:5000
Web App 2: 192.160.xx.xx:6000
Web App 3: 192.160.xx.xx:7000

So if I want to work on Webb App 2 I need to remember what port number it is or run docker ps or something similar to remind me, however if I could do something like:

Web App 1: webapp1.dev
Web App 2: webapp2.dev
Web App 3: webapp3.dev

I wouldn’t need to worry about port numbers and whatnot, is there a way to do something like this or similar?

If the webapps are fixed and will be used for a very long time, I guess you can map the port to a private domain using the local server like ngnix

server {
listen webapps1.dev:80;
server_name webapps1.dev;
location / {
proxy_pass http://127.0.0.1:5000;
}
}

1 Like

That’s interesting, I’ll give that a try thanks!