i really do not understand the following command in Dockerfile
EXPOSE 8080
I built a java application and dockerized it via this Dockerfile
FROM openjdk:10-jre-slim
WORKDIR /app
COPY ./target/display-console-1.0-SNAPSHOT.jar /app
CMD ["java", "-jar", "display-console-1.0-SNAPSHOT.jar"]
My java application got a controller which listen on port 8085.
So when i use it from my localhost, i just do something like
docker run -ti my-docker-hub-account/my-image -p 8085:8085
and all works perfectly !
So, what is the interest of command
EXPOSE XXXX
in Dockerfile ?
thanks by advance
2
Answers
The official documentation is pretty clear on the purpose
So if you use the
-P
(capital P) option it will expose the listed ports withEXPOSED
(to random ports on the host).And it also functions for documentation purposes to other people consuming and re-purposing those dockerfiles.
The EXPOSE command let the docker container be accessed over network on the specified port / port range.
When you run a web service from a docker container you must use this command so that you can access it from the outside of the container.