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

How to map ports in docker containers

Apr 18, 2023 pm 03:21 PM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

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)

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

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 docker How to restart docker Apr 15, 2025 pm 12:06 PM

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).

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

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.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

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

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

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)

Docker Volumes: Managing Persistent Data in Containers Docker Volumes: Managing Persistent Data in Containers Apr 04, 2025 am 12:19 AM

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.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

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".

See all articles