0
0
Dockerdevops~30 mins

Exposing ports to host in Docker - Mini Project: Build & Apply

Choose your learning style9 modes available
Exposing Ports to Host with Docker
📖 Scenario: You are working on a web application inside a Docker container. To test it on your computer's browser, you need to make the container's web server accessible through your computer's network.
🎯 Goal: Learn how to expose a port from a Docker container to your host machine so you can access the container's service from your browser.
📋 What You'll Learn
Create a Dockerfile with a simple web server
Define the port the container listens on
Expose the container port to the host machine
Run the container with port mapping
Verify the service is accessible from the host
💡 Why This Matters
🌍 Real World
Developers often run web servers or APIs inside containers and need to access them from their computers or other devices on the network.
💼 Career
Understanding how to expose container ports to the host is essential for testing, debugging, and deploying containerized applications.
Progress0 / 4 steps
1
Create a Dockerfile with a simple web server
Create a file named Dockerfile with these exact lines to set up a basic Python web server listening on port 8000: FROM python:3.12-slim CMD ["python", "-m", "http.server", "8000"]
Docker
Need a hint?

This Dockerfile uses Python's built-in HTTP server on port 8000.

2
Add EXPOSE instruction for port 8000
Add a line EXPOSE 8000 to the Dockerfile to declare that the container listens on port 8000.
Docker
Need a hint?

The EXPOSE instruction tells Docker which port the container will use.

3
Run the container with port mapping
Run the Docker container using the command docker run -d -p 8080:8000 webserver to map container port 8000 to host port 8080. Assume the image is named webserver.
Docker
Need a hint?

The -p flag maps host port 8080 to container port 8000.

4
Verify the web server is accessible on host port 8080
Write the command curl http://localhost:8080 to check if the web server running inside the container is reachable from your host machine.
Docker
Need a hint?

The curl command fetches the web page served by the container.