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

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

问题描述

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



我使用的Docker文件是如下:

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

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

  $ cat test.txt 
这是一个测试

但是,当我构建此图像时,我会收到一个图像,指出目标路径不是目录。

  $ docker build。 
将构建上下文发送到Docker守护程序4.608 kB
将构建上下文发送到Docker守护程序
步骤0:FROM debian:latest
---> 9a61b6b1315e
步骤1:运行mkdir -p / usr / src / app
--->使用缓存
---> 86bd44a8776e
步骤2:WORKDIR / usr / src / app
--->使用缓存
---> ed6771adc681
步骤3:ADD test.txt / usr / src / app
stat /var/lib/docker/devicemapper/mnt/ee5b9b7029f2adf27d332cbb0d98d6ad9927629a7569fd2d9574cb767b23547b/rootfs/usr/src/app/test.txt:不是目录

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



我正在CentOS 7上尝试安装以下Docker版本:

 客户端版本:1.7.1 
客户端API版本:1.19
Go版本(客户端):go1.4.2
Git commit(client):786b29d
操作系统/ Arch(客户端):linux / amd64
服务器版本:1.7.1
服务器API版本:1.19
转到版本):go1.4.2
Git commit(server):786b29d
操作系统/ Arch(服务器):linux / amd64

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

  FROM debian:最新
运行mkdir -p / usr / src / app
WORKDIR / usr / src / app
COPY test / usr / src / app $ b请注意,在上面的Docker文件中,我复制整个./test目录,而不是只有./test.txt。



Docker文档对于COPY指令,有以下示例用例类似于我正在尝试的操作:

  COPY可以有人指出我做错了什么吗?????txt / mydir / 



<如何复制单个文件?

如Dockerfile文档所述:



  • 如果< src> 是任何其他类型的文件,它将与其元数据一起单独复制。在这种情况下,如果< dest> 以尾部斜线 / 结束,则将被视为目录, < src> 的内容将以< dest> / base(< src>) / p>


  • 如果< dest> 不以尾部斜杠结尾,则会被视为常规文件并且< src> 的内容将写在< dest>



因此,您必须编写 COPY test.txt / usr / src / app / ,尾随 /


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

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

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

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.

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

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

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

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?

解决方案

As stated in the Dockerfile documentation:

  • 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>).

  • 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>.

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

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

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