(7) Docker data management

Docker data management

This chapter describes how to manage data within Docker and between containers. There are two main ways to manage data in containers:

Volumes

Mount the host directory (Bind mounts)

data volume

A data volume is a special directory available to one or more containers that bypasses UFS and provides many useful features:

Data volumes can be shared and reused between containers

Modifications to the data volume will take effect immediately

Updates to data volumes do not affect mirroring

The data volume will always exist by default, even if the container is deleted

Note: The use of data volumes is similar to mounting a directory or file under Linux. The files in the directory designated as the mount point in the mirror will be hidden, and the mounted data volume can be displayed.

Create a data volume

$ docker volume create my-vol

View all data volumes

$ docker volume ls

local               my-vol

Use the following command on the host to view information about the specified data volume

$ docker volume inspect my-vol
[
    {
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/my-vol/_data",
        "Name": "my-vol",
        "Options": {},
        "Scope": "local"
    }
]

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

Let's create a container named web and load a data volume into the container's /webapp directory.

$ docker run -d -P \
    --name web \
    # -v my-vol:/wepapp \
    --mount source=my-vol,target=/webapp \
    training/webapp \
    python app.py

View the specific information of the data volume Use the following command
on the host to view the information of the web container

$ docker inspect web

The data volume information is under the "Mounts" Key

"Mounts": [
    {
        "Type": "volume",
        "Name": "my-vol",
        "Source": "/var/lib/docker/volumes/my-vol/_data",
        "Destination": "/app",
        "Driver": "local",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
],

delete data volume

$ docker volume rm my-vol

Data volumes are designed to persist data, their life cycle is independent of containers, Docker does not automatically delete data volumes after containers are deleted, and there is no mechanism for garbage collection to process data that is not referenced by any container roll. If you need to remove the data volume while deleting the container. You can use docker rm -vthis .

Unowned data volumes can take up a lot of space, to clean up use the following command

$ docker volume prune

mount host directory

Choose between -v or ---mount parameter

New Docker users should choose the --mount parameter. Experienced Docker users are already familiar with -v or --volume, but the --mount parameter is recommended.

Mount a host directory as a data volume

Use the --mount flag to specify to mount a localhost directory into the container.

$ docker run -d -P \
    --name web \
    # -v /src/webapp:/opt/webapp \
    --mount type=bind,source=/src/webapp,target=/opt/webapp \
    training/webapp \
    python app.py

The above command loads the host's /src/webapp directory into the container's /opt/webapp directory. This function is very convenient when testing. For example, users can place some programs in the local directory to check whether the container is working properly. The path of the local directory must be an absolute path. In the past, when using the -v parameter, if the local directory did not exist, Docker would automatically create a folder for you. Now, when using the –mount parameter, if the local directory does not exist, Docker will report an error.

The default permission for Docker to mount the host directory is read-write, and users can also specify read-only by increasing readonly.

$ docker run -d -P \
    --name web \
    # -v /src/webapp:/opt/webapp:ro \
    --mount type=bind,source=/src/webapp,target=/opt/webapp,readonly \
    training/webapp \
    python app.py

After adding readonly, it is mounted as read-only. If you create a new file in the /opt/webapp directory in the container, the following error will be displayed

/opt/webapp # touch new.txt
touch: new.txt: Read-only file system

View the specific information of the data volume Use the following command
on the host to view the information of the web container

$ docker inspect web

The configuration information for mounting the host directory is under the "Mounts" Key

"Mounts": [
    {
        "Type": "bind",
        "Source": "/src/webapp",
        "Destination": "/opt/webapp",
        "Mode": "",
        "RW": true,
        "Propagation": "rprivate"
    }
],

Mount a local host file as a data volume
--mountMarker can also mount a single file from the host into the container

$ docker run --rm -it \
   # -v $HOME/.bash_history:/root/.bash_history \
   --mount type=bind,source=$HOME/.bash_history,target=/root/.bash_history \
   ubuntu:17.10 \
   bash

root@2affd44b4667:/# history
1  ls
2  diskutil list

This will record the commands entered in the container.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324932716&siteId=291194637