How to map ports in docker containers
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.
- 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
The parameters of this command -p 8000:8080
means mapping the 8080
port in the container to the host's 8000
port. The last parameter my-web-service
specifies the Docker image to run.
- 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
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.
- 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
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.
- 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
- 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
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.
- 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}}"
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop <container_name> Command Use docker kill <container_name> command in the host terminal (force exit)

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop <container_id>); start the container (docker start <container_id>); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

DockerVolumes ensures that data remains safe when containers are restarted, deleted, or migrated. 1. Create Volume: dockervolumecreatemydata. 2. Run the container and mount Volume: dockerrun-it-vmydata:/app/dataubuntubash. 3. Advanced usage includes data sharing and backup.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".
