Docker Port Mapping
Hello. In this tutorial, we will talk about Docker and port mapping in docker.
1. What is Docker?
In the present world, Docker is an important term –
- Often used in CI/CD platform that packages and runs the application with its dependencies inside a container.
- Is a standard for Linux Containers.
- A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux.
1.1 What is Docker used for?
It is used for –
- For environment replication, while the code runs locally on the machine.
- For numerous deployment phases i.e. Dev/Test/QA.
- For version control and distributing the application’s OS within a team.
1.2 Basic Docker terminology?
- Image: Representation of Docker container i.e. a JAR or WAR file in Java.
- Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar.
- Engine: The code that manages, creates, and runs the Docker containers.
- Hub: A public developer’s registry to distribute their code.
- Repository: A collection of Docker-related images i.e. different versions of the same application.
1.3 Setting up Docker
If someone needs to go through the Docker installation, please watch this video.
2. Docker port mapping
One of the key features of Docker is its ability to map container ports to ports on the host machine, which is known as port mapping. Port mapping is essential for allowing external access to containerized applications, as it allows incoming network traffic to be directed to the correct container port. By default, Docker containers are isolated from the host machine and other containers, which means they cannot communicate with each other or with external systems without explicit configuration. To enable port mapping in Docker, developers must specify the desired port mappings when creating or running a container. This is typically done using the -p
or --publish
flag in the docker run
command. The syntax for specifying port mappings is as follows:
Command syntax
docker run -p host_port:container_port image_name
Here, host_port
is the port on the host machine that will be mapped to container_port
, which is the port on the container that will receive incoming traffic. Multiple port mappings can be specified by repeating the -p
flag with different port numbers. Overall, port mapping is a crucial feature of Docker that enables containers to communicate with each other and with external systems. By mapping container ports to ports on the host machine, developers can easily expose containerized applications to the outside world and make them accessible over the network.
2.1 Why do we use port mapping?
We use port mapping in Docker for several reasons:
- Exposing containerized applications to the network: When a container is started, by default it is isolated from the host machine and other containers. To expose a containerized application to the outside world, we need to map the container’s port to a port on the host machine. This allows external systems to connect to the application running inside the container.
- Running multiple containers on the same host: When multiple containers are running on the same host, each container needs to use a unique port number to avoid port conflicts. By using port mapping, we can map each container’s port to a unique port on the host machine, allowing multiple containers to run simultaneously.
- Load balancing and scaling: Port mapping is essential for load balancing and scaling containerized applications. By mapping multiple containers to the same port on the host machine, we can distribute incoming network traffic evenly across all containers, ensuring high availability and scalability.
- Security: Port mapping allows us to control which ports are accessible from the outside world. By only mapping the ports that are necessary for the application to function, we can reduce the attack surface and minimize the risk of security breaches.
Overall, port mapping provides a simple and flexible way to expose containerized applications to the network, scale, and load balance them, and enhance their security.
3. Ways to assign a new port mapping to a running container
There are several ways to assign a new port mapping to a running container in Docker:
3.1 Using the docker update command
Using the docker update
command: This command can be used to modify the configuration of a running container, including its port mapping. For example, to map port 8080 on the host machine to port 80 on a running container with ID “abc123”, the following command can be used:
Example syntax
docker update --publish-add 8080:80 abc123
This will add a new port mapping to the container’s configuration, allowing external systems to connect to the application running inside the container using port 8080 on the host machine.
3.2 Using the docker container stop and start commands
Using the docker container stop
and docker container start
commands: Another way to assign a new port mapping to a running container is to stop the container, modify its port mapping, and then start it again. For example, to map port 8080 on the host machine to port 80 on a running container with ID “abc123”, the following commands can be used:
Example syntax
docker container stop abc123 docker container run --publish 8080:80 --detach abc123
This will stop the container, create a new container instance with the updated port mapping, and start the new instance in detached mode.
3.3 Using a container orchestration platform
Using a container orchestration platform: Container orchestration platforms like Kubernetes and Docker Swarm provide tools for managing containers at scale, including updating their configurations. These platforms allow administrators to modify container configurations, including their port mappings, without stopping or restarting the containers themselves.
4. Relaunch a stopped container
To relaunch a Docker container on a new port, you can follow these steps:
Stop the existing container: First, stop the container that is currently running on the old port. You can use the docker stop
command followed by the container name or ID. For example:
Command syntax
docker stop my_container
Remove the existing container: Next, remove the existing container using the docker rm command followed by the container name or ID. For example:
Command syntax
docker rm my_container
Launch a new container on the new port: Finally, launch a new container on the new port using the docker run command with the appropriate options, including the new port mapping. For example, to launch a new container on port 8080, you can use the following command:
Command syntax
docker run -p 8080:80 my_image
This command will launch a new container based on the my_image image, with port 80 in the container mapped to port 8080 on the host. You can replace my_image with the name or ID of the image you want to use. By following these steps, you can relaunch a Docker container on a new port. Keep in mind that any data or changes made to the container will be lost when the container is stopped and removed, so make sure to back up any important data before proceeding.
That concludes this tutorial, and I hope that it provided you with the information you were seeking. Enjoy your learning journey, and don’t forget to share!
5. Summary
In conclusion, Docker port mapping is a powerful feature that enables containers to communicate with external systems, and to run multiple containers on the same host without port conflicts. By mapping container ports to unique ports on the host machine, developers and administrators can expose containerized applications to the outside world, distribute incoming network traffic evenly across all containers, and control which ports are accessible from the outside world, improving security. There are several ways to assign a new port mapping to a running container, including using the “docker update” command, stopping and restarting the container, or using a container orchestration platform. Overall, Docker port mapping is a critical aspect of containerization, making it possible to run and manage containerized applications at scale. Overall, understanding how to use these Docker commands effectively is crucial for managing containers and ensuring the smooth operation of your applications. You can also download the commands used in this tutorial from the Downloads section.
6. Download the Project
This was a tutorial to understand port mapping in docker.
You can download the full source code of this example here: Docker Port Mapping