Saturday, February 19, 2022

[SOLVED] Dockerfile ADD doesn't work

Issue

When I was trying to build my own docker image with Dockerfile, I found that files doesn't copied into my image after I use ADD or COPY command.

To test the ADD command, I create a simple dockerfile like this:

FROM centos:7
MAINTAINER jayce 

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

And my file structure is simple like this:

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

0 directories, 2 files

When I use docker build command to build an image, the process goes like this:

[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]$

The process ends with the cat command didn't found the test.txt I added into the image with the dockerfile script.

This confused me a lot and I tried it in different environment but none of my attempts failed. Is there something I missed?

My docker version:

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

Somebody help me! Thanks in advance!


Solution

Your source directory is correct — '.'. Your destination dir may lead to issues (~/var). I would specify the absolute path of the home dir intended e.g.

/home//var. Furthermore, you can inspect the Docker image at the layer the build broke — in your case:

docker run --rm -it 64f1a5a8e039 bash  

Further details here: How can I inspect the file system of a failed `docker build`?



Answered By - vmatekole
Answer Checked By - David Marino (WPSolving Volunteer)