如何使用 Dockerfile 将文件从主机复制到容器

问题描述 投票:0回答:8

我写了一个看起来像这样的 Dockerfile

FROM ubuntu:12.04

RUN apt-get update
RUN apt-get install -y wget

现在我的主机中有一个名为

abc.txt
的文件。我怎样才能将它复制到这个容器中。我可以在 Dockerfile 中添加从主机复制到容器的任何步骤吗?

docker boot2docker dockerfile docker-registry
8个回答
166
投票

像这样使用 COPY 命令:

COPY foo.txt /data/foo.txt
# where foo.txt is the relative path on host
# and /data/foo.txt is the absolute path in the image

官方文档

中阅读有关 COPY 的更多详细信息

另一种方法是使用 ADD,但如果您不想使用 ADD 的一些高级功能(例如解压 tar.gz 文件),这不是最佳实践。如果您仍想使用 ADD 命令,请这样做:

ADD abc.txt /data/abc.txt
# where abc.txt is the relative path on host
# and /data/abc.txt is the absolute path in the image

官方文档

中阅读有关 ADD 的更多详细信息

52
投票

对于那些遇到这个(非常不清楚)错误的人:

复制失败:stat /var/lib/docker/tmp/docker-builderXXXXXXX/abc.txt:没有这样的文件或目录

可能有很多原因,包括:

  • 对于 docker-compose 用户,请记住 docker-compose.yml
    context
    会覆盖 Dockerfile 的上下文。您的 COPY 语句现在需要导航相对于 docker-compose.yml 中定义的路径,而不是相对于您的 Dockerfile。
  • COPY 行上的尾随注释或分号:
    COPY abc.txt /app  #This won't work
  • 该文件位于被
    .dockerignore
    .gitignore
    文件忽略的目录中(小心通配符)
  • 你打错字了

有时,

WORKDIR /abc
后接
COPY . xyz/
可以在
COPY /abc xyz/
失败的地方起作用,但它有点难看。


9
投票

我遇到了这个问题,我无法将 zeppelin [1GB] 目录复制到 docker 容器中并且遇到问题

复制失败:统计 /var/lib/docker/tmp/docker-builder977188321/zeppelin-0.7.2-bin-all:否 这样的文件或目录

我使用的 docker 版本:17.09.0-ce 并通过以下步骤解决了该问题。

第1步:将zeppelin目录[我想复制到docker包中]复制到包含“Dockfile”的目录中

第2步:编辑Dockfile并添加命令[我们要复制的位置]

ADD ./zeppelin-0.7.2-bin-all /usr/local/

第3步:进入包含DockFile的目录并运行命令[也可以使用替代方案]

docker build

第4步:docker镜像创建成功并带有日志

步骤5/9:添加./zeppelin-0.7.2-bin-all /usr/local/ ---> 3691c902d9fe

步骤 6/9:WORKDIR $ZEPPELIN_HOME ---> 3adacfb024d8 .... 成功构建b67b9ea09f02


4
投票

我在 CentOS 7 上使用 Docker 19.03.8 时遇到以下错误:

COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXXX/abc.txt: no such file or directory

对我来说,解决方案是使用

docker build .
而不是
docker build - < Dockerfile
从 Docker 文件构建。



2
投票

我能够将文件从主机复制到 dockerfile 中的容器,如下所示:

  1. 在我的 C 驱动器上创建了一个文件夹 --> c:\docker
  2. 在同一文件夹中创建测试文件 --> c:\docker est.txt
  3. 在同一文件夹中创建一个 docker 文件 --> c:\docker\dockerfile
  4. docker文件的内容如下,将文件从本地主机复制到容器的根目录: 来自 ubuntu:16.04

    复制测试.txt /

  5. 从 docker hub 拉取 ubuntu 的副本 --> docker pull ubuntu:16.04
  6. 从 dockerfile 构建镜像 --> docker build -t myubuntu c:\docker\
  7. 从新镜像 myubuntu 构建容器 --> docker run -d -it --name myubuntucontainer myubuntu "/sbin/init"
  8. 连接到新创建的容器-->docker exec -it myubuntucontainer bash
  9. 检查text.txt文件是否在根目录下 --> ls

您应该会看到该文件。


0
投票

如果你想复制当前目录的内容,你可以运行:

docker build  -t <imagename:tag> -f- ./ < Dockerfile

0
投票

这就是我将外部 nginx.conf 文件复制到 Linux 上容器的 /etc/nginx 目录中所需要做的事情。

在 docker-compose.yml 中

 services:
 
   xhgui:
     build:
       # This directory is at docker/xhgui
       context: ./xhgui
       args:
         # This file is at docker/xhgui/config
         NGINX_CONFIG_FILE: ./config/nginx.conf
     image: xhgui/xhgui:latest
     restart: always
     # Not sure if this volume declaration is necessary
     volumes:
       - ../:/xhgui

然后在docker/xhgui/Dockerfile中

ARG NGINX_CONFIG_FILE
COPY ${NGINX_CONFIG_FILE} /etc/nginx/nginx.conf
© www.soinside.com 2019 - 2024. All rights reserved.