When using Docker in a production environment, data often needs to be persisted or shared between multiple containers, which inevitably involves container data management operations. There are two main ways to manage data in containers: 1. Data Volumes: Data in the container is directly mapped to the local host environment; how to create a data volume in the container and mount the local directory or file to the data volume in the container. Data Volume A data volume is a special directory available for use by containers. It maps the host operating system directory directly into the container, similar to the mount operation in Linux. Data volumes can provide many useful features, as follows: 1. Create a data volume in the container When using the docker run command, use the -v flag to create a data volume in the container. Repeat the -v flag multiple times to create multiple volumes. Next, create a web container using the training/webapp image and create a data volume mounted to the /webapp directory of the container: $ docker run -d -P --name web -v /webapp training/webapp python app.py -P is the port that exposes the container service, which is automatically mapped to a temporary port on the local host. 2. Mount a host directory as a data volume You can also use the -v flag to specify an existing local directory to be mounted into the container as a data volume (recommended). $ docker run -d -P --name web -v /src/webapp:/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 feature is very convenient when testing. For example, users can put some programs or data in a local directory and then run and use them in the container. In addition, the path to the local directory must be an absolute path. If the directory does not exist, Docker will automatically create it. The default permissions for Docker mounted volumes are read-write (rw), and users can also specify read-only via ro: $ docker run -d -P --name web -v /src/webapp:/opt/webapp:ro training/webapp python app.py After adding :ro, the data in the mounted data volume cannot be modified in the container. 3. Mount a local host file as a data volume The -v flag can also be used to mount individual files from the host into the container as data volumes (not recommended). $ docker run --rm -it -v ~/.bash_history:/.bash_history ubuntu /bin/bash This will record the command history entered in the container. If you mount a file directly into a container, using file editing tools, including vi or sed --in-place, may cause the file inode to change. Starting with Docker 1.1.0, this will result in an error message. So the recommended way is to directly mount the directory where the file is located. Data volume container If users need to share some continuously updated data between multiple containers, the easiest way is to use a data volume container. A data volume container is also a container, but its purpose is to provide data volumes for other containers to mount. First, create a data volume container dbdata, and create a data volume in it and mount it to /dbdata: $ docker run -it -v /dbdata --name dbdata ubuntu root@3ed94f279b6f:/# View the /dbdata directory: root@3ed94f279b6f:/# ls bin boot dbdata dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var You can then use --volumes-from in other containers to mount the data volumes in the dbdata container. For example, create two containers, db1 and db2, and mount the data volume from the dbdata container: $ docker run -it --volumes-from dbdata --name db1 ubuntu $ docker run -it --volumes-from dbdata --name db2 ubuntu At this point, containers db1 and db2 both mount the same data volume to the same /dbdata directory. Any writes made by any of the three containers to this directory can be seen by the other containers. For example, create a test file in the dbdata container as follows: root@3ed94f279b6f:/# cd /dbdata root@3ed94f279b6f:/dbdata# touch test root@3ed94f279b6f:/dbdata# ls test View it inside the db1 container: $ docker run -it --volumes-from dbdata --name db1 ubuntu root@4128d2d804b4:/# ls bin boot dbdata dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@4128d2d804b4:/# ls dbdata/ test You can use the --volumes-from parameter multiple times to mount multiple volumes from multiple containers. You can also mount data volumes from other containers that have already mounted container volumes. The container that mounts the data volume using the --volumes-from parameter does not need to be running. If the mounted container (including dbdata, db1, and db2) is deleted, the data volume will not be automatically deleted. If you want to delete a data volume, you must explicitly use the docker rm -v command when deleting the last container that still mounts it to specify that the associated container should be deleted at the same time. Migrating data using data volume containers The data volume container can be used to back up and restore the data volumes therein to achieve data migration. These two operations are described below. 1. Backup Use the following command to back up the data volumes in the dbdata data volume container: Copy the code as follows: $ docker run --volumes-from dbdata -v $(pwd):/backup --name worker ubuntu tar cvf /backup/backup.tar /dbdata First, create a container worker using the ubuntu image. Use the --volumes-from dbdata parameter to let the worker container mount the data volume of the dbdata container (that is, the dbdata data volume), and use the -v $(pwd):/backup parameter to mount the local current directory to the /backup directory of the worker container. After the worker container is started, the tar cvf /backup/backup.tar /dbdata command is used to back up the contents of /dbdata to /backup/backup.tar in the container, that is, backup.tar in the current directory of the host. 2. Recovery If you want to restore data to a container, you can follow the steps below. First create a container dbdata2 with a data volume: $ docker run -v /dbdata --name dbdata2 ubuntu /bin/bash Then create another new container, mount the dbdata2 container, and use untar to decompress the backup file into the mounted container volume: $ docker run --volumes-from dbdata2 -v $(pwd):/backup --name worker ubuntu bash cd /dbdata tar xvf /backup/backup.tar The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: mysql method to view the currently used configuration file my.cnf (recommended)
>>: Detailed explanation of several ways to create objects and object methods in js
Recently, due to business adjustments in the comp...
1. A container is an independently running applic...
Preface : Today I was asked, "Have you carefu...
Table of contents history pushState() Method push...
Use anti-shake to make DIV disappear when the mou...
The previous article introduced two methods to ch...
Migration is unavoidable in many cases. Hardware ...
Table of contents vue custom directive Global Dir...
In the previous article, we explained how nginx r...
Preface: The importance of database backup is sel...
Docker is an open source project that provides an...
Table of contents Overview Getting started with d...
The <tbody> tag is used to define the style...
Table of contents JS obtains the .txt file conten...
Earlier, we used Docker to simply deploy the Spri...