In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
In the process of using Docker in the production environment, it is often necessary to persist the data or share the data among multiple containers, which inevitably involves the data management operation of the container.
There are two main ways to manage data in a container:
1. Data volume (Data Volumes): the data in the container is mapped directly 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.
two。 Data volume containers (Data Volume Containers): use specific containers to maintain data volumes. How to use the data volume container to share data between the container and the host, the container and the container, and realize the backup and recovery of the data.
Data volume
A data volume is a special directory that can be used by the container, which 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. Data volumes can be shared and reused among containers, and data transfer between containers will become efficient and convenient.
two。 Changes to the data in the data volume will take effect immediately, whether in-container or local operations
3. Updates to data volumes do not affect mirroring, decoupling applications and data
4. The volume will remain until there is no container in use, and it can be safely unloaded.
1. Create a data volume within the container
When using the docker run command, use the-v flag to create a data volume within the container. The-v flag can be reused multiple times to create multiple volumes.
Let's use the training/webapp image to create a web container and mount a data volume to the container's / webapp directory:
$docker run-d-P-name web-v / webapp training/webapp python app.py
-P is the port that exposes CCS and is a temporary port that is automatically mapped to the local host.
two。 Mount a host directory as a data volume
Using the-v flag, you can also specify that a local existing directory is mounted to 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, and if the directory does not exist, Docker will automatically create it.
The default permission for Docker to mount data volumes is read and write (rw), and users can also specify it as read-only through ro:
$docker run-d-P-name web-v / src/webapp:/opt/webapp:ro training/webapp python app.py
With the addition of 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 mount a single file from the host to the container as a data volume (not recommended).
$docker run-- rm-it-v ~ / .bash_history:/.bash_history ubuntu / bin/bash
This allows you to record the history of commands entered in the container.
If you mount a file directly to a container, using file editing tools, including vi or sed--in-place, may cause changes to the file inode, starting with Docker 1.1.0, which will result in an error message. So the recommended way is to mount the directory where the file is located.
Data volume container
If users need to share some continuously updated data among multiple containers, the easiest way is to use a data volume container. The data volume container is also a container, but its purpose is to provide data volumes for mounting by other containers.
First, create a data volume container dbdata and create a data volume in it to mount to / dbdata:
$docker run-it-v / dbdata-- name dbdata ubunturoot@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 data volumes 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, the containers db1 and db2 both mount the same data volume to the same / dbdata directory. The writes of either side of the three containers in this directory can be seen by the other containers.
For example, create a test file in the dbdata container as follows:
Root@3ed94f279b6f:/# cd / dbdataroot@3ed94f279b6f:/dbdata# touch testroot@3ed94f279b6f:/dbdata# ls
Test
View it in the db1 container:
$docker run-it-volumes-from dbdata-name db1 ubunturoot@4128d2d804b4:/# lsbin boot dbdata dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr varroot@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 already have container volumes mounted.
The container of the data volume mounted with the-- volumes-from parameter does not need to remain running by itself.
If you delete mounted containers (including dbdata, db1, and db2), the data volume is not automatically deleted. If you want to delete a data volume, you must explicitly use the docker rm-v command to delete the associated container when deleting the last container that still holds it.
Use data volume containers to migrate data
The data volume can be backed up and restored by the data volume container to realize the 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:
The copy code is as follows: $docker run-volumes-from dbdata-v $(pwd): / backup-name worker ubuntu tar cvf / backup/backup.tar / dbdata
First, a container worker is created using the ubuntu image. Use the-- volumes-from dbdata parameter to have the worker container mount the dbdata container's data volume (that is, the dbdata data volume), and use the-v $(pwd): / backup parameter to mount the local current directory to the worker container's / backup directory. After the worker container starts, the command tar cvf / backup/backup.tar / dbdata is used to back up the contents under / dbdata as / backup/backup.tar in the container, that is, the backup.tar under the current directory of the host host.
two。 Restore
If you want to restore the data to a container, you can follow these steps.
First create a container dbdata2 with data volumes:
$docker run-v / dbdata-- name dbdata2 ubuntu / bin/bash
Then create another new container, mount the dbdata2 container, and use untar to extract the backup file to the mounted container volume:
$docker run-- volumes-from dbdata2-v $(pwd): / backup-- name worker ubuntu bashcd / dbdatatar xvf / backup/backup.tar
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.