0
0
Dockerdevops~15 mins

Port mapping with -p flag in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Port mapping with -p flag
📖 Scenario: You want to run a web server inside a Docker container and make it accessible from your computer's browser.To do this, you need to map the container's internal port to a port on your computer.
🎯 Goal: Learn how to use the -p flag in the docker run command to map ports from the container to your computer.
📋 What You'll Learn
Create a Docker container running the nginx web server.
Map the container's port 80 to your computer's port 8080 using the -p flag.
Verify the port mapping by running the container and checking the output.
💡 Why This Matters
🌍 Real World
Port mapping is essential when running web servers or services inside containers so you can access them from your computer or network.
💼 Career
Understanding port mapping helps you deploy containerized applications and troubleshoot network access issues in real environments.
Progress0 / 4 steps
1
Create a Docker container running nginx
Write the docker run command to start a container named mynginx using the nginx image without any port mapping.
Docker
Need a hint?

Use docker run --name mynginx -d nginx to start the container in detached mode.

2
Add port mapping with -p flag
Modify the docker run command to map port 8080 on your computer to port 80 inside the container using the -p flag.
Docker
Need a hint?

Use -p 8080:80 to map ports.

3
Run the container with port mapping
Run the docker run command with the -p 8080:80 flag and container name mynginx to start the nginx server with port mapping.
Docker
Need a hint?

Use the same command as before to run the container.

4
Verify port mapping with docker ps
Write the docker ps command to list running containers and show the port mapping for mynginx.
Docker
Need a hint?

Use docker ps to see running containers and their port mappings.