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

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

问题描述

我在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

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-在主机和容器上编写持久数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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