Docker modify the port mapping and mounting path of the container

Original: Docker modify the port mapping and mounting path of the container

Sometimes it is necessary to modify the configuration of the container for some reasons, such as port mapping and mounting path, but if you delete the container and then re-run, then the previous changes made in the container will be lost, this article provides two methods, you can Modify the configuration parameters while retaining the modifications.

Method 1: Save the container as an image

1. Stop the container

docker stop CONTAINER

2. Save the container as a new image

docker commit CONTAINER NEWIMAGE:TAG

3. Start a new image, at this time you can re-specify parameters

docker run --name CONTAINER OPTIONS NEWIMAGE:TAG

Method two directly modify the container configuration file

1. Obtain the container configuration file path and stop the container

docker inspect CONTAINER
{
"ResolvConfPath": "/var/lib/docker/containers/96b9323df1569e83c9945a9daab253c7c5e6cab35cd4b5eb19b16913e130cd14/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/96b9323df1569e83c9945a9daab253c7c5e6cab35cd4b5eb19b16913e130cd14/hostname",
"HostsPath": "/var/lib/docker/containers/96b9323df1569e83c9945a9daab253c7c5e6cab35cd4b5eb19b16913e130cd14/hosts",
"LogPath": "/var/lib/docker/containers/96b9323df1569e83c9945a9daab253c7c5e6cab35cd4b5eb19b16913e130cd14/96b9323df1569e83c9945a9daab253c7c5e6cab35cd4b5eb19b16913e130cd14-json.log",
}

Intercept part of the output as shown above. Among them, / var / lib / docker / containers / 96b9323df1569e83c9945a9daab253c7c5e6cab35cd4b5eb19b16913e130cd14 / is the configuration folder, and stop the container after recording the path.

docker stop CONTAINER

2. Enter linuxkit

(1) Enter in the terminal:
screen ~/Library/Containers/com.docker.docker/Data/vms/0/tty
Tip:

-Press ctrl + a + k and press y to confirm to kill this screen
-Use ctrl + a + d to temporarily exit this screen, and if you need to enter again, you should use screen -dr, you cannot use screen ~ / Library again /Containers/com.docker.docker/Data/vms/0/tty, this command will create a screen again and cause some inexplicable problems

(2) Enter the configuration folder:
cd /var/lib/docker/containers/96b9323df1569e83c9945a9daab253c7c5e6cab35cd4b5eb19b16913e130cd14/
(3) Modify the configuration file

You need to modify both the hostconfig.json and config.v2.json files in this directory

Modify port mapping

hostconfig.json

{
"PortBindings":{"8080/tcp":[{"HostIp":"0.0.0.0","HostPort":"22"}],
                "9090/tcp":[{"HostIp":"0.0.0.0","HostPort":"9999"}]}
}

config.v2.json

{
"ExposedPorts":{"8080/tcp":{},"9090/tcp":{}}
}
Modify the mount path

hostconfig.json

{
"Binds": ["/data:/mnt/data", "/home/logs:/logs"]
}

config.v2.json

{
"MountPoints":{"/mnt/data":{"Source":"/data","Destination":"/mnt/data","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/data","Target":"/mnt/data"},"SkipMountpointCreation":false},
               "/home/logs":{"Source":"/home/logs","Destination":"/logs","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/home/logs","Target":"/logs"},"SkipMountpointCreation":false}}
}
(4) Restart the docker service

After saving, exit the screen and restart the docker service.
Check if the settings are effective and you can run

docker inspect CONTAINER

Check whether the configuration just took effect in the two keywords "Mounts" and "Ports".

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12690378.html