Dockerfile ADD不起作用

时间:2017-03-31 09:17:46

标签: linux docker dockerfile centos7

当我尝试使用Dockerfile构建自己的docker镜像时,我发现在使用ADDCOPY命令后文件没有复制到我的图像中。

要测试ADD命令,我创建一个简单的dockerfile,如下所示:

FROM centos:7
MAINTAINER jayce 

RUN mkdir ~/var
ADD . ~/var/
RUN cd ~/var
RUN ls -la ~/var
RUN cat ~/var/test.txt

我的文件结构很简单:

[sysmanager@jayce-vm-centos test]$ tree
.
├── Dockerfile
└── test.txt

0 directories, 2 files

当我使用docker build命令构建图像时,过程如下:

[sysmanager@jayce-vm-centos test]$ sudo docker build .
Sending build context to Docker daemon 3.072 kB
Step 1 : FROM centos:7
 ---> 67591570dd29
Step 2 : MAINTAINER jayce
 ---> Running in 966b82d33d91
 ---> 22e938a4b921
Removing intermediate container 966b82d33d91
Step 3 : RUN mkdir ~/var
 ---> Running in e9ed8986f793
 ---> 672ebaac6e2c
Removing intermediate container e9ed8986f793
Step 4 : ADD . ~/var/
 ---> 592eb5f1b653
Removing intermediate container 6d0adfab0bbf
Step 5 : RUN cd ~/var
 ---> Running in 085c524f3999
 ---> b0b4edf374f2
Removing intermediate container 085c524f3999
Step 6 : RUN ls -la ~/var
 ---> Running in e454df478b21
total 0
drwxr-xr-x. 2 root root   6 Mar 31 09:08 .
dr-xr-x---. 3 root root 125 Mar 31 09:08 ..
 ---> 53557c808816
Removing intermediate container e454df478b21
Step 7 : RUN cat ~/var/test.txt
 ---> Running in 64f1a5a8e039
cat: /root/var/test.txt: No such file or directory
The command '/bin/sh -c cat ~/var/test.txt' returned a non-zero code: 1
[sysmanager@jayce-vm-centos test]$

该过程以cat命令结束,没有找到我使用dockerfile脚本添加到图像中的test.txt

这让我很困惑,我在不同的环境中尝试过,但我的尝试都没有失败。有没有我错过的东西?

我的码头版:

Client:
 Version:         1.12.6
 API version:     1.24
 Package version: docker-common-1.12.6-11.el7.centos.x86_64
 Go version:      go1.7.4
 Git commit:      96d83a5/1.12.6
 Built:           Tue Mar  7 09:23:34 2017
 OS/Arch:         linux/amd64

Server:
 Version:         1.12.6
 API version:     1.24
 Package version: docker-common-1.12.6-11.el7.centos.x86_64
 Go version:      go1.7.4
 Git commit:      96d83a5/1.12.6
 Built:           Tue Mar  7 09:23:34 2017
 OS/Arch:         linux/amd64

有人帮助我!提前谢谢!

1 个答案:

答案 0 :(得分:3)

您的源目录是正确的 - '。'。您的目标目录可能会导致问题(〜/ var)。我会指定家庭目录的绝对路径,例如。

/家庭//变种此外,您可以在构建中断的层上检查Docker镜像 - 在您的情况下:

docker run --rm -it 64f1a5a8e039 bash  

此处有更多详情: How can I inspect the file system of a failed `docker build`?