New to Docker, how to deal with duplicate ports best?

Hello
im new to docker so please forgive what might be a silly question.
Ive read thru a number of articles and posts here but i think ive got a disconnect on how to best deal with applications that use the same posts within a container.
For example multiple containers that want to use simple https 443. Typically these would be separated by path or other means.
When i try to run them i get errors saying port is already in use.
Any pointers would be appreciated.

Hello and welcome,

you are right - by default only one container can provide services on a specific port to the outside world (= to other computers within your network).
EITHER container nginx_1 OR nginx_2 can provide its port 443 as port 443 to the outside world.

So you have some options to deal with this “issue”:

  1. let container nginx_1 provide it service on computer’s port 443 and nginx_2 on computer’s port 444.
  2. you can use a reverse proxy serving on computer’s port 443 and forwarding hte requests to container nginx_1 or nginx_2 depending on the path, the hostname, whatever…

For a beginner I would choose option 1). For docker-compose.yml you can modify the first port-number to specify the port to be used on the outside world:

[...]
services:
  kb4_nginx:
    image: nginx:latest
    ports:
      - '8083:80'
[...]

will allow me to use computer’s port 8083 to connect to container’s port 80.
I you use plain docker-commands you can do the same with -p <outside-port>:<container-port>.

For option 2) I would choose traefik (of course there are other options available) which itself can be run as a docker-container and forward the requests on the computer’s port :443 to the backend containers (nginx_1 and nginx_2 in this example). If you need more information about this way feel free to ask and I can provide some sample-configuration on how to configure traefik for https and what to add to the backend-containers to be used by the traefik-container.

Thank you.
Yes, i understand they need different ports at the host.
I tried editing some of those thru the web gui but i must have been missing something as it wouldnt let me edit the in place container it was trying to deploy a new one and trying to edit at the command line told me it was already there so i must be missing something.
As i said, new, probably something silly :slight_smile:

I am at a loss. How did you start the container and how did you specify the port within the web ui (which web ui?)?

According to documentation I found in the internet it is not possible (at least not a official/supported way) to change the published port of a created container. Instead stop and remove the container and restart it with the desired port-mapping.

At the command line you can get a list of running and stopped containers with docker ps -a.
With docker container stop <containername|containerid> you can stop a running container and with docker container rm <containername|containerid> a stopped container can be removed.
It is a good idea to remove stopped and unused/abandoned containers as you cannot have multiple containers with the same name.

But maybe I didn’t get the point of your issue :question:

Im new to this so hopefully im making sense, but i started these using stock docker run or docker compose commands without any variables or optional parameters.
I knew there would be come conflicts but i assumed i would be able to edit those thru the gui, in this case portainer.
Im if understanding this ill need to do that over adding in options to change the ports?
Is there no way to edit that after the fact?