Can muliple containers use same port?

Hi guys,

Can multiple containers run on same port if we are not exposing port to host?

Thanks in advance.

1 Like

Your question is ambiguous.

Each container has it’s own ip (unless --network=host is used). As such each container can bind the same container port.

If it’s about published ports: of course not.
Regardless whether docker is involved or not, a port can only be bound once for an ip.

1 Like

Sorry for not explaining clearly.
If i have a service(application) running on any port say 3000 in container1. Will it be accessible in container2 ?
I have not published or exposed port from conatiner1.

I am trying to run e2e tests in different conatiners on same port, im getting address already in use error.

Something is not adding up here. Please share how you start these containers (the exact(!) commands or if docker compose is used, the content of the compose file - please do not redact anything other than ips, domain names, usernames and passwords.), so we can understand what you actually do, and suggest what might need to be done differently.

Ambigious question, I am unable to understand and don’t have enough details.

This is the Dockerfile
FROM Microsoft Artifact Registry
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app/
ARG CACHE_BUST
RUN yarn

I create 2 images from this Dockerfile
docker build -t image1 .
docker build -t image2 .

I start 2 different containers from 2 different images and run e2e script (for running end to end tests)
docker run --name container1 image1 bash -c “npm run e2e”
docker run --name container2 image2 bash -c “npm run e2e”

e2e script starts application in 3000 port and run tests.
When i run e2e scripts simultaneously in 2 different containers I get this below error,
Error: listen EADDRINUSE: address already in use 0.0.0.0:3000

Is the highlighted explanation correct ?