docker Summary of 3 Ways to Modify Container Mount Directory
- 2021-09-16 08:36:48
- OfStack
Mode 1: Modify the configuration file (docker service needs to be stopped)
1. Stop docker service
systemctl stop docker. service (critical, docker service must be stopped before modification)
2. vim/var/lib/docker/containers/container-ID/config. v2.json
Modify the directory location in the configuration file, and then save to exit
"MountPoints":{"/home":{"Source":"/docker","Destination":"/home","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"//docker/","Target":"/home"}}}
3. Start docker service
systemctl start docker.service
4. Start the docker container
docker start < container-name/ID >
Mode 2: Submit the existing container as a new image and then rerun it
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5a3422adeead ubuntu:14.04 "/bin/bash" About a minute ago Exited (0) About a minute ago agitated_newton
$ docker commit 5a3422adeead newimagename
$ docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash
Then stop the old container and use the new container. If you want the new container to use the old name for some reason, use docker rename after deleting the old container.
Mode 3: export container is the mirror, then import is the new mirror
$docker container export -o ./myimage.docker Container ID
$docker import ./myimage.docker newimagename
$docker run -ti -v "$PWD/dir1":/dir1 -v "$PWD/dir2":/dir2 newimagename /bin/bash
Then stop the old container and use the new container. If you want the new container to use the old name for some reason, use docker rename after deleting the old container.
Additional knowledge: How to restart Docker without losing data, teach you to mount data volume Volume
When you use Docker to deploy web application or mysql database, you will find that when the container is restarted, the log or database data generated during the operation of the container will be emptied, so how do we save these data?
This requires understanding how docker mounts the host disk directory to store data permanently.
1. Execute Docker Volume when creating a container
Using the docker run command, you can run an Docker container, use mirroring ubuntu/nginx, and mount the local directory/tmp/source to the container directory/tmp/destination
docker run -itd --volume /tmp/source:/tmp/destination --name test ubuntu/nginx bash
An Docker container is created based on the ubuntu/nginx image.
Specifies that the name of the container is test, which is specified by the name option.
Docker Volume is specified by the volume (which can be abbreviated to-v) option, and the/tmp/source directory of the host corresponds to the/tmp/destination directory 11 in the container.
2. View Docker Volume
Using the docker inspect command, you can view the details of the Docker container:
docker inspect-format= '{{json. Mounts}}' test python-m json. tool [{"Destination": "/tmp/destination", "Mode": "," Propagation ":", "RW": true, "Source":/tmp/source "," Type ":" bind}]
With the format option, you can optionally view the desired container information. . Mount is the Docker Volume information for the container.
python-m json. tool format the output json string.
Source represents the directory on the host, is/tmp/source.
Destination is the directory in the container, is/tmp/destination.
3. Native files can be synchronized to containers
Create a new hello. txt file in the Native/tmp/source directory
touch /tmp/source/hello.txtls /tmp/source/hello.txt
hello. txt files are visible in the container/tmp/destination/directory
Using the docker exec command, you can execute commands in a container.
docker exectest ls /tmp/destination/hello.txt
Therefore, modifications to the direction/tmp/source/on the host machine can be synchronized to the container direction/tmp/destination/.
4. Container files can be synchronized to the host machine
Create a new world. txt file in the container/tmp/destination directory
docker exec test touch /tmp/destination/world.txtdocker exec test ls /tmp/destination/hello.txtworld.txt
The world. txt file is visible in the host machine/tmp/source/directory
ls /tmp/source/hello.txt world.txt