0
0
Dockerdevops~5 mins

Exposing ports to host in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, you run an app inside a container, but you want to use it from your computer or other devices. Exposing ports lets your computer talk to the app inside the container through specific doors called ports.
When you want to access a web server running inside a container from your browser on your computer.
When you run a database in a container and want your local apps to connect to it.
When you develop an API inside a container and need to test it from outside the container.
When you want to share a service running in a container with other devices on your network.
When you need to debug or monitor a containerized app by connecting to its exposed ports.
Commands
This command runs an Nginx web server container in the background and exposes port 80 inside the container to port 8080 on your computer. This means you can open your browser and go to localhost:8080 to see the web page.
Terminal
docker run -d -p 8080:80 nginx
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6a7b8c9d0e1f2
-d - Run the container in detached mode (in the background).
-p - Map a port on the host to a port in the container.
This command lists all running containers so you can check if your Nginx container is running and see the port mapping.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1b2c3d4e5f6 nginx "/docker-entrypoint.…" 10 seconds ago Up 9 seconds 0.0.0.0:8080->80/tcp loving_morse
This command fetches the web page served by Nginx through the exposed port 8080 on your computer, showing that the port mapping works.
Terminal
curl http://localhost:8080
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1>Welcome to nginx!</h1> </body> </html>
Key Concept

If you remember nothing else from this pattern, remember: exposing ports with -p lets your computer talk to apps inside containers through specific ports.

Common Mistakes
Not using the -p flag when running the container.
Without -p, the container's ports are not accessible from your computer, so you cannot reach the app.
Always use -p host_port:container_port to expose the container's port to your computer.
Using the same host port for multiple containers without changing it.
Host ports must be unique; otherwise, Docker will give an error because the port is already in use.
Use different host ports for each container, like -p 8080:80 and -p 8081:80.
Exposing the wrong container port (e.g., -p 8080:8080 when the app listens on 80).
If the container port does not match the app's listening port, the app won't be reachable through the mapped port.
Check the app's internal port and map that port to a host port, like -p 8080:80 for Nginx.
Summary
Use docker run with -p host_port:container_port to expose container ports to your computer.
Check running containers and port mappings with docker ps.
Test access to the containerized app through the exposed host port using curl or a browser.