When I am not exposing any ports when writing my Dockerfile, nor am I binding any ports when running docker run
, I am still able to interact with applications running inside the container. Why?
I am writing my Dockerfile for my Node application. It's pretty simple and looks like this:
FROM node:8
COPY . .
RUN yarn
RUN yarn run build
ARG PORT=80
EXPOSE $PORT
CMD yarn run serve
Using this Dockerfile, I was able to build the image using docker build
$ cd ~/project/dir/
$ docker build . --build-arg PORT=8080
And run it using docker run
$ docker run -p 8080 <image-id>
I then accessed the application, running inside the Docker container, on an IP address like http://172.17.0.12:8080/
and it works.
However, when I removed the EXPOSE
instruction from the Dockerfile, and remove the -p
option in docker run
, the application still works! It's like Docker is automatically binding my ports
Additional Notes:
- It appears that another user have experienced the same issue
- I have tried rebuilding my image using
--no-cache
after I removed theEXPOSE
instructions, but this problem still exists. - Using
docker inspect
, I see no entries forConfig.ExposedPorts