0
0
Dockerdevops~30 mins

Port mapping in Compose in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Port mapping in Compose
📖 Scenario: You are setting up a simple web application using Docker Compose. The application runs inside a container and listens on port 80. To access it from your computer's browser, you need to map the container's port 80 to a port on your computer.
🎯 Goal: Learn how to write a Docker Compose file that maps a container port to a host port so you can access the application from your browser.
📋 What You'll Learn
Create a Docker Compose file named docker-compose.yml
Define a service called webapp using the nginx image
Map container port 80 to host port 8080 using port mapping
💡 Why This Matters
🌍 Real World
Port mapping is essential to access applications running inside containers from your computer or other devices on the network.
💼 Career
Understanding Docker Compose port mapping helps in deploying and managing containerized applications in development and production environments.
Progress0 / 4 steps
1
Create the basic Docker Compose file
Create a file named docker-compose.yml and write the starting lines to define the version as '3.8' and an empty services section.
Docker
Need a hint?

Start with version: '3.8' on the first line, then add services: below it.

2
Add the webapp service with nginx image
Under the services section, add a service named webapp that uses the nginx image.
Docker
Need a hint?

Indent the webapp service under services: and specify image: nginx under it.

3
Add port mapping to expose container port 80 on host port 8080
Inside the webapp service, add a ports section that maps host port 8080 to container port 80.
Docker
Need a hint?

Use ports: followed by - "8080:80" indented properly under webapp.

4
Run Docker Compose and check port mapping
Run docker compose up -d in your terminal to start the container. Then run docker compose ps and write the output showing the port mapping line for the webapp service.
Docker
Need a hint?

After starting the container, docker compose ps shows the port mapping like 0.0.0.0:8080->80/tcp.