In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to manage data in Docker". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to manage data in Docker.
Managing Data in Containers
So far we have introduced some basic ideas of Docker, knowing how to use Docker's image and how to communicate through the network between multiple container. In this chapter we will show you how to manage data within the container of docker and how to share data between different container.
We will introduce two main ways to manage data in docker:
Data volumes and
Data volume container
Data volumes
A data volume is a special-purpose directory in one or more container. Bypassing Union File System, it provides the following useful features for persisting and sharing data:
Data volumes can share and reuse data between different container
Changes to Data volume take effect in a timely manner (translator: data volumn is a directory, multiple container are mounted to this directory, you can see the information of volumne through docker inspect)
Changes to data volume are not included when upgrading image. (translator: image is stateless in the entire design of docker, which is very beneficial to upgrade reuse. Data that marks the status, such as database data, production log, and so on, should be placed in volume. The persistence and recovery of volume is described below, in the form of files, not through image)
Persistence of Volumes until no container uses them
Adding a data volume
You can use-v to add a data volume when you docker run. This parameter can be used multiple times during docker run to add multiple data volumes. Let's mount a volume for our web application container.
$sudo docker run-d-P-name web-v / webapp training/webapp python app.py
Here a new volume will be created into / webapp in container. (if you log in to one of your container shell via ssh or-I, use ls / webapp to verify that the mount is successful.)
Note: you can also add the VOLUME field to the Dockerfile, so that when you create a new image container, a new volume is automatically created.
Mount a Host Directory as a Data Volume
Use-v not only to create a new volume, but also to mount a host directory into container.
$sudo docker run-d-P-name web-v / src/webapp:/opt/webapp training/webapp python app.py
This command will put the local directory / src/webapp mount to the / opt/webapp directory in container. It is very convenient to test the program with this method. For example, we can mount our source code into container through this method. After modifying the local code, we can immediately see how the modified code works in container. The host directory must be an absolute path. If this directory does not exist, docker will automatically create it for you.
Note that it is not possible to use Dockerfile here, because this usage runs counter to portability and sharing. Because the local directory, as his name tells us, is locally related and may not work on all hosts. (who knows what host looks like when you use image)
Docker defaults to volume as read-write, but we can also mount a directory as read-only:
$sudo docker run-d-P-name web-v / src/webapp:/opt/webapp:ro training/webapp python app.py
Here we also mount the / src/webapp directory, but we add the ro parameter to tell docker that the volume is read-only.
Creating and mounting a Data Volume Container
If you have some persistent data and want to share it between different container, or want to use it in some non-persistent container, the best way is to use Data Volumn Container to mount the data into your container. (translator: as the translator mentioned at the beginning, the container of docker is stateless, that is, data that marks the status, such as database data, application log, etc., should not be placed in container, but in Data Volume Container, which is very similar to funcational programming, so I like to call a general docker container functional container to distinguish data volume container.)
Let's create a named Data Volume Container to share data.
$docker run-d-v / dbdata-- name dbdata training/postgres
After doing this, you can transfer / dbdata mount to other container via-- volumes-from.
$docker run-d-volumes-from dbdata-name db1 training/postgres
You can continue to share it to another container.
$docker run-d-volumes-from dbdata-name db2 training/postgres
-volumes-from can be used multiple times to mount multiple volumes in multiple conatainer.
This operation is chained, and the volume we enter through-- volumes-from mount in db1 can continue to be used by other container.
$docker run-d-name db3-volumes-from db1 training/postgres
(translator: instead of using volume container directly, we use the functional container of db1 to mount the volume to another funcational container. The so-called chain is dbdata-> db1-> db3)
If you remove all the container of mount volumes, including the initialized dbdata container, the volume will be removed. Through this property, you can easily upgrade data or migrate data between different container.
Backup, restore, or migrate data volumes
Another use of Volume is to back up, restore, and migrate data. The specific approach is as follows: use-- volumes-from to create a new container mount volume
$sudo docker run-- volumes-from dbdata-v $(pwd): / backup ubuntu tar cvf / backup/backup.tar / dbdata
Here we launch a new container and mount a volume from dbdata. At the same time, a local directory is mounted to the container. Finally, we back up the data in dbdata to / backup with a tar command. After the command ends and stops the container, we get a backup data locally.
Ubuntu container is used here to package and back up the data in volume to a directory in host.)
The backed up data can be restored to this container, or other container that uses this volume. First create a container
$sudo docker run-v / dbdata-- name dbdata2 ubuntu
Then un-tar backs up the files to data volume
$sudo docker run-- volumes-from dbdata2-v $(pwd): / backup busybox tar xvf / backup/backup.tar
You can use your favorite tools plus the above techniques to automatically back up, migrate and restore data.
Next steps
Now we have learned more about how to use docker. Next we will look at how to combine the services provided by Docker and Docker Hub to achieve automatic build, and learn some knowledge of private repository.
At this point, I believe you have a deeper understanding of "how to manage data in Docker". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.