Bird
Raised Fist0
Dockerdevops~10 mins

Exposing ports to host in Docker - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Exposing ports to host
Start Docker Container
↓
Specify Port Mapping
↓
Docker Maps Container Port to Host Port
↓
Host Can Access Container Service via Host Port
↓
End
This flow shows how Docker maps a container's internal port to a port on the host machine, allowing access from outside the container.
Execution Sample
Docker
docker run -p 8080:80 nginx
Runs an nginx container exposing container port 80 on host port 8080.
Process Table
StepActionContainer PortHost PortResult
1Start container with -p 8080:80808080Docker maps container port 80 to host port 8080
2Container runs nginx server on port 80808080nginx listens inside container on port 80
3Host accesses localhost:8080808080Request forwarded to container port 80, nginx responds
4Stop container--Port mapping removed, host cannot access container service
💡 Container stopped, port mapping no longer active
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Container Port-808080-
Host Port-808080808080-
Container RunningNoYesYesYesNo
Host AccessNoNoNoYesNo
Key Moments - 3 Insights
Why do we write -p 8080:80 instead of just -p 80?
Because -p 8080:80 tells Docker to map container port 80 to host port 8080. Without specifying host port, Docker assigns a random port, making it hard to access predictably. See execution_table step 1.
Does exposing a port mean the container service is accessible from outside the host machine?
Not necessarily. Exposing ports maps container ports to host ports, but external access depends on host firewall and network settings. The execution_table shows host access at localhost:8080, which is local to the host.
What happens if the container stops running?
Port mapping stops and host cannot access the container service anymore. See execution_table step 4 where container stops and port mapping is removed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the host start being able to access the container service?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Host Access' variable in variable_tracker after each step.
If we change the command to 'docker run -p 9090:80 nginx', what will be the host port in the execution table?
A80
B9090
C8080
DRandom port
💡 Hint
The host port is the first number in -p hostPort:containerPort mapping, see execution_table step 1.
According to the variable_tracker, what is the state of 'Container Running' after step 4?
ANo
BYes
CUnknown
DDepends on host
💡 Hint
Look at the 'Container Running' row in variable_tracker under 'Final' column.
Concept Snapshot
Docker port exposing syntax: docker run -p hostPort:containerPort image
Maps container's internal port to a port on the host.
Host accesses container service via hostPort.
Port mapping ends when container stops.
Useful for accessing web servers or APIs inside containers.
Full Transcript
This visual execution shows how Docker exposes container ports to the host machine. We start a container with the command 'docker run -p 8080:80 nginx' which maps container port 80 to host port 8080. The container runs nginx listening on port 80 internally. When the host accesses localhost:8080, the request is forwarded to the container's port 80 and nginx responds. When the container stops, the port mapping is removed and the host can no longer access the service. Variables like container port, host port, container running state, and host access change step by step. Key points include specifying both host and container ports in -p, understanding local host access, and that port mapping ends when the container stops.

Practice

(1/5)
1.

What does the Docker option -p 8080:80 do when running a container?

easy
A. It maps port 8080 inside the container to port 80 on the host machine.
B. It maps port 80 inside the container to port 8080 on the host machine.
C. It exposes port 8080 inside the container without mapping to the host.
D. It blocks port 80 on the host machine.

Solution

  1. Step 1: Understand Docker port mapping syntax

    The syntax -p host_port:container_port means the host port is on the left and container port is on the right.
  2. Step 2: Apply to given ports

    Given -p 8080:80, port 80 inside the container is accessible via port 8080 on the host.
  3. Final Answer:

    It maps port 80 inside the container to port 8080 on the host machine. -> Option B
  4. Quick Check:

    host_port:container_port = 8080:80 [OK]
Hint: Remember host port is before colon, container port after [OK]
Common Mistakes:
  • Confusing host and container ports order
  • Thinking ports are blocked instead of mapped
  • Assuming ports are exposed but not mapped
2.

