What is the difference between expose and publish in Docker?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the realm of Docker, understanding the nuances between terms like "expose" and "publish" is crucial for orchestrating containerized applications efficiently. Both terms deal with making network services available but involve different stages and aspects of Docker's network configuration.
Understanding "Expose" in Docker
At its core, "expose" in Docker is a command that serves as a form of documentation within a Dockerfile. The EXPOSE instruction indicates the ports on which a container's application is expected to listen. Importantly, the EXPOSE command does not actually map these ports to the host machine—it merely specifies that the application listens on these ports inside the container.
Syntax and Usage
Within a Dockerfile, the syntax for exposing a port is simple:
For example:
or
Docker relies on this information for potential future automation but does not make any network services on the host available. The EXPOSE directive effectively doesn't do anything at runtime unless it is coupled with Docker-related intentions such as using docker run -P or explicit port mapping.
Key Aspects of EXPOSE
- Documentation: Serves as self-describing documentation for anyone reviewing the Dockerfile.
- No Actual Port Mapping: Does not publish ports or enable external connectivity.
- Networking: Acts in conjunction with service discovery tools or frameworks to define network semantics.
Understanding "Publish" in Docker
In contrast, "publish" is a runtime parameter often denoted in commands like docker run or docker-compose files. It explicitly maps a port on the container to a port on the host machine, thereby allowing external access to container services. This is implemented using the -p or --publish flag.
Syntax and Function
When running a Docker container, you can publish ports using the following syntax:
For example, to expose a web server on port 80 within the container, you might run:
This command forwards requests from port 8080 on the host system to port 80 in the container, allowing for external access to the application.
Key Aspects of PUBLISH
- Actual Port Forwarding: Implements the actual network configuration needed for accessing services.
- Access Control: Through host port configuration, controls what services are externally accessible.
- Security: Requires careful planning to avoid exposing sensitive services unnecessarily.
Combined Usage and Practical Examples
While EXPOSE serves as a form of documenting and potential configuration guidance, publish actively engages host-based port forwarding. If you only utilize EXPOSE in a Dockerfile, the ports will remain inaccessible from outside the Docker host unless you also publish them.
Example Scenario
Suppose you have an application within a Docker container that needs to be accessible from the internet. A Dockerfile might include:
Running the container with:
This combination of EXPOSE and publish ensures the application listens on port 3000 inside the container and is also reachable via port 3000 on the host machine.
Table: Differences Between EXPOSE and PUBLISH
| Aspect | EXPOSE | PUBLISH |
| Purpose | Documentation of expected listening ports | Actual network port mapping to enable external access |
| Dockerfile Directive | Yes | No |
| CLI Command | docker build | docker run with -p, --publish |
| Networking Impact | Internal to the container | Connects host ports to container ports |
| Security | No direct security impact | Needs careful configuration to avoid undue exposure |
| Example Syntax | EXPOSE 443 | docker run -p 80:80 my-container |
Conclusion
Understanding the distinction between "expose" and "publish" is essential for managing Docker containers' networking capabilities. While EXPOSE serves a mainly informational role, publish settings are operational and influence network accessibility. Correct usage of these directives ensures efficient and secure containerized application deployment.

