Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify Port Mapping after docker Container starts

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Many novices are not very clear about how to modify the port mapping after the docker container starts. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

How to modify the port mapping after the docker container starts?

When docker run creates and runs the container, you can specify the port mapping rules with-p. However, there are also situations where you forget to set the port mapping at first or set it incorrectly and need to be modified. When docker start runs the container, it does not provide a-p option or setting for you to modify the specified port mapping rules.

The usual indirect approach is to save the image, create a new container, and specify the new port mapping at creation time.

Method 1: delete the original container and rebuild the new container

The simplest solution is to delete the original container and build a new one. Of course, don't forget to add port mapping this time. The advantage is that it is simple and fast, and it is widely used in the test environment. The disadvantage is that if it is database mirroring, it will be troublesome to build a new one and reconfigure it again.

Method 2: use the new mirror image of docker commit

Docker commit: commit the file changes and configuration information of a container to a new image. This will be very useful when testing, import all the container file changes and configuration information into a new docker image, and then restart a container with this new image, which will have no effect on the previous container.

1. Stop the docker container

Docker stop container01

2. Commit the docker container

Docker commit container01 new_image:tag

3. Restart a container with the newly generated image in the previous step

Docker run-- name container02-p 80:80 new_image:tag

The advantage of this approach is that it will not affect other containers on the unified host, and the disadvantage is that it is messy to manage.

Method 3: modify the file port and restart the docker service

1. Stop the container (docker stop d00254ce3af7)

2. Stop the docker service (systemctl stop docker)

3. Modify the port in the hostconfig.json file of this container (if the port is also recorded in the config.v2.json, modify it, too)

Cd / var/lib/docker/containers/d00254ce3af7* # if there is no port mapping before, there should be a paragraph like this: "PortBindings": {} add a mapping to write: "PortBindings": {"8080/tcp": [{"HostIp": "HostPort": "60000"}]} the first number is the container port and the second is the host port. It is easier to map port 60000 of the host to port 8080 of the container and modify the existing port mapping. Just change the port number.

4. Start the docker service (systemctl start docker)

5. Launch the container (docker start d00254ce3af7)

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report