Which of the following is the correct Docker command to expose container port 5000 to host port 3000?

docker run ... myapp
easy
A. docker run -p 3000:5000 myapp
B. docker run -p 5000 myapp
C. docker run --expose 3000:5000 myapp
D. docker run -p 5000:3000 myapp

Solution

  1. Step 1: Recall port mapping syntax

    The correct syntax is -p host_port:container_port.
  2. Step 2: Match ports to command

    To expose container port 5000 on host port 3000, use -p 3000:5000.
  3. Final Answer:

    docker run -p 3000:5000 myapp -> Option A
  4. Quick Check:

    host_port:container_port = 3000:5000 [OK]
Hint: Host port is always before colon in -p option [OK]
Common Mistakes:
  • Swapping host and container ports
  • Using --expose with port mapping syntax
  • Omitting container port in -p option
3.

What will be the output when running this command and then accessing http://localhost:8080?

docker run -d -p 8080:80 nginx
medium
A. Docker will throw a syntax error and not run.
B. Connection refused error because port 80 is not exposed.
C. The container will run but port 8080 will be closed.
D. You will see the default Nginx welcome page in your browser.

Solution

  1. Step 1: Understand the command

    The command runs Nginx container detached with port 80 inside mapped to host port 8080.
  2. Step 2: Accessing localhost:8080

    Since port 8080 on host maps to container's port 80 where Nginx serves, accessing localhost:8080 shows Nginx default page.
  3. Final Answer:

    You will see the default Nginx welcome page in your browser. -> Option D
  4. Quick Check:

    Port mapping works, Nginx serves on port 80 [OK]
Hint: Map container port 80 to host to access web server [OK]
Common Mistakes:
  • Assuming port 80 is not exposed automatically
  • Expecting syntax error from correct command
  • Thinking container runs but ports are closed
4.

Identify the error in this Docker command that tries to expose port 3306:

docker run -p 3306: mysql
medium
A. Missing container port in the -p option.
B. Port 3306 is not allowed to be exposed.
C. The image name 'mysql' is incorrect.
D. The command should use --expose instead of -p.

Solution

  1. Step 1: Check -p option syntax

    The -p option requires both host and container ports in host_port:container_port format.
  2. Step 2: Analyze given command

    The command uses -p 3306: without specifying container port, which is invalid syntax.
  3. Final Answer:

    Missing container port in the -p option. -> Option A
  4. Quick Check:

    -p requires host_port:container_port [OK]
Hint: Always specify both host and container ports with -p [OK]
Common Mistakes:
  • Using only one port number with -p
  • Confusing --expose and -p options
  • Assuming image name causes port errors
5.

You want to run two containers both exposing port 80 inside, but accessible on host ports 8081 and 8082 respectively. Which Docker commands correctly achieve this?

hard
A. docker run -d -p 8081:8081 app1 && docker run -d -p 8082:8082 app2
B. docker run -d -p 80:8081 app1 && docker run -d -p 80:8082 app2
C. docker run -d -p 8081:80 app1 && docker run -d -p 8082:80 app2
D. docker run -d --expose 8081:80 app1 && docker run -d --expose 8082:80 app2

Solution

  1. Step 1: Understand port mapping for multiple containers

    Each container exposes port 80 internally. To avoid conflict, map to different host ports like 8081 and 8082.
  2. Step 2: Check each option's correctness

    docker run -d -p 8081:80 app1 && docker run -d -p 8082:80 app2 correctly maps host ports 8081 and 8082 to container port 80 for app1 and app2 respectively. Others misuse port order or --expose.
  3. Final Answer:

    docker run -d -p 8081:80 app1 && docker run -d -p 8082:80 app2 -> Option C
  4. Quick Check:

    Host ports differ, container ports 80 mapped correctly [OK]
Hint: Map different host ports to same container port for multiple containers [OK]
Common Mistakes:
  • Swapping host and container ports
  • Using same host port for multiple containers
  • Confusing --expose with port mapping
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard