How to publish ports in docker files?
Asked Answered
C

4

40

I need to map the ports on the host to the ports on the container. I can achieve this by running the "docker run" command with the -p option. How do I achieve this through the Dockerfile? Using the following gives a "deprecated error"

EXPOSE 80:8080

How else can I make the exposed ports public through the dockerfile?

Compartment answered 23/9, 2015 at 13:1 Comment(0)
M
59

You can't. What ports are published on the docker host is strictly a decision that should be made by the local administrator, not by the image they are trying to run; this would be (a) a security problem (hey, I just opened up ssh access to your system!) and (b) prone to failure (my webserver container can't bind on port 80 because I'm already running a server on port 80).

If you want to avoid long docker run command lines, consider using something like docker-compose to automate the process. You can then pass docker-compose a configuration like:

mywebserver:
  image: myname/mywebserver
  ports:
    - 80:8080

And then a simple docker-compose up will start your container with container port 8080 bound to host port 80.

Update 2017-03-11

In response to Willa's comment:

  • Using docker-compose will not help with the port collision issue. The port collision issue is a reason why images should not be able to specify host port bindings. I was simply offering docker-compose as an alternative to long docker run command lines with multiple port bindings. The port collision issue would potentially allow a container to perform a denial-of-service attack on your host: if, for example, a container starts and binds to port 80 before an Apache server on your host (or in another container), then you have just lost your web service.

  • Regarding the security issue: If an image were able to specify host port bindings, it would be possible for containers to open up access to the container without your knowledge. Permitting a remote user to access a container on your host opens you up to the possibility of a host compromise in the event that the namespace features in the kernel fail to completely isolate the container, and even if you completely trust the isolation it opens you up to potential legal problems if that container is used for illicit purposes. In either case it's a bad idea.

Mabelmabelle answered 23/9, 2015 at 13:20 Comment(3)
How is using docker-compose solve the potential for port collision though? Besides can you expound a bit on the security issue?Collaboration
I need to solve this without docker-compose. I've been searching for 4-5 hours to the problem, nothing i can find works.Samaria
docker-compose is just a fancy wrapper around docker run. You can of course accomplish the same thing with the -p (--publish) option to docker run.Mabelmabelle
B
23

There's a difference between expose and publish.

Expose means to open the port on the container side, publish means to open it on the Docker host to the outside world.

For example, if your docker run command had -p 80:8080, it's exposing port 8080 on the container and publishing port 80 on the host.

You can only really expose ports in the Dockerfile, if you want the flexibility to publish the ports, you'll need to use the -P opting with docker run. Like so:

docker run -P your_app
Boucher answered 23/9, 2015 at 13:22 Comment(1)
One more thing to mention here is that publishing happens to random ports: -P, --publish-all Publish all exposed ports to **random** ports. To see the exact mappings use docker port CONTAINER_NAMEKatelin
C
1
  1. EXPOSE a list of ports on your Dockerfile.

  2. Run docker run -d -P --name app_name app_image_name.

  3. After the previous steps succeed,

    run docker port app_name which will display you an output like below:

    80/tcp -> 0.0.0.0:32769 443/tcp -> 0.0.0.0:32768

Congruity answered 14/8, 2020 at 18:9 Comment(0)
L
0

What you can do is to EXPOSE a list of ports on your Dockerfile and later run the command docker run -P your_app to publish all the ports exposed on the Dockerfile

Luhey answered 9/11, 2018 at 12:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.