Port Mapping

DockerBeginner
Practice Now

Introduction

In containers, isolation is a key feature. By default, a container's network is separate from the host system. This means a web server running inside the container may be healthy, but still unreachable from outside unless you deliberately publish a port.

Port mapping bridges that gap. It forwards traffic from a chosen port on the host to a chosen port inside the container. This is one of the most important container networking basics because it explains how isolated services become reachable.

Without that publishing step, a service may be perfectly healthy inside the container but effectively private to the container's own network namespace.

In this lab, you will practice:

  • Running an Nginx web server container.
  • Mapping the host's port 8080 to the container's port 80.
  • Verifying the mapping and accessing the service.
  • Cleaning up the resources.

Expose the Container Port

In this step, you will start a new Docker container using the Nginx image.

Nginx is a web server that, by default, listens on port 80 inside the container. However, since the container is isolated, port 80 is not automatically accessible from your LabEx environment. To fix this, you will map port 8080 on your host machine to port 80 inside the container.

The syntax for port mapping is -p <host_port>:<container_port>.

This order matters. The left side is the host port clients connect to, and the right side is the port the application is actually listening on inside the container.

Run the following command in your terminal:

docker run -d -p 8080:80 --name my-nginx nginx

Breaking down the command:

  • docker run: The command to create and start a container.
  • -d: Runs the container in detached mode (background).
  • -p 8080:80: The port mapping flag.
    • 8080: The port on your host machine (LabEx environment).
    • 80: The port inside the container (where Nginx is listening).
  • --name my-nginx: Names the container "my-nginx".
  • nginx: The image to use.

If successful, Docker will print the long Container ID.

At this point, Docker has not changed Nginx itself. It has created a networking rule so requests arriving on the host port are forwarded into the container's service port.

Check Port Configuration

Now you need to verify that the container is running and the port mapping is correctly configured. The docker ps command provides this information.

Run the following command:

docker ps

You should see output similar to this:

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                  NAMES
abc123456789   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 10 seconds   0.0.0.0:8080->80/tcp   my-nginx

Understanding the Output:

Look closely at the PORTS column: 0.0.0.0:8080->80/tcp.

  • 0.0.0.0:8080: This means the host is listening on port 8080 on all network interfaces.
  • ->: Indicates the forwarding direction.
  • 80/tcp: This is the destination port inside the container.

This confirms that any traffic sent to port 8080 on your machine will be forwarded to port 80 inside the container.

That forwarding is why port mapping is often described as "publishing" a container port. You are exposing an otherwise isolated service to the host or external clients.

You can also use a specific command to check just the ports:

docker port my-nginx

Test the Service

Now that the port is mapped, let's test if the web server is reachable. Since you mapped the host's port 8080, you can access the Nginx server by sending a request to localhost:8080.

We will use curl, a command-line tool for making web requests.

Run the following command:

curl localhost:8080

What to expect:

You should see the raw HTML code of the default Nginx welcome page.

<!DOCTYPE html>
<html>
  <head>
    <title>Welcome to nginx!</title>
...
    <h1>Welcome to nginx!</h1>
...
</html>

If you see this HTML output, congratulations! Your request traveled from the host (port 8080) -> into the container (port 80) -> was processed by Nginx -> and the response was sent back.

That round trip is the practical proof that networking between host and container is working, not just that the container process happens to be running.

Clean Up Resources

In this final step, we will clean up the environment. It is good practice to stop and remove containers that are no longer needed to free up ports and system resources.

First, stop the running container:

docker stop my-nginx

Next, remove the stopped container:

docker rm my-nginx

Verify that the container is gone:

docker ps -a

The my-nginx container should no longer appear in the list.

Summary

In this lab, you successfully learned the fundamentals of Docker port mapping. You have:

  1. Launched an Nginx container in detached mode.
  2. Mapped the host's port 8080 to the container's port 80 using the -p flag.
  3. Verified the port configuration using docker ps.
  4. Accessed the service from the host using curl.
  5. Cleaned up the container.

Understanding port mapping is essential for deploying web applications, as it allows external traffic to reach the services running inside your isolated containers.