×
☰ See All Chapters

Docker Publish Container Ports

By default containers does not publish any of its ports to the outside world. To allow external connections to the container, you have to open (publish) specific ports. You have to add the --publish or -p flag to the docker run command to make a port available to services outside of Docker, or to Docker containers which are not connected to the container’s network This creates a firewall rule which maps a container port to a port on the Docker host to the outside world. Below syntax is used to publish the ports.

docker run IMAGE[:TAG|@DIGEST] -p [host_ip]:[host_port]:[container_port]

Example:

Flag value

Description

-p 8080:80

Map TCP port 80 in the container to port 8080 on the Docker host.

-p 192.168.1.100:8080:80

Map TCP port 80 in the container to port 8080 on the Docker host for connections to host IP 192.168.1.100.

-p 8080:80/udp

Map UDP port 80 in the container to port 8080 on the Docker host.

-p 8080:80/tcp -p 8080:80/udp

Map TCP port 80 in the container to TCP port 8080 on the Docker host, and map UDP port 80 in the container to UDP port 8080 on the Docker host.

docker-publish-container-ports-0
 

All Chapters
Author