在docker-compose build或docker-compose up上创建文件/文件夹 [英] Create files / folders on docker-compose build or docker-compose up

查看:1245
本文介绍了在docker-compose build或docker-compose up上创建文件/文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进入Docker的第一步,所以我尝试制作一个创建简单index.html和目录图像的Dockerfile(请参见下面的代码)

I'm trying my first steps into Docker, so I tried making a Dockerfile that creates a simple index.html and a directory images (See code below)

然后我运行docker-compose build来创建映像,并运行docker-compose-up来运行服务器。但是我没有文件index.html或文件夹图像。

Then I run docker-compose build to create the image, and docker-compose-up to run the server. But I get no file index.html or folder images.

这是我的Dockerfile:

This is my Dockerfile:

FROM php:apache
MAINTAINER brent@dropsolid.com

WORKDIR /var/www/html

RUN touch index.html \
    && mkdir images

这是我的docker-compose.yml

And this is my docker-compose.yml

version: '2'
services:
  web:
    build: .docker/web
    ports:
      - "80:80"
    volumes:
      - ./docroot:/var/www/html

我希望这会创建一个带有图像目录和index.html的docroot文件夹,但我只会得到docroot。

I would expect that this would create a docroot folder with an image directory and an index.html, but I only get the docroot.

推荐答案

映像确实包含那些文件



Dockerfile包含有关如何生成映像的说明。从该Dockerfile构建的映像确实包含 index.html images /

在运行时,您根据映像创建了一个容器内置的。在该容器中,将外部目录 ./ docroot 挂载为 / var / www / html

At runtime, you created a container from the image you built. In that container, you mounted the external directory ./docroot as /var/www/html.

挂载将隐藏该路径之前的内容,因此该挂载将隐藏 / var / www / html 的先前内容,将它们替换为 ./ docroot 中的任何内容。

A mount will hide whatever was at that path before, so this mount will hide the prior contents of /var/www/html, replacing them with whatever is in ./docroot.

在您询问的评论中


是否有可能先挂载然后创建文件之类的东西?还是不可能?

is there a possibility then to first mount and then create files or something? Or is that impossible?

您做事的方式是,将它们挂载在原始文件上,因此一旦

The way you have done things, you mounted over your original files, so they are no longer accessible once the container is created.

有两种处理方法。

如果将这些文件放在图像中的其他路径中,则装载不会覆盖它们。

If you put these files in a different path in your image, then they will not be overwritten by the mount.

WORKDIR /var/www/alternate-html

RUN touch index.html \
    && mkdir images

WORKDIR /var/www/html

现在,在运行时您仍然可以在 / var / www / html 进行此装载,其中将包含外部目录中的内容。可能是也可能不是空目录。您可以在启动时告诉容器运行脚本并在其中复制内容。

Now, at runtime you will still have this mount at /var/www/html, which will contain the contents from the external directory. Which may or may not be an empty directory. You can tell the container on startup to run a script and copy things there, if that's what you want.

COPY entrypoint.sh /entrypoint.sh
RUN chmod 0755 /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]

(这是假设您没有定义的入口点-如果这样做,则可能只需要调整现有脚本即可。)

(This is assuming you do not have a defined entrypoint - if you do, you'll maybe just need to adjust your existing script instead.)

entrypoint.sh:

entrypoint.sh:

#!/bin/sh

cp -r /var/www/alternate-html/* /var/www/html
exec "$@"

这将运行 cp 命令,然后将控制权移交给任何 CMD

This will run the cp command, and then hand control over to whatever the CMD for this image is.

您还可以选择只需在外部将所需文件预填充到 ./ docroot 中即可。然后它们将在容器启动并添加目录装入时就在那里。

You also have the option of simply pre-populating the files you want into ./docroot externally. Then they will just be there when the container starts and adds the directory mount.

这篇关于在docker-compose build或docker-compose up上创建文件/文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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