06. Manage Docker container data

Table of contents

1 Introduction

2. How Docker implements data management

2.1. Data Volumes

2.2. Data Volume Containers

3. Simple example

3.1. Example of data volume

3.2. Example of data volume container


1 Introduction

When using Docker in a production environment, on the one hand, it is necessary to save data or share data among multiple containers; on the other hand, after the Docker container is deleted, the state information of the container will not be retained. So how to achieve persistence of information? This necessarily involves data management for containers.

2. How Docker implements data management

There are two main ways to implement data management (or data persistence) in a Docker container:

2.1. Data Volumes

A data volume is essentially a mounted directory, similar to a directory mounted using the Linux mount command. Data volumes can be used by containers, and data volumes can be shared and reused between different containers. Modifications to data volumes take effect immediately. The data volume and the container are independent of each other, and updates to the data volume will not affect the image.

Even if the container is deleted, the data volume will always exist by default until the data volume is deleted.

In Docker, you can use -mount and -v to mount data volumes to containers.

2.2. Data Volume Containers

A data volume container is a special container used to maintain data volumes. It can share data information among multiple containers. Data migration can be easily completed by using the data volume container.

3. Simple example

3.1. Example of data volume

You can use -mount or -v to mount the container on the data volume.

The difference between -mount and -v is that -mount will directly report an error if the host directory does not exist, while -v will automatically create it.

1) Create a data volume "myvolume".

docker volume create myvolume

After creation, view all data volumes.

docker volume ls

You can also use the inspect command to view detailed information about data volumes.

docker inspect myvolume

2) Start a container and use the data volume.

Here use the Nginx image to create a container named mynginx and port mapping 1234:80.

docker run -d -p 1234:80 --name=mynginx --mount type=volume,source=myvolume,target=/usr/share/nginx/html/ nginx

However, I am prompted here that --mount cannot be recognized, and the --mount command was not found through docker run --help. Guess it has something to do with the docker version. I am using docker 1.13.0 version here. After searching around the Internet, I found that docker-ce 75.06 or higher is required. Here you can verify for yourself.

Parameter Description:

  1. --mount : Specifies to mount the data volume when the container starts.
  2. type: Specifies the method of mounting the data volume. There are the following parameters:
    1. volume. Ordinary data volume, the default type. Its functions are mapped to the host "ar/lib/docker/volumes" directory.
    2. bind. Bind data volumes. Use this type to map the data volume to the specified directory of the host when mounting it.
    3. tmpfs. Temporary data volume, only mount the directory of the container to the memory of the host machine. Generally, this method is not used in the actual environment.
  3. source: Specifies the directory or data volume on the host. The data volume myvolume created in step (1) is used here.
  4. target: Mount the "/usr/share/nginx/htm/" directory in the container to the host.

3) You can also use the -v parameter to mount.

Using -v we mount the host /home/data directory to the /data/mydatavolume directory in the container.

# --privileged=true 不加这个,容器内使用ls会提示权限问题
docker run -it --privileged=true -v /home/data/:/data/mydatavolume centos /bin/bash

Inside the container:

Host:

You can see that the directory has been successfully mounted, and when you modify the content on the host, it will be directly synchronized to the inside of the container.

The -v command format is:

-v 宿主机目录:容器内部目录

3.2. Example of data volume container

The data volume container is also a container, which is specially used to provide data volumes for other containers to mount. If users need to share some continuously updated data among multiple containers, the easiest way is to use data volume containers.

1) Create a data volume container.

Create a data volume container dbdata, and create a data volume in it to mount to "/dbdata":

docker run -it -v /dbdata --name dbdata centos

2) Generate some files in the data volume container.

echo hello world > a.txt

3) Create a container container1 and use --volumes-from to mount the data volume of the dbdata container.

docker run -it --volumes-from dbdata --name=container1 centos

In the container container1, you can view the directory /dbdata, which already has the newly generated a.txt:

4) Similarly, you can continue to build the container container2, and use --volumes-from to mount the data volume of the dbdata container.

docker run -it --volumes-from dbdata --name=container2 centos

and create b.txt:

5) The data can also be seen in the container container1.

It can be seen from the results that the two containers container1 and container2 mount the same data volume, and the data volumes are all in the same dbdata directory. In this way, any party of the container can write in this directory, and other containers can also see it. In this way, data sharing between different containers is conveniently realized, and container data migration is easily realized by using this method.

Guess you like

Origin blog.csdn.net/p793049488/article/details/132003525