如何使用 Docker 的 COPY/ADD 指令将单个文件复制到映像 [英] How to use Docker's COPY/ADD instructions to copy a single file to an image

查看:20
本文介绍了如何使用 Docker 的 COPY/ADD 指令将单个文件复制到映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将单个文件从构建上下文的根目录复制到 docker 映像中新创建的目录中.

I am trying to copy a single file from the root of the build context into a newly created directory in a docker image.

我使用的Dockerfile如下:

The Dockerfile that I am using is as follows:

FROM debian:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY test.txt /usr/src/app

test.txt 文件是一个简单的 ASCII 文本文件,如下:

The test.txt file is a simple ASCII text file as follows:

$ cat test.txt 
This is a test

但是,当我构建此映像时,我得到一个说明目标路径不是目录的映像.

However, when I build this image I get an image stating that the destination path is not a directory.

$ docker build .
Sending build context to Docker daemon 4.608 kB
Sending build context to Docker daemon 
Step 0 : FROM debian:latest
 ---> 9a61b6b1315e
Step 1 : RUN mkdir -p /usr/src/app
 ---> Using cache
 ---> 86bd44a8776e
Step 2 : WORKDIR /usr/src/app
 ---> Using cache
 ---> ed6771adc681
Step 3 : ADD test.txt /usr/src/app
stat /var/lib/docker/devicemapper/mnt/ee5b9b7029f2adf27d332cbb0d98d6ad9927629a7569fd2d9574cb767b23547b/rootfs/usr/src/app/test.txt: not a directory

我尝试过使用 docker 版本、基础映像和安装了 docker 的发行版的多种组合.我也尝试过使用 ADD 而不是 COPY,但结果是一样的.

I have tried using multiple combinations of docker versions, base images, and distributions with docker installed. I have also tried using ADD instead of COPY but the result is the same.

我目前正在安装以下 docker 版本的 CentOS 7 上尝试此操作:

I am currently trying this on CentOS 7 with the following docker version installed:

Client version: 1.7.1
Client API version: 1.19
Go version (client): go1.4.2
Git commit (client): 786b29d
OS/Arch (client): linux/amd64
Server version: 1.7.1
Server API version: 1.19
Go version (server): go1.4.2
Git commit (server): 786b29d
OS/Arch (server): linux/amd64

我注意到我可以复制目录,但不能复制文件.例如,如果我在构建上下文根目录中创建一个test"目录并将 test.txt 放在测试目录中,那么我可以成功复制该目录:

What I have noticed is that I can copy a directory, but not a file. For example, if I create a "test" directory in the build context root and put test.txt inside the test directory then I can copy the directory successfully:

FROM debian:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY test /usr/src/app

请注意,在上面的 Dockerfile 中,我复制了整个 ./test 目录,而不仅仅是 ./test.txt.此构建成功.

Note that in the above Dockerfile I copy the entire ./test directory rather than just ./test.txt. This build successfully.

Docker 文档 包含以下 COPY 指令示例用例,即类似于我正在尝试做的事情:

The Docker documentation has the following sample use case for the COPY instruction which is similar to what I am trying to do:

COPY hom?.txt /mydir/

谁能指出我做错了什么?如何复制单个文件?

Can anyone point out what I am doing wrong? How can I go about copying a single file?

推荐答案

如 Dockerfile 中所述文档:

  • 如果 <src> 是任何其他类型的文件,它会与元数据一起单独复制.在这种情况下,如果 <dest> 以斜杠 / 结尾,它将被视为目录和 将写在 <dest>/base(<src>).

  • If <src> is any other kind of file, it is copied individually along with its metadata. In this case, if <dest> ends with a trailing slash /, it will be considered a directory and the contents of <src> will be written at <dest>/base(<src>).

如果 <dest> 不以斜杠结尾,则将其视为常规文件,<src> 的内容将是写在 <dest>.

If <dest> does not end with a trailing slash, it will be considered a regular file and the contents of <src> will be written at <dest>.

因此,您必须在 COPY test.txt/usr/src/app/ 后面加上 /.

Thus, you have to write COPY test.txt /usr/src/app/ with a trailing /.

这篇关于如何使用 Docker 的 COPY/ADD 指令将单个文件复制到映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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