0

Difference between "expose" and "publish" in docker this post describes 3 different options to expose ports:

If you do not specify [EXPOSE or -p], the service in the container will not be accessible from anywhere except from inside the container itself.

If you EXPOSE a port, the service in the container is not accessible from outside Docker, but from inside other Docker containers. So this is good for inter-container communication.

If you EXPOSE and -p a port, the service in the container is accessible from anywhere, even outside Docker.

What if I have defined EXPOSE in Dockerfile and I want container to expose these ports to the host?

Example:

If I have the following Dockerfile.

FROM node:6

# ...

CMD node ./dist/bin/server.js

EXPOSE 8001
EXPOSE 8002

and I run docker run, I want mapping to be setup 8001:8001, 8002:8002.

However, I need to do this without hardcoding the port values using -p option.

The use case:

Migrating from VM based deployment system to Docker based deployment system. Each docker container is running in its own VM. Therefore, there will not be conflicts with other services.

Community
  • 1
  • 1
Gajus
  • 69,002
  • 70
  • 275
  • 438
  • Yes, that's still a problem I think. You could think about using `docker-compose` – n2o Sep 15 '16 at 15:57

2 Answers2

0

To map to specific ports like you've asked you have to use the -p PORT:PORT option.

The other option is to use -P to publish all ports and then docker will randomly assign ports starting typically in the 32700 range to the various ports are that are defined by the image.

erik
  • 1
  • 1
    I have described these options in the original question. It does not answer the question. – Gajus Sep 15 '16 at 16:12
0

Solved my problem with a little :

docker run $(cat Dockerfile | grep EXPOSE | sed -n 's/EXPOSE \([0-9]*\)/-p \1:\1/p' | tr '\n' ' ') app
Gajus
  • 69,002
  • 70
  • 275
  • 438