Docker - 安装的卷不像常规挂载

时间:2017-04-28 13:48:44

标签: linux docker drupal docker-compose

我是docker的新手,所以我确信我做错了什么。我也是not a php developer但在这种情况下这不重要。

我正在使用drupal泊坞窗图片,该图片的数据位于/var/www/html目录。

我正在尝试使用主机系统上本地目录中的drupal站点覆盖此数据。

根据the docs,这是预期的行为

  

将主机目录挂载为数据卷

     

除了创建一个   使用-v标志的卷也可以从你的目录安装一个目录   Docker引擎的主机进入容器。

     

$ docker run -d -P --name web -v / src / webapp:/ webapp training / webapp   python app.py

     

此命令安装主机目录/ src / webapp,   进入/ webapp的容器。如果路径/ webapp已存在   在容器的图像中,/ src / webapp挂载覆盖,但确实如此   不删除预先存在的内容。一旦装载被移除,   内容可以再次访问。这与预期一致   mount命令的行为。

但是我发现容器上不存在本地drupal站点文件。我的完整工作流程如下:

docker-compose.yml
drupal:
  container_name: empower_drupal
  build: ./build/drupal-local-codebase
  ports:
   - "8888:80"
   - "8022:22"
   - "443"
 #volumes: THIS IS ALSO NOT WORKING
 #- /home/sameh/empower-tap:/var/www/html


$ docker-compose up -d
# edit the container by snapshotting it
$ docker commit empower_drupal empower_drupal1
$ docker run -d -P --name empower_drupal2 -v /home/sameh/empower-tap:/var/ww/html empower_drupal1
# snapshot the container to examine it
$ docker commit 9cfeca48efd3 empower_drupal2
$ docker run -t -i empower_drupal2 /bin/bash

empower_drupal2容器没有/home/sameh/empower-tap目录中的正确文件。

1 个答案:

答案 0 :(得分:3)

为什么这不起作用

这是你所做的,带有一些注释。

$ docker-compose up -d

鉴于您的docker-compose.yml,volumes部分被注释掉,此时您已经运行了容器,但没有安装卷。

# edit the container by snapshotting it
$ docker commit empower_drupal empower_drupal1

除非您的容器在启动时对其自身进行更改,否则您在此处所做的全部内容都是您已经拥有的图像的副本。

$ docker run -d -P --name empower_drupal2 -v /home/sameh/empower-tap:/var/ww/html empower_drupal1

您已在此处运行新副本,已装入卷。好的,这个文件现在可以在这个容器中使用。

# snapshot the container to examine it
$ docker commit 9cfeca48efd3 empower_drupal2

我在这里假设你想将卷的内容提交到图像中。那样不行。 commit documentation很清楚这一点:

  

提交操作不包括容器内安装的卷中包含的任何数据。

$ docker run -t -i empower_drupal2 /bin/bash

因此,正如您所发现的那样,当您运行commit生成的图像但没有卷装入时,文件就不存在了。

此外,您的docker-compose.yml示例中不清楚volumes:部分在注释之前的位置。目前它似乎在左边缘,这是行不通的。它需要与build:ports:处于同一级别才能使用drupal服务。

该怎么做

这取决于你的目标。

只需从本地

复制文件即可

如果您只是想用本地系统中的文件填充图像,可以在Dockerfile中执行此操作。

COPY local-dir/* /var/www/html

您提到此副本无法正常工作,因为该目录不是本地目录。不幸的是,用symlink这样的东西很难解决。您最好的选择是在构建之前将目录复制到本地上下文。 Docker does not plan to change this behavior

覆盖开发内容

一种常见的情况是您希望使用本地目录进行开发,以便立即反映更改,而不是进行重建。但是,如果不进行开发,则需要将文件烘焙到图像中。

在这种情况下,首先告诉Dockerfile将文件复制到图像中,如上所述。这样,图像构建将包含它们,卷装或否。

然后,在进行开发时,使用docker-compose.yml中的volumes:或docker run的-v标志来安装卷。卷装置将覆盖图像中的任何内容,因此您将使用本地文件。当您完成并且代码已准备就绪时,只需进行映像构建,您的最终文件将被烘焙到映像中以进行部署。

使用卷加提交

您也可以通过安装音量,将内容复制到其他位置,然后提交结果,以略微迂回的方式执行此操作。

# start a container with the volume mounted somewhere
docker run -d -v /home/sameh/empower-tap:/var/www/html_temp [...etc...]

# copy the files elsewhere inside the container
docker exec <container-name> cp -r /var/www/html_temp /var/www/html

# commit the result
docker commit empower_drupal empower_drupal1

然后,您应该在生成的图像中安装已装入的卷文件。