问题描述
我对docker撰写文件中的命名卷的功能有些困惑,尤其是在备份/还原我的应用程序时.
I'm a little bit confused with the functionnality of named volumes in a docker compose file specifically when it comes to backup/restore my app.
我实际上正在测试此dockercompose文件:
I'm actually testing this dockercompose file :
version: '2'
services:
django:
build:
context: "{{ build_dir }}/docker/django"
depends_on:
- db
environment:
[...]
volumes:
- code:/data/code
- www:/var/www
- conf:/data/conf
networks:
- front
- db
expose:
- "8080"
entrypoint: "/init"
db:
build:
context: "{{ build_dir }}/docker/postgres"
environment:
[...]
volumes:
- data:/var/lib/postgresql/data
networks:
- db
volumes:
data:
www:
code:
conf:
networks:
front:
external:
name: "proxy_nw"
正如文档所述,我尝试使用命名卷而不是仅数据容器.但是我应该如何备份我的数据?
As the documentation said, I tried to use named volume instead of data only container. But how am I suppose to backup my data ?
使用仅数据容器,我会做一个docker run --rm --volume-from DOC backup_container save
,这确实很容易.
With a data only container I would have done a docker run --rm --volume-from DOC backup_container save
which is really easy.
现在,我在本主题中阅读了 使用类似docker run --rm --volume data --volume www --volume code --volume conf backup_container save
之类的东西.这不是那么简单,因为我有许多具有不同类型和卷名的应用程序,因此这意味着我的保存数据的命令对于每个应用程序都必须有所不同.它使自动化过程复杂化.
Now I read in this topic that I should use something like docker run --rm --volume data --volume www --volume code --volume conf backup_container save
. This is not so simple because I have many applications with different types and names of volumes so it means that my command to save my data would have to be different for each application. It complicate automation process.
实际上,此语法docker run --volume data --volume www container_image my_command
不正确.它需要容器内的挂载点,因此docker run --volume data:/somewhere --volume www:/somewhereelse container_image my_command
.因此,与备份容器一起使用甚至更加复杂.
Actually this syntaxedocker run --volume data --volume www container_image my_command
is not correct.It needs the mountpoint inside the container, so it would bedocker run --volume data:/somewhere --volume www:/somewhereelse container_image my_command
.So it's even more complicated to use with a backup container.
那么,这种情况下的最佳实践是什么?
So, what are the best practices in this case ?
推荐答案
实际上,它应该与官方文档中所述的方法相同.数据卷容器将其数据存储在虚拟根目录"中,因此您应该使用下一条命令进行备份:
Actually it should be done same way as written in official documentation. Data volume container stores it's data in "virtual root", so you should backup with next command:
docker run --rm \
--volume [DOCKER_COMPOSE_PREFIX]_[VOLUME_NAME]:/[TEMPORARY_DIRECTORY_TO_STORE_VOLUME_DATA] \
--volume $(pwd):/[TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE] \
ubuntu \
tar cvf /[TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE]/[BACKUP_FILENAME].tar /[TEMPORARY_DIRECTORY_TO_STORE_VOLUME_DATA]
其中:
- -rm 表示将清除为此运行命令创建的映像 默认情况下,
- DOCKER_COMPOSE_PREFIX 是您的项目目录名称
- VOLUME_NAME 是撰写文件中的数据量容器名称
- TEMPORARY_DIRECTORY_TO_STORE_VOLUME_DATA 是用于装载卷数据的目录
- TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE 是虚拟映射到您当前目录的目录,将在其中放置备份
- BACKUP_FILENAME -备份文件的名称(您可以在当前目录中找到它)
- ubuntu -您可以将图像类型更改为带有tar的另一个容器:)
- --rm means that the image created for this run command will be cleaned up
- DOCKER_COMPOSE_PREFIX in default is your project directory name
- VOLUME_NAME is the data-volume container name from compose file
- TEMPORARY_DIRECTORY_TO_STORE_VOLUME_DATA is a directory to mount your volume data
- TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE is a directory virtually mapped to your current directory, where the backup will be placed
- BACKUP_FILENAME - a name of backup file (you find it in current directory)
- ubuntu - you may change image type to another container with tar :)
将数据取回卷(还原):
Get data back into the volume(restore):
docker run --rm \
--volume [DOCKER_COMPOSE_PREFIX]_[VOLUME_NAME]:/[TEMPORARY_DIRECTORY_STORING_EXTRACTED_BACKUP] \
--volume $(pwd):/[TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE] \
ubuntu \
tar xvf /[TEMPORARY_DIRECTORY_TO_STORE_BACKUP_FILE]/[BACKUP_FILENAME].tar -C /[TEMPORARY_DIRECTORY_STORING_EXTRACTED_BACKUP] --strip 1
其中:
- TEMPORARY_DIRECTORY_STORING_EXTRACTED_BACKUP 是将提取的文件复制到的目录(此目录与卷链接,因此将写入该目录)
- -C -告诉tar从何处提取内容
- -strip 1 -删除前导路径元素(例如,如果备份内容位于/temp文件夹或类似文件夹中,则为父目录)
- TEMPORARY_DIRECTORY_STORING_EXTRACTED_BACKUP is a directory where the extracted files will be copied to (this is linked with the volume and will therefore write to it)
- -C - tell tar where to extract the contents
- --strip 1 - remove leading path elements (e.g. the parent directory if the backup contents are located in a /temp folder or similar)
这篇关于我应该如何备份和备份?恢复docker命名卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!