How to convert a Spring-Boot web service into a Docker image?

When you run a Docker container, all the ports, which any application happen to listen on within it, are not published by default.

In order to publish a port, you need to specify it while running the container using your image. For all the details on how to do it, you can check the “EXPOSE” section in the documentation of docker run command: https://docs.docker.com/engine/reference/run/

Shortly speaking, you want to add another option while running your container:

docker run --name mycontainer1 -p 8080:8080 myimage1

I’m not sure if you wanted to achieve this by adding

EXPOSE 8080

in your Dockerfile. Actually, this does not mean that the port will be exposed when the image is used to run a container. As you might find in Dockerfile reference:

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.

Leave a Comment