How to dynamically modify container port mapping in Docker

How to dynamically modify container port mapping in Docker

Preface: Docker port mapping is often done by mapping the internal port of the container to the specified port of the host machine through -p during the Docker Run command. Generally speaking, the port corresponding to the container port is determined in advance to be mapped. However, in some cases, you have to temporarily map ports. For example, when running a MySQL container in Docker, the default port is not open. So is there any way to expose the specified port in the running container? Please read below--->

Method 1: Change the Docker configuration file (risky)

To achieve our goal, we need to modify the Docker configuration file. Generally speaking, we need to modify the following files: config.v2.json and hostconfig.json. The default path is /var/lib/docker/containers/<容器名稱> .

First, shut down the Docker service through systemctl stop docker . Then modify the ExposedPorts configuration information in the config.v2.json file to add the container internal port, such as "8080/tcp":{}. Then modify the PortBindings configuration information in hostconfig.json as shown below.

After completing the above configuration, restart the Docker service systemctl restart docker , and restart the specified container to access the corresponding port. (This method is risky and is not recommended for frequent use)

 "Config": {
  "ExposedPorts": {
   // Add internal port 5432 mapping "5432/tcp": {},
   "8080/tcp": {}
  },s
  ...
 },

"PortBindings":{
  // Add internal port and external port 15432
  "5432/tcp":[
   {
    "HostIp":"",
    "HostPort":"15432"
   }
  ],
  "8080/tcp":[
   {
    "HostIp":"",
    "HostPort":"28080"
   }
  ]
 },

Method 2: Iptables port forwarding

The principle of Docker's network port mapping is to achieve port forwarding through Iptables. Based on this principle, we can directly use iptables to forward the port to the target container IP. Port forwarding can be achieved by using the following command. This method relies on Iptables rules. In some scenarios, it may cause Iptables rule conflicts and affect the effective startup of the container.

# Port mapping iptables -t nat -A DOCKER -p tcp --dport <container external port> -j DNAT --to-destination <container ip>:<container internal port>
# Cancel the port mapping rule iptables -t nat -D DOCKER -p tcp -d 0/0 --dport <container external port> -j DNAT --to-destination <container ip>:<container internal port>

This is the end of this article about how to dynamically modify container port mapping in Docker. For more information about how to modify container port mapping in Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solution for multiple Docker containers not having the same port number
  • How to modify the port mapping of a running Docker container
  • Perfect solution to the problem of not being able to access the port of the docker container under Windows 10
  • Docker connects to a container through a port
  • Docker dynamically exposes ports to containers
  • Solution to the Docker container being unable to access the host port
  • Docker implements container port binding local port

<<:  Optimizing the slow query of MySQL aggregate statistics data

>>:  JavaScript ES6 Module Detailed Explanation

Recommend

A brief discussion on whether too many MySQL data queries will cause OOM

Table of contents Impact of full table scan on th...

How to embed other web pages in a web page using iframe

How to use iframe: Copy code The code is as follo...

Solution for multiple Docker containers not having the same port number

Background In Docker, four containers are created...

Overview and Introduction to Linux Operating System

Table of contents 1. What is an Operating System ...

How to implement input checkbox to expand the click range

XML/HTML CodeCopy content to clipboard < div s...

Several ways to use require/import keywords to import local images in v-for loop

Table of contents Problem Description Method 1 (b...

Mini Program to implement Token generation and verification

Table of contents process Demo Mini Program Backe...

3 ways to add links to HTML select tags

The first one : Copy code The code is as follows: ...

Complete steps to install mysql5.7 on Mac (with pictures and text)

I recently used a Mac system and was preparing to...

mysql5.7.19 winx64 decompressed version installation and configuration tutorial

Recorded the installation tutorial of mysql 5.7.1...

Let's talk in detail about the difference between unknown and any in TypeScript

Table of contents Preface 1. unknown vs any 2. Th...

Excel export always fails in docker environment

Excel export always fails in the docker environme...

vsftpd virtual user based on MySql authentication

Table of contents 1. MySQL installation 1.2 Creat...