Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Networking Exposing Ports EXPOSE in Dockerfile publish -p publishall
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-networking-exposing-ports-expose-in-dockerfile-publish-p-publishall

Docker Docker Networking Exposing Ports EXPOSE in Dockerfile publish -p publishall

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Expose a container's port to the host machine, allowing external access. EXPOSE in Dockerfile.

2. Step-by-Step

  1. Create a new Dockerfile with a single line: EXPOSE 80
  2. Build the Docker image with docker build -t my-web-server .
  3. Run the container with docker run -d --name my-web-server -p 8080:80 my-web-server
  4. Check the container's port mapping with docker port my-web-server 80
  5. ⚠️ Stop the container with docker stop my-web-server and remove it with docker rm my-web-server

3. Real Commands with Examples


Example 1: Exposing a single port

  • Purpose: Expose port 80 from the container to port 8080 on the host.
  • Command: docker run -d --name my-web-server -p 8080:80 my-web-server
  • Brief explanation of flags: -p publishes a container port to the host.

Example 2: Exposing multiple ports

  • Purpose: Expose ports 80 and 443 from the container to ports 8080 and 8443 on the host.
  • Command: docker run -d --name my-web-server -p 8080:80 -p 8443:443 my-web-server
  • Brief explanation of flags: -p publishes a container port to the host, multiple flags are used for multiple ports.

Example 3: Exposing all ports

  • Purpose: Expose all ports from the container to the host.
  • Command: docker run -d --name my-web-server --publish-all my-web-server
  • Brief explanation of flags: --publish-all publishes all container ports to the host.

4. Common Errors


Error 1: Port not exposed

  • Error message or symptom: docker: Error response from daemon: Cannot start container ...: ...: Port is already allocated.
  • Why it happens: The port is already in use by another container or process.
  • One-line fix: docker stop <container_name> and then docker rm <container_name>.

Error 2: Port mapping not correct

  • Error message or symptom: docker: Error response from daemon: Cannot start container ...: ...: Port 8080 is already mapped to another container.
  • Why it happens: The port is already mapped to another container.
  • One-line fix: docker stop <container_name> and then docker rm <container_name>.

Error 3: Port not accessible

  • Error message or symptom: curl: (7) Failed to connect to localhost port 8080: Connection refused
  • Why it happens: The container is not running or the port is not exposed.
  • One-line fix: docker start <container_name>.

5. Quick Checks


Scenario 1: Container running and port exposed

  • Situation: Container is running and port 80 is exposed.
  • Command to run: docker ps
  • What a correct response looks like: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ... my-web-server "nginx -g 'daemon ..." 5 seconds ago Up 5 seconds (healthy) 0.0.0.0:8080->80/tcp my-web-server

Scenario 2: Container not running

  • Situation: Container is not running.
  • Command to run: docker ps -a
  • What a correct response looks like: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ... my-web-server "nginx -g 'daemon ..." 10 minutes ago Exited (1) 10 minutes ago my-web-server

Scenario 3: Port not exposed

  • Situation: Port 80 is not exposed.
  • Command to run: docker port my-web-server 80
  • What a correct response looks like: docker: Error response from daemon: Cannot access a non-existent port: 80

6. Last-Minute Reference

  • EXPOSE in Dockerfile: specifies the port to expose.
  • -p flag: publishes a container port to the host.
  • --publish-all flag: publishes all container ports to the host.
  • docker port: checks the port mapping of a container.
  • docker ps: lists running containers.
  • docker ps -a: lists all containers.
  • docker stop: stops a container.
  • docker rm: removes a container.
  • docker start: starts a container.
  • docker build: builds a Docker image.
  • docker run: runs a Docker container.
  • ⚠️ docker rm -f: removes a container and its associated volumes.
  • ⚠️ docker system prune: removes all stopped containers, dangling images, and unused networks.


ADVERTISEMENT