Docker
expose
publish
container networking
port configuration

What is the difference between expose and publish in Docker?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

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:

dockerfile
EXPOSE <port> [<port>/<protocol>...]

For example:

dockerfile
EXPOSE 80

or

dockerfile
EXPOSE 80/tcp 443/tcp

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:

bash
docker run -p <host_port>:<container_port> <image_name>

For example, to expose a web server on port 80 within the container, you might run:

bash
docker run -p 8080:80 <image_name>

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:

dockerfile
FROM node:alpine
EXPOSE 3000
CMD ["node", "app.js"]

Running the container with:

bash
docker run -p 3000:3000 my-node-app

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

AspectEXPOSEPUBLISH
PurposeDocumentation of expected listening portsActual network port mapping to enable external access
Dockerfile DirectiveYesNo
CLI Commanddocker builddocker run with -p, --publish
Networking ImpactInternal to the containerConnects host ports to container ports
SecurityNo direct security impactNeeds careful configuration to avoid undue exposure
Example SyntaxEXPOSE 443docker 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.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.