docker-compose 在主机和容器上持久化数据 [英] docker-compose persistent data on host and container

查看:48
本文介绍了docker-compose 在主机和容器上持久化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 docker-compose yml 3.0+ 中遇到卷问题

I have a problem with volumes in docker-compose yml 3.0+

所以我知道一个卷的行为就像一个挂载..但是我已经设置了一个 wiki,当我在 docker-compose 中设置一个卷时,容器上的数据将被删除(隐藏)

So I know that a volume behaves like a mount.. But I have set up a wiki and when i set a volume in the docker-compose, the data on the container will be removed (hidden)

那么如何先将容器中的数据保存到主机,下次启动容器时,它只会覆盖我保存的数据.

So how can I save data from my container to my host first and the next time I start the container, it will just overrides the data I saved.

所以现在的情况是:我从docker-compose up --build"开始,然后创建一个卷(空)并将其复制到容器中.结果容器上该文件夹中的所有内容都被删除

So the current situation is: I start with "docker-compose up --build" and a volume is created (empty) and will be copied to the container.. Everything in that folder on the container is deleted as a result

docker-compose.yml

docker-compose.yml

version: '3.1'

services:

  doku-wiki:
    build: .
    ports:
      - '4000:80'

Dockerfile

FROM php:7.1-apache
COPY dokuwiki-stable /var/www/html/
COPY entrypoint.sh /entrypoint.sh
RUN chmod 777 /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 80

推荐答案

听起来您正在使用主机卷,您可以将主机目录映射到容器中.执行此操作后,图像中该位置的任何内容都将不可见,只有主机上存在的文件才可见.

It sounds like you are using a host volume where you map a host directory into the container. When you do this, anything at that location inside your image will not be visible, only the files as they exist on the host.

如果您想从图像中复制文件来初始化卷,您有两个选择:

If you want to have a copy of the files from inside your image to initialize the volume, you have two options:

  1. 切换到命名卷.Docker 会自动将这些初始化为镜像的内容,包括任何权限.如果您不需要从 docker 外部直接访问文件,这是首选解决方案.

  1. Switched to a named volume. Docker will automatically initialize these to the contents of the image, including any permissions. If you don't require direct access to the files from outside of docker, this is the preferred solution.

更改您的图像入口点以及您在图像中存储文件的位置.

Change your image entrypoint and the location where you store your files in the image.

在第二个选项中,如果您希望/data 成为您的应用程序的卷,您可以有一个 entrypoint.sh:

On the second option, if you want /data to be a volume for your application, you could have an entrypoint.sh that does:

#!/bin/sh

if [ ! -d "/data" ]; then
  ln -s /data_save /data
elif [ -z "$(ls -A /data)" ]; then
  cp -a /data_save/. /data/
fi
exec "$@"

您的图像需要将所有初始文件保存到/data_save 而不是/data.然后,如果目录为空,它会将/data_save 的副本复制到您的卷/data.如果卷根本没有映射,那么它只会创建一个从/data 到/data_save 的符号链接.最后一行从 Dockerfile 或 docker run cli 运行 CMD ,就好像入口点不存在一样.添加到 Dockerfile 的行将如下所示:

Your image would need to save all the initial files to /data_save instead of /data. Then if the directory is empty it would do a copy of /data_save to your volume /data. If the volume wasn't mapped at all, then it just creates a symlink from /data to /data_save. The last line runs the CMD from your Dockerfile or docker run cli as if the entrypoint wasn't ever there. The added lines to your Dockerfile would look like:

COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

这篇关于docker-compose 在主机和容器上持久化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