Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
The EXPOSE instruction used in Dockerfiles informs Docker how a running container maps internal ports to external ones. This post will cover how to use this instruction effectively in Dockerfiles as well as how to publish ports to connect apps outside of the containerâs network and to the host machine.
Define port mappings using EXPOSE
When writing your Dockerfiles, the instruction EXPOSE tells Docker the running container listens on specific network ports. This acts as a kind of port mapping documentation that can then be used when publishing the ports.
EXPOSE <port> [<port>/<protocol>...]
Here is an example Dockerfile that includes the EXPOSE instruction.
FROM debian:stableRUN apt-get update && apt-get install -y --force-yes apache2EXPOSE 80 443VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"]ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
You can also specify this within a docker run command, such as:
docker run --expose=1234 my_app
But EXPOSE will not allow communication via the defined ports to containers outside of the same network or to the host machine. To allow this to happen you need to publish the ports.
Publish ports
There are several flags you can use when using the docker run command to publish a containerâs ports outside of the containerâs network and map them the host machineâs ports. These are the flags -p and -P, and they differ in terms of whether you want to publish one or all ports.
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.âââDocker docs:Â EXPOSE
docker run -p 80:80/tcp -p 80:80/udp my_app
In the above example, the first number following the -p flag is the host port, and the second is the container port.
To publish all the ports you define in your Dockerfile with EXPOSE and bind them to the host machine, you can use the -PÂ flag.
docker run -P my_app
View ports on a running container
Once you have run your container and published its ports, you can then view the port mappings using the docker port command.
docker port CONTAINER [PRIVATE_PORT[/PROTOCOL]]
You can use this to lists all port mappings or just a specific mapping for a Docker container.
docker port test_container7890/tcp -> 0.0.0.0:43219876/tcp -> 0.0.0.0:1234
docker port test_container 7890/tcp0.0.0.0:4321
Find out more
This article has covered how to define port mappings in a Dockerfile using EXPOSE, how you can then publish these to connect to the host machine and other apps, as well as viewing the ports of a running container. To learn more see the Docker guides as well as my other posts on Docker.
Docker Documentation
Read more from ryanwhocodes
- Run bash or any command in a Docker container
- Top 10 Docker commands you canât live without
- The ups and downs of docker-composeâââhow to run multi-container applications
Dockerfile EXPOSE: how to define ports for Docker containers was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.