-2

I have following in Dockerfile for a spring boot project:

EXPOSE 8081

Does it mean that container would deploy the application on its port 8081?

And then in docker run command, I need to do:

docker run -p 8080:8081 -t imagename

So that URL http://localhost:8080 would map to container's 8081 port where application is deployed, and hence is accessible.

5
  • What is your question exactly? from docker documentation: "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" Apr 15, 2020 at 17:35
  • I have settings as in the post. But localhost:8080 doesn't work for me. So I ma not sure about the mappings here.
    – Mandroid
    Apr 15, 2020 at 17:37
  • Are you running docker on linux host? If yes, then paste the content of /etc/hosts/ file? Apr 15, 2020 at 17:40
  • 1
    Does your springboot project run on port 8080 or port 8081? Apr 15, 2020 at 17:40
  • And is your springboot project listening on all interfaces or only loopback/127.0.0.1?
    – BMitch
    Apr 15, 2020 at 18:02

3 Answers 3

1

If you want to access your service from outside docker you have to use the -p flag to publish the port. The EXPOSE instruction doesn't publish the port. You can also use the -P to publish every exposed port.

See https://docs.docker.com/engine/reference/builder/#expose

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.

5
  • Thats's what I have done as mentioned in post. I have used -p 8080:8081. But I am not able to access end point doing, localhost:8080.
    – Mandroid
    Apr 15, 2020 at 17:42
  • Your service in your container is running on port 8081 ? Can you run docker ps to check the mapping
    – Mickael B.
    Apr 15, 2020 at 17:46
  • 8081/tcp, 0.0.0.0:8081->8080/tcp
    – Mandroid
    Apr 15, 2020 at 17:47
  • In your container can you run netstat -tulpn | grep 8081
    – Mickael B.
    Apr 15, 2020 at 17:49
  • Wait it should be the other way 0.0.0.0:8080->8081/tcp. Maybe you can use the -P to publish every exposed port and check again docker ps
    – Mickael B.
    Apr 15, 2020 at 17:52
0

"EXPOSE" command is particularly useless while running the container on local machine. Its for the Docker management service (Say AWS Elastic BeanStalk). when EB sees a "EXPOSE PORT" command in the Dockerfile, it automatically does the port mapping for you. Under the hood, it will start the container with: Docker run -p "targetPort:ContainerPort" image-name

To answer your question: Does it mean that container would deploy the application on its port 8081? Container is not deployed at that port (8081), rather a mapping is created from: the container with PORT (8081) (running inside a linux machine) to the computer on which you access the browser.

Port mapping with -p flag is some what like this: Your machine port(Whatever you use on your browser): What the internal container is working on

1
  • I appreciate the effort in this answer. However, I am unable to understand what your answer means starting from " Container is not deployed at that port (8081)...." to the end. Are you saying, when a request comes from an external client to my laptop (where the docker container is running) with destination = Laptop_IP:8081, the request is forwarded to the container on Container_IP:80 ?
    – Jayanth
    Dec 29, 2022 at 8:36
0

Quoting from documentation for "docker run -p":

    ----
    docker run -p 127.0.0.1:80:8080/tcp ubuntu bash
    ----

This binds port 8080 of the container to TCP port 80 on 127.0.0.1 of the host machine...

Note that ports which are not bound to the host (i.e., -p 80:80 instead of -p 127.0.0.1:80:80) will be accessible from the outside.

In your case, you mentioned:

    ----
    docker run -p 8080:8081 -t imagename
    ----

Since, localhost is not mentioned in this command, it would mean even requests from other machines reaching your machine with destination = machine_IP:8080 would be directed by your machine to container:8081. The container is expected to listen on 8081.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.