Docker - EXPOSE Instruction

Last Updated : 8 Sep, 2025

The EXPOSE instruction exposes a particular port with a specified protocol inside a Docker Container.  In the simplest terms, the EXPOSE instruction tells Docker to get all the information required during the runtime from a specified port. These ports can be either TCP or UDP, but it's TCP by default. It is also important to understand that the EXPOSE instruction only acts as an information platform (like Documentation) between the creator of the Docker image and the individual running the Container.  Some points to be noted are: 

  • It can use TCP or UDP protocol to expose the port.
  • The default protocol is TCP if no other protocol is specified.
  • It does not map ports on the host machine.
  • It can be overridden using the publish flag (-p) while starting a Container.

Expose Vs Publish in Docker

Purpose

  • Expose: Specifies ports exposed from the container to the container network.
  • Publish: Maps container ports to specific host ports for external access.

Usage

  • Expose: Exposes ports within the container network for inter-container communication.
  • Publish: Maps container ports to host ports for external access.

Scope

  • Expose: Limited to the container network.
  • Publish: Allows access from outside the container network.

Configuration

  • Expose: Defined in Dockerfile using the EXPOSE directive.
  • Publish: Defined at runtime with -p or --publish option.

Visibility

  • Expose: Ports are visible to other containers in the same network.
  • Publish: Ports can be accessed from the host machine and external systems.


The syntax to EXPOSE the ports by specifying a protocol is:

 EXPOSE <port>/<protocol>

In this article, we are going to discuss some practical examples of how to use EXPOSE instruction in your Dockerfile and overriding it using the publish flag while starting a Docker Container.

Docker Expose Port

Docker "expose" port is a way to specify which ports within a Docker container should be accessible to other containers or the host system. It does not actually publish the ports to the host system or make them accessible outside the container. Instead, it serves as a documentation mechanism to indicate which ports are intended to be used for communication between Docker containers.

  • EXPOSE 80/tcp
  • EXPOSE 80/udp

Publishing and EXPOSE Docker Ports

Follow the below steps to implement the EXPOSE instruction in a docker container:

Step 1: Exposing Ports

Exposing Ports in Dockerfile: Ports can be exposed within a Dockerfile using the EXPOSE instruction. This instruction informs Docker that the container listens on the specified network ports at runtime. However, it does not actually publish the port

Let's create a Dockerfile with two EXPOSE Instructions, one with TCP protocol and the other with UDP protocol.

Example:

FROM ubuntu:latest
EXPOSE 80/tcp
EXPOSE 80/udp

Step 2: Build the Docker Image with Expose ommand

To build the Docker Image using the above Dockerfile, you can use the Docker Build command.

sudo docker build -t expose-demo
Building Image

Step 3: Running the Docker Container

To run the Docker Container, you can use the Docker run command.

sudo docker run -it expose-demo bash
Running Docker

Step 4: Verify the Ports

To verify the ports exposed, you can use the Docker inspect command.

sudo docker image inspect --format='' expose-demo 
Port Verification

In the above screenshot, you can see the ExposedPorts object contains two exposed ports that we have specified in the Dockerfile.

Step 5: Publish ports when creating a Docker container

To publish all the exposed ports, you can use the -p flag.

sudo docker run -P -d expose-demo
Using EXPOSE Instruction

Step 6: Viewing the exposed ports

You can just the list containers to check the published ports using the following command. But make sure that the Container is running.

sudo docker start <container-id>
sudo docker container ls
Published Ports

Docker Command to Expose Multiple Ports

To expose multiple ports in a Dockerfile, you can use the EXPOSE instruction followed by the port numbers and their corresponding protocols. Here's an example:

FROM ubuntu:latest
EXPOSE 80/tcp
EXPOSE 80/udp

This Dockerfile starts with the ubuntu:latest base image. It then exposes two ports, 80/tcp and 80/udp, using the EXPOSE instruction.

  • EXPOSE 80/tcp: This instruction exposes port 80 on the container for TCP traffic.
  • EXPOSE 80/udp: This instruction exposes port 80 on the container for UDP traffic.
Comment