How to dynamically add ports to Docker without rebuilding the image

How to dynamically add ports to Docker without rebuilding the image

Sometimes you may need to modify or add exposed ports to a Docker container during operation, but sometimes you don't want to create a new image for the running container. At this time, you can find the storage location of the docker container and then modify the configuration file directly.

Our containers are all stored in /var/lib/docker/containers下.

My container is at /var/lib/docker/containers/797f3d0cb82aec7d1c355c7461b5bc5a050c1c0cbbe5d813ede0edad061e6632

Each container ID corresponds to a hash_id. You can see that the nginx container exposes two ports. Next, let's add another port 9999.

Enter the directory corresponding to the container:

cd /var/lib/docker/containers/797f3d0cb82aec7d1c355c7461b5bc5a050c1c0cbbe5d813ede0edad061e6632

We see:

Copy hostconfig.json and use the json conversion tool to find the "PortBindings" node

Next we will add one on top by following the same pattern.

As shown in the figure:

Modify hostconfig.json and save.

Then modify config.v2.json的ExposedPorts , as shown in the figure:

Save and exit.

Restart Docker,

Restart the container.

As you can see, port 9999 has been exposed.

Summarize

The above is the method that I introduced to you to dynamically add ports to Docker without re-building the image. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to set port mapping for running container in Docker
  • How to achieve network access with Docker port mapping
  • Docker learning notes Docker port mapping
  • Detailed explanation of docker dynamically mapping running container ports
  • Docker port mapping in detail
  • Docker gets the image and reports an error: docker: Error response from daemon

<<:  MySQL Community Server 8.0.11 installation and configuration method graphic tutorial

>>:  A detailed introduction to JavaScript primitive values ​​and wrapper objects

Recommend

Use js to write a simple snake game

This article shares the specific code of a simple...

jQuery implements the function of adding and deleting employee information

This article shares the specific code of jQuery t...

JavaScript Dom implements the principle and example of carousel

If we want to make a carousel, we must first unde...

Detailed explanation of the difference between uniapp and vue

Table of contents 1. Simple page example 2.uni-ap...

Share JS four fun hacker background effect codes

Table of contents Example 1 Example 2 Example 3 E...

The button has a gray border that is ugly. How to remove it?

I used the dialog in closure and drew a dialog wit...

HTML meta usage examples

Example Usage Copy code The code is as follows: &l...

Vue implements the browser-side code scanning function

background Not long ago, I made a function about ...

How to start Vue project with M1 pro chip

Table of contents introduction Install Homebrew I...

HTML Tutorial: Ordered Lists

<br />Original text: http://andymao.com/andy...

MySQL uses custom functions to recursively query parent ID or child ID

background: In MySQL, if there is a limited level...

Detailed process of NTP server configuration under Linux

Table of contents 1. Environment Configuration 1....

jQuery canvas generates a poster with a QR code

This article shares the specific code for using j...

Vue+Vant implements the top search bar

This article example shares the specific code of ...