【问题标题】:How do I restrict which directories and files are copied by Docker?如何限制 Docker 复制的目录和文件?
【发布时间】:2019-05-23 14:20:40
【问题描述】:

我有一个 Dockerfile,它明确定义了上下文目录中的哪些目录和文件被复制到应用程序目录。但不管这个 Docker 试图复制上下文目录中的所有文件。

Dockerfile 在上下文目录中。

我的测试代码和数据文件位于上下文目录的正下方。它试图复制上下文目录中的所有内容,而不仅仅是我的 COPY 命令指定的目录和文件。所以我收到了几百条以下错误消息,除了在每个目录和子目录中指定每个文件:

ERRO[0043] Can't add file /home/david/gitlab/etl/testdata/test_s3_fetched.csv to tar: archive/tar: missed writing 12029507 bytes
...
ERRO[0043] Can't close tar writer: archive/tar: missed writing 12029507 bytes 
Sending build context to Docker daemon  1.164GB
Error response from daemon: Error processing tar file(exit status 1): unexpected EOF

我对参考的阅读是,如果没有 ADD 或 COPY 指令,它只会复制所有文件和目录。

我尝试了以下 COPY 模式

COPY ./name/ /app/name
COPY name/ /app/name
COPY name /app/name

WORKDIR /app
COPY ./name/ /name

WORKDIR /app
COPY name/ /name

WORKDIR /app
COPY name /name

我的 Dockerfile:

FROM python3.7.3-alpine3.9

RUN apk update && apk upgrade && apk add bash
# Copy app
WORKDIR /app
COPY app /app
COPY configfiles /configfiles
COPY logs /logs/
COPY errorfiles /errorfiles
COPY shell /shell
COPY ./*.py .

WORKDIR ../
COPY requirements.txt /tmp/

RUN pip install -U pip && pip install -U sphinx && pip install -r /tmp/requirements.txt

EXPOSE 22 80 8887

我希望它只复制我的文件,而不会出现与尝试复制我在 COPY 命令中未指定的文件相关的错误。由于 Docker 输出由于 aqll thew 错误消息而滚动出我的终端窗口,我看不到它是否成功使用我的 COPY 命令。

【问题讨论】:

    标签: docker dockerfile


    【解决方案1】:

    构建目录下的所有文件都被复制到 docker 构建上下文的初始层。
    考虑使用 .dockerignore file 从构建中排除文件和目录。

    【讨论】:

    • 这让我看到了潜在的问题,消除了数百条消息。图片名称错误
    【解决方案2】:

    尝试按以下方式复制文件-

     # set working directory
     WORKDIR /usr/src/app
    
     # add and install requirements
     COPY ./requirements.txt /usr/src/app/requirements.txt
     RUN pip install -r requirements.txt
    
     # add app
     COPY ./errorfiles /usr/src/app
    

    此外,您还必须确保您的 docker-compose.yml 文件已正确构建-

     version: "3.6"
     services:
          users:
               build:
                    context: ./app
                    dockerfile: Dockerfile
               volumes:
                    - "./app:/usr/src/app"
    

    在这里,我假设您的 docker-compose.yml 文件位于您应用的父目录中。

    看看这是否有效。 :)

    【讨论】:

    • COPY . /usr/src/app 不是我想要的,我只想复制特定的目录和文件。
    • 假设您要复制一个目录errorfiles,因此您可以将其设置为COPY ./errorfiles /usr/src/app,其中/usr/src/app 是您希望在虚拟机中拥有该目录的路径。
    猜你喜欢
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 2019-03-15
    • 1970-01-01
    • 2019-09-29
    相关资源
    最近更新 更多