Port Mapping from Docker Host to container


What is port mapping?
Port mapping in Docker allows services running inside a container to be accessible from the outside world, specifically from the Docker host.
How It Works:
Expose Container Port: A service (like a web server) inside a container listens on a specific port, such as port 80 for HTTP.
Map to Host Port: Docker enables you to map the container’s internal port (e.g., 80) to a port on the Docker host (e.g., 8080). This mapping connects the two, allowing external users to access the service via the host's IP address and specified port.
External Access: Once port mapping is in place, external users can access the service running in the container by connecting to the Docker host’s IP address and port number, such as
http://localhost:8080
.
Example:
docker run -d -p 8080:80 httpd
-p 8080:80
: Maps port 8080 on the Docker host to port 80 inside the container, making the Apache HTTP server inside the container accessible from the host machine athttp://localhost:8080
.
Task: Port Mapping from Docker Host to Container
Now that we've covered the basics, let's work on port mapping with some hands-on tasks. In this exercise, we'll map a port from the Docker host to a container and manage the container's lifecycle.
- Running an HTTPD Container in the Background with Port Mapping
docker run -d -p 80:80 httpd
-d
: Runs the container in the background, allowing you to continue using the terminal.-p 80:80
: Maps port 80 on the Docker host to port 80 inside the container.Result: The Apache HTTPD server inside the container is accessible at
http://localhost:80
while the container continues to run in the background.
- Checking Running Containers
This command displays all containers that are currently running, showing their status, IDs, and names.
docker ps
- Accessing the Running Container with a Secondary Shell
docker exec -it <container id/name> /bin/bash
exit
docker ps
This command opens an interactive shell session inside the running container, allowing you to interact with the container’s environment.
Exit the secondary shell: When you exit, only closes the shell session but does not stop the container
To exit the shell without stopping the container, simply type: exit
- Stopping a Container by Terminating the Main Process
when you run kill 1
inside a container, it terminates the main process, causing the container to stop ("Exited (0)" ). This action automatically exits you from the container's shell and returns you to the Docker host terminal.
Kill 1
Check the container status after stopping it:
docker ps -a
- Gracefully Stopping and Forcefully Killing Containers
To gracefully stop a container:
docker stop <container id/name>
To forcefully terminate a container:
docker kill <containerid/name>
- Cleaning Up: Removing Containers and Images
To remove a stopped container:
docker rm <container id/name>
To list all Docker images stored locally:
docker image ls
To remove a specific image:
docker image rm <image id>
To remove all Docker images in one go:
docker rmi $(docker images -q)
To remove all stopped containers at once:
docker container prune
For bypassing the confirmation prompt, you can use the -f (force) flag:
docker container prune -f
Outcome
By completing this task, you have learned how to map ports between the Docker host and a container. You’ve seen how to run a container in the background with port mapping, allowing external access to services running inside the container. You’ve also learned how to manage containers, including starting and stopping them, accessing them via a secondary shell, and cleaning up resources.
Key Takeaways:
Port Mapping: Connects a container’s internal port to a port on the Docker host, enabling external access.
Running Containers: You can run containers in the background and manage their lifecycle with commands.
Interactive Access: Use
docker exec
to interact with a running container anddocker attach
for direct interaction.Container Management: Commands like
docker stop
,docker kill
, anddocker rm
help you control and clean up containers.
With these skills, you are now equipped to manage Docker containers effectively and make your services accessible from outside the container environment.
Subscribe to my newsletter
Read articles from Sandhya Babu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Sandhya Babu
Sandhya Babu
🌟 Aspiring DevOps Engineer | Cloud & DevOps Enthusiast🌟 Hello! I’m Sandhya Babu, deeply passionate about DevOps and cloud technologies. Currently in my exploring phase, I’m learning something new every day, from tools like Jenkins, Docker, and Kubernetes to the concepts that drive modern tech infrastructures. I have hands-on experience with several Proof of Concept (POC) projects, where I've applied my skills in real-world scenarios. I love writing blogs about what I've learned and sharing my experiences with others, hoping to inspire and connect with fellow learners. With certifications in Azure DevOps and AWS SAA-C03, I’m actively seeking opportunities to apply my knowledge, contribute to exciting projects, and continue growing in the tech industry. Let’s connect and explore the world of DevOps together!