Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 181 of 8023. Docker Orchestration and StorageFree lesson

Exposing Services via Port Mapping

Map container ports to host ports to allow external traffic to reach specific services running within an isolated internal network.

In this lesson, you will learn how to bridge the gap between your isolated containerized services and the outside world using port mapping.

By default, Docker containers run within their own isolated network namespaces. While a service inside a container might be listening on port 80, it is hidden behind the container’s private IP address, inaccessible to your host machine or external users. To make that service reachable, you must create a "port map" that tells Docker to forward traffic from a specific port on your host machine to the corresponding port inside the container.

How Port Mapping Works

Think of your container as an apartment inside a large, secure building (the Docker host). The container has its own internal "room number" (the application port). Without a map, no one from the outside street can reach that specific room. By configuring a port map, you are essentially installing an intercom system that connects a specific "street address" (the host port) to that "room" (the container port).

When you run a container, you use the -p flag to define this connection:

docker run -p 8080:80 my-web-server

In this example, 8080 is the port on your host machine, and 80 is the port the web server is listening on inside the container. If you open your browser and navigate to http://localhost:8080, your request is routed through the host, through the Docker network bridge, and delivered directly to the application running inside the container.

Why This Matters

Port mapping is essential for development and orchestration. It allows you to run multiple containers—even those using the same internal port—simultaneously on a single host. You simply map each one to a different host port (e.g., 8081:80, 8082:80). This mechanism provides the necessary control to expose only the services you intend to make public, while keeping your database or background worker services tucked away in private, unmapped ports.

Key Takeaway: Port mapping acts as a gateway that links a host's entry point to a container's internal service, allowing you to selectively expose isolated applications to external traffic.