skip to Main Content

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


  1. The official documentation is pretty clear on the purpose

    The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

    So if you use the -P (capital P) option it will expose the listed ports with EXPOSED (to random ports on the host).

    And it also functions for documentation purposes to other people consuming and re-purposing those dockerfiles.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search