Home > Operation and Maintenance > Docker > How to map ports in docker containers

How to map ports in docker containers

PHPz
Release: 2023-04-18 15:53:15
Original
9560 people have browsed it

Docker is an open source application container engine that allows developers to quickly package, distribute and run any application, whether locally, in the cloud or on a mobile device, using this excellent tool. The biggest feature of Docker is that running containers can be quickly deployed to different hosts and the consistency of the environment is maintained. Due to Docker's excellent performance and ease of use, it has been widely used in cloud computing, DevOps, microservices and other fields. This article will introduce port mapping for Docker containers.

  1. Port mapping of Docker container

Each service in the Docker container needs to occupy a port number. If the service running inside the container needs to communicate with an external system, then this port number needs to be mapped to a port on the host. This process can be called port mapping.

In Docker, you can start a container through the docker run command and specify the port mapping parameters. For example, assuming that a web service is running in the container and the listening port is 8080, we can use the following command to map the 8080 port in the container to the host's 8000 Port:

docker run -p 8000:8080 my-web-service
Copy after login

The parameters of this command -p 8000:8080 means mapping the 8080 port in the container to the host's 8000port. The last parameter my-web-service specifies the Docker image to run.

  1. Multiple port mapping

If the service running in the container needs to expose multiple ports, you can do it by adding the -p parameter multiple times. mapping. For example, assuming that the service running in the container needs to listen to two ports 8080 and 8081 at the same time, we can use the following command to map these two ports to different ports on the host:

docker run -p 8000:8080 -p 8001:8081 my-web-service
Copy after login

The parameters of this command-p 8000:8080 means mapping the 8080 port in the container to the 8000 port of the host, parameter-p 8001:8081 means mapping the 8081 port in the container to the 8001 port on the host.

  1. Bind to local IP

By default, the port mapped by the Docker container is bound to all network card interfaces of the host. If you want to specify mapping to a specific IP address, you can use the -p parameter format:

docker run -p IP:HOST_PORT:CONTAINER_PORT my-web-service
Copy after login

where IP refers to the local IP to be mapped Address, HOST_PORT is the port number on the host, CONTAINER_PORT is the port number that the service in the container actually listens to.

  1. UDP port mapping

Different from TCP ports, UDP ports need to be mapped using the -p parameter format. For example, you can map the UDP port 8080 in the container to the UDP port 8000 of the host through the following command:

docker run -p 8000:8080/udp my-udp-service
Copy after login
  1. Random port mapping

In some cases, if it is not necessary to map the port in the container to the fixed port of the host, you can use the -P parameter to specify random port mapping. For example, you can start and automatically map the service port through the following command:

docker run -P my-web-service
Copy after login

In this command, the parameter -P means to automatically map all ports in the container to random ports on the host. port. Docker will randomly select an unoccupied host port and map the port of the service in the container to it.

  1. View mapped ports

You can use the docker ps command to view running containers and their mapped ports. For example, you can use the following command to list all running containers and their mapped ports:

docker ps --format "table {{.ID}}\t{{.Names}}\t{{.Ports}}"
Copy after login

This command will display the ID, name, and their mapped port number of each container.

Docker port mapping is to create a network interface between the container and the host for bidirectional data transmission. When configuring port mapping, you need to pay attention to network security issues and port conflicts to avoid unnecessary trouble. I hope this article can help readers understand Docker's port mapping and network configuration.

The above is the detailed content of How to map ports in docker containers. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template