How to modify the port mapping operation of the docker container

Create a docker run in and run the container, you can specify the port mapping rules by -p. However, we often encounter the beginning forget to set the port mapping or set the wrong needs to be modified. When the docker start running container does not provide a -p option or settings let you modify the specified port mapping rules. So this situation how can we deal with it? Today Docker Jun teach you how to modify the port mapping operation of the docker container?

Method One: Remove the original container, to re-build a new container

The most simple solution, delete the original container, a re-built. Of course, do not forget to add the port mapping.

    Advantages and disadvantages: the advantage is simple and quick, more in a test environment. The disadvantage is that if a database mirroring, then rebuild a reconfigured once again, more trouble.

Method Two: Modify the container configuration file, restart the docker services

Container configuration file path:

  1. /var/lib/docker/containers/[hash_of_the_container]/hostconfig.json

    Wherein the hash value is hashofthecontainer docker mirror can be viewed by the docker PS or inspect containername docker. (CONTAINER ID can be seen)

 

    As shown above, in which a document is PortBindings, wherein 8080 / tcp port 8080 corresponding to the interior of the container, the HostPort is mapped to the corresponding port of the host 9190.8361 / tcp corresponds to the interior of the container port 8361, corresponding to the HostPort It is mapped to port 9191 of the host. Modify Ports on Demand, and then restart the docker service, and then start the container service on it.

  1. systemctl restart docker

    Advantages and disadvantages: The advantage of this method is that no side effects, easy to operate. The disadvantage is first to stop docker container, and then restart the entire docker service , if running multiple container services, then on the same host, it will affect other container services.

 

Method three: the use of docker commit the new configuration mirroring

    docker commit: a container of the file and change configuration information to commit a new image. This will be very useful for testing, and all the files and configuration information into the container changes into a new docker mirror, and then use this new image from a heavy container, container prior to this will not have any effect.

1, stop docker container

  1. docker stop container01

2, commit the container docker

  1. docker commit container01 new_image:tag

3, the previous step with a newly generated image from one container to re

  1. docker run --name container02 -p 80:80 new_image:tag

    Advantages and disadvantages: The advantage of this approach is that does not affect other containers on the unity host, the drawback is it seemed more chaotic management, the second method is not so intuitive.

Original: https://www.cnblogs.com/shijf/p/10386193.html

Guess you like

Origin www.cnblogs.com/whitebai/p/12122237.html