MiamiBeach
MiamiBeach

Reputation: 3497

Docker: what is the difference in docker ps output between 123-125/tcp and 0.0.0.0:123-125->123-125/tcp notations?

After starting docker container with standart command ...

docker run -p 123-125:123-125 -it <container_name> "bin/bash" 

... and exiting it with CTRL-P + CTRL-Q, I noticed that suddenly I see 2 blocks in port column of docker ps output:

100-102/tcp, 0.0.0.0:123-125:123-125/tcp

I was never setting 100-102 ports for my container.

I cannot get 2 things:

  1. what is the difference between these 2 notations: 100-102/tcp and 0.0.0.0:123-125:123-125/tcp in docker ps output?
  2. why I have additional 100-102/tcp output in docker ps if my -p parameter was only listing 123-125:123-125/tcp?

Upvotes: 1

Views: 448

Answers (3)

Faeeria
Faeeria

Reputation: 826

The first parameters are the ports exposed by your container. They were probably exposed in the Dockerfile used to create the image. They are only a kind of specification/documentation telling you you should probably publish those ports.

The second parameters are the exposed ports that you bound to your host system : those portsare linked to ports from your host, and they are open to outside connections coming from any IP address (0.0.0.0). These ports are published (thus the -p)

If you had run your container with the -P options, it would have published/bound all of the exposed ports (or port range, in your case). Publishing ports with -p only expose and bind the ports you specify with that options. Which is what you want here.

Here is an example to be clearer :

  1. I create a Dockerfile. In it I expose the port range 3000-3002
FROM node:current-alpine

EXPOSE 3000-3002/tcp

COPY . /app

WORKDIR /app

CMD ["node", "server.js"]

  1. I build the image : docker build -t testing .
  2. I run a container : docker run testing

Here is the result of docker ps :

CONTAINER ID   IMAGE     COMMAND                 [...] PORTS           NAMES
59b8a12a8d70   testing   "docker-entrypoint.s…"  [...] 3000-3002/tcp   amazing_gagarin

As you can see, I didn't publish any ports, but I can see my exposed ports in the PORTS section of docker ps

  1. I try to connect to localhost:3000 : nothing works -> the ports aren't published.

  2. I launch another container with docker run -p 3005-3007:3005-3007 testing.

Here is the result of docker ps :

CONTAINER ID   IMAGE     COMMAND                 [...] PORTS                                             NAMES
d55dd52d94a3   testing   "docker-entrypoint.s…"  [...] 3000-3002/tcp, 0.0.0.0:3005-3007->3005-3007/tcp   agitated_boyd
  1. I try to connect to localhost:3005 : it works ! -> I published my ports, binding them to my host ports, so I can connect to them (also because my app does listen to ports from 3000-3007)

  2. I try to connect to localhost:3000 : it still does not work -> the port range 3000-3002 is still not published, but is still marked as exposed, telling me that I might want to use this range.

Upvotes: 2

Charlie
Charlie

Reputation: 23838

The publish option (-p) is not following any of the allowed formats:

ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort

The given run command has 3 colon - but the first part is not an IP. So, I believe that Docker is not taking your command well.

The correct run command, if you want to match container ports 123..125 to the corresponding host portsm is:

docker run -p 123-125:123-125 -it <container_name> "bin/bash" 

Upvotes: 0

user14967413
user14967413

Reputation: 1416

Please see Docker ref for the format of -p option:
format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort

  1. 0.0.0.0 specifies the IP from which your container can be accessed on the host. 0.0.0.0 is the default and means all IP addresses (i.e. everybody connecting to your host can access your container via exposed ports 123-125). On the other hand, 127.0.0.1 would mean you can access your container only from localhost.

  2. I assume the additional port could be exposed by your base image (defined in FROM in your Dockerfile).

Btw, I am not sure if your syntax 0.0.0.0:123-125:123:125 with three colons is correct.

Upvotes: 0

Related Questions