Docker management container data

Posted by fantasticham on Sat, 19 Feb 2022 19:08:40 +0100

docker manages data in containers in two main ways:

-Data Volumes

-Mount Host Directory (Bind mounts)

Data Volume in order to well realize data saving and data sharing, Docker puts forward the concept of Volume, which simply bypasses the default federated file system and exists on the host in the form of normal files or directories. Also known as data Volume. A data Volume is a special directory that can be used by one or more containers. It bypasses UFS and can provide many useful features:

1. Data volumes can be shared and reused between containers

2. The modification of the data volume will take effect immediately

3. The update of data will not affect the mirrored data volume

4. The default will always exist, even if the container is deleted

5. The purpose of data volume is to persist data, which is independent of the life cycle of the container. Docker will not automatically delete volumes. When you delete a container, it will not "garbage collect" until there is no container to use.

Create data volume

docker volume create my-vol

Or create a database and run it

You can add a data volume by adding the - v parameter when docker run s. The - v parameter can also be used multiple times to mount multiple data volumes.

docker run -d -P --name web -v /webapp training/webapp python app.py

This command will create a data volume to store data in the / webapp folder in the container.

You can also define it in Dockerfile when building an image.

Find the data volume path and locate it with the docker inspect command

docker inspect web

We can see the path of the data volume saved on the host:

...
Mounts": [
    {
        "Name": "fac362...80535",
        "Source": "/var/lib/docker/volumes/fac362...80535/_data",
        "Destination": "/webapp",
        "Driver": "local",
        "Mode": "",
        "RW": true
    }
]

... And see that the permission RW is true

View all data volumes

docker volume ls

Delete a volume

docker volume rm my-vol

The data volume is designed to persist data. Its life cycle is independent of the container. Docker will not automatically delete the data volume after the container is deleted, and there is no garbage collection mechanism to deal with the data volume without any container reference. The unowned data volume may occupy a lot of space, so it should be deleted in time

Mount data volume

When using the docker run command, use the -- mount flag to mount the data volume into the container. Multiple data volumes can be mounted in a docker run.

docker run -itd \
--name Container name \
--mount source=Data volume name,target=Corresponding directory in container \
Image name

It is better to create a startup container through run rather than create/start. After creating a startup container with the create/start command, it is quite troublesome to mount the data volume. Many configuration files need to be modified, But it's not impossible. Docker volume create my volume docker volume LS docker volume inspect my volume docker volume RM my volume docker run - ITD \ - name container name \ - mount source = data volume name, target = corresponding directory \ image name in the container. Adding "\" means to wrap the last carriage return to the comment. The system understands that the command has not ended yet, Therefore, continue to wait for the user to input until the end character is read, such as enter. If the data volume does not exist, docker will be created automatically. If the directory on the container does not exist, docker will be created automatically

Mount directory

docker run -itd \
--name Container name \
--mount type=bind,source=Host Directory,target=Corresponding directory in container \
Image name

Data volume container

If users need to share some continuously updated data among multiple containers, the simplest way is to use the data volume container. The data volume container is also a container, but its purpose is to provide data volumes for other containers to mount.

Create data volume container

docker run -itd --name name -v dir centos

Mount data volume container

docker run -itd --name name --volumes-from db_data centos

Backup, store, and move data volumes

Another very useful feature is the use of data volume containers for backup, storage, and migration operations.

$ docker run --volumes-from dbdata -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /dbdata

Then create a new container

$ docker run -v /dbdata --name dbdata2 ubuntu /bin/bash

Then unzip the data volume and mount it to the container

$ docker run --volumes-from dbdata2 -v $(pwd):/backup ubuntu cd /dbdata && tar xvf /backup/backup.tar

recovery

Then create a new container

$ docker run -v /dbdata --name dbdata2 ubuntu /bin/bash

Create another container, mount the container of dbdata2 , and use , untar , to unzip the backup files into the mounted container volume.

$ sudo docker run --volumes-from dbdata3 -v $(pwd):/backup busybox tar xvf
/backup/backup.tar

Topics: Operation & Maintenance Docker Container