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 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 -
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 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
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github