What is the difference between "expose" and "publish" in Docker?
In Docker, "expose" and "publish" are two related but distinct concepts that are used to control network access to containers.
The "expose" instruction is used in a Dockerfile to specify which ports the container should listen on for incoming connections. For example, the following Dockerfile excerpt exposes port 80 for HTTP traffic:
FROM nginx
EXPOSE 80
Note that the "expose" instruction does not actually open any ports on the container's network interface, nor does it create any network mappings. Instead, it simply documents which ports the container is designed to use.
The "publish" option, on the other hand, is used with the "docker run" command to create a network mapping between a port on the host system and a port exposed by the container. For example, the following command runs a container based on the nginx image and publishes port 8080 on the host system to port 80 in the container:
docker run -p 8080:80 nginx
This allows the container to receive incoming traffic on port 8080 from the host system's network interface, and forward it to port 80 inside the container.
So, in summary, "expose" is used to declare which ports a container listens on, while "publish" is used to create a network mapping between a port on the host system and a port exposed by the container.
-
How to Connect to the Localhost of the Machine from inside of a Docker Container?
If you are running some kind of a server or any other service (such as a database) on localhost and the service isn’t exposing any port, you may have found yourself wondering how to connect to this...
Questions -
How Do I Edit a File inside a Docker Container?
If you want to edit a file inside a docker container, you can follow these steps: 1. Find the id of the container To find the id of the running container, you can use the following command: docker ...
Questions -
How to Fix Docker Permission Denied Issue?
If you are struggling with the Docker permission denied error, we have prepared a quick fix for you. Step 1 - Create a docker group The first step is you create a docker group if you haven’t done i...
Questions -
What Is the Difference between Docker-Compose Ports and Expose?
You may have wondered what is the difference between docker-compose ports and docker-compose expose. Here is a clear explanation to help you better understand the matter. Ports Ports mentioned in t...
Questions
We are hiring.
Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.
Help us in making the internet more reliable.

Help us with developer education and get paid.

Reliability is the
ultimate feature
Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.
Explore Better Stack
