Difference between "expose" and "publish" in docker this post describes 3 different options to expose ports:
If you do not specify [
EXPOSEor-p], the service in the container will not be accessible from anywhere except from inside the container itself.If you
EXPOSEa 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
EXPOSEand-pa 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.