Dockerfile ADD command adds the contents of source directory from the host to a specific folder location in the docker container as defined in the Dockerfile.

In this article you would also see how ADD is different when compared to VOLUME

ADD command in Dockerfile

root@ubuntu14-04:/home/kmaringanti/addtest# cat Dockerfile


FROM ubuntu:16.10
ADD folderInHost /folderInDocker

Above are the dockerfile contents.


root@ubuntu14-04:/home/kmaringanti/addtest# ls -lart
total 16
-rw-r--r-- 1 root        root          58 Dec  1 13:10 Dockerfile
drwxr-xr-x 3 root        root        4096 Dec  1 13:10 .
drwxr-xr-x 2 root        root        4096 Dec  1 13:20 folderInHost
drwxr-xr-x 8 kmaringanti kmaringanti 4096 Dec  1 17:30 ..
root@ubuntu14-04:/home/kmaringanti/addtest#

Folder created in host which is added to the docker image


root@ubuntu14-04:/home/kmaringanti/addtest/folderInHost# ls -alrt
total 12
drwxr-xr-x 3 root root 4096 Dec  1 13:10 ..
-rw-r--r-- 1 root root   47 Dec  1 13:20 1
drwxr-xr-x 2 root root 4096 Dec  1 17:35 .
root@ubuntu14-04:/home/kmaringanti/addtest/folderInHost#

For now no contents are there in the host.


root@ubuntu14-04:/home/kmaringanti/addtest# docker build -t="addtest" .
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:16.10
 ---> 241684c864ba
Step 1 : ADD folderInHost /folderInDocker
 ---> Using cache
 ---> 183de9cff59a
Successfully built 183de9cff59a



root@ubuntu14-04:/home/kmaringanti/addtest# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE	
addtest             latest              183de9cff59a        4 days ago          101.4 MB		
ubuntu              16.10               241684c864ba        2 weeks ago         101.4 MB
ubuntu              latest              56465e1e45d2        7 weeks ago         127.2 MB

Created an image addtest


root@ubuntu14-04:/home/kmaringanti/addtest# docker run -it addtest /bin/bash
root@0406a6e4352f:/#
root@0406a6e4352f:/#
root@0406a6e4352f:/# ls -lart
total 76
drwxr-xr-x   2 root root 4096 Oct  8 10:11 home
drwxr-xr-x   2 root root 4096 Oct  8 10:11 boot
drwxr-xr-x   2 root root 4096 Nov  4 20:30 srv
drwxr-xr-x   2 root root 4096 Nov  4 20:30 opt
drwxr-xr-x   2 root root 4096 Nov  4 20:30 mnt
drwxr-xr-x   2 root root 4096 Nov  4 20:30 media
drwxr-xr-x   2 root root 4096 Nov  4 20:30 lib64
drwxr-xr-x   8 root root 4096 Nov  4 20:31 lib
drwx------   2 root root 4096 Nov  4 20:31 root
drwxr-xr-x   2 root root 4096 Nov  4 20:31 bin
drwxrwxrwt   2 root root 4096 Nov  4 20:31 tmp
drwxr-xr-x  11 root root 4096 Nov 16 20:58 usr
drwxr-xr-x   2 root root 4096 Nov 16 20:58 sbin
drwxr-xr-x  13 root root 4096 Nov 16 20:58 var
drwxr-xr-x   5 root root 4096 Nov 16 20:58 run
drwxr-xr-x   2 root root 4096 Dec  1 07:40 folderInDocker
-rwxr-xr-x   1 root root    0 Dec  5 09:20 .dockerinit
-rwxr-xr-x   1 root root    0 Dec  5 09:20 .dockerenv
drwxr-xr-x  35 root root 4096 Dec  5 09:20 etc
dr-xr-xr-x 126 root root    0 Dec  5 09:20 proc
dr-xr-xr-x  13 root root    0 Dec  5 09:20 sys
drwxr-xr-x   5 root root  380 Dec  5 09:20 dev
drwxr-xr-x  35 root root 4096 Dec  5 09:20 ..
drwxr-xr-x  35 root root 4096 Dec  5 09:20 .

Now run a container with the addtest image. And you would see a folder created folderInDocker as per the Dockerfile. But as usual you will not see any files as its not there in the folderInHost folder.


root@ubuntu14-04:/home/kmaringanti/addtest# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
0406a6e4352f        addtest:latest      "/bin/bash"         About a minute ago   Up About a minute                       sleepy_morse
r 



root@ubuntu14-04:/home/kmaringanti/addtest/folderInHost# cat >file1.txt
this is file1 contents
^C


root@ubuntu14-04:/home/kmaringanti/addtest/folderInHost# cat file1.txt
this is file1 contents



root@ubuntu14-04:/home/kmaringanti/addtest/folderInHost# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0406a6e4352f        addtest:latest      "/bin/bash"         2 minutes ago       Up 2 minutes                            sleepy_morse



root@ubuntu14-04:/home/kmaringanti/addtest/folderInHost# docker attach 0406a6e4352f	

root@0406a6e4352f:/# cd folderInDocker/	
root@0406a6e4352f:/folderInDocker# ls
root@0406a6e4352f:/folderInDocker#

Come out CTRL+P+Q of console and now add files in host folder. Now add a file in host folder and check whether its there in the docker container. And you will see that its not present. Hence ADD is not similar to VOLUME. It only works when a new image is created after the source folder is updated.


After adding folders build again to reflect new folder content changes in docker container folder

root@ubuntu14-04:/home/kmaringanti/addtest/folderInHost# ls
file1.txt
root@ubuntu14-04:/home/kmaringanti/addtest/folderInHost#
r

Now you have the contents in host


root@ubuntu14-04:/home/kmaringanti/addtest# docker build -t="addtest:1.0" .
Sending build context to Docker daemon 3.584 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:16.10
 ---> 241684c864ba
Step 1 : ADD folderInHost /folderInDocker
 ---> 3f28bc00dab7
Removing intermediate container 2720458fadd8
Successfully built 3f28bc00dab7



root@ubuntu14-04:/home/kmaringanti/addtest# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
addtest             1.0                 3f28bc00dab7        6 seconds ago       101.4 MB	
addtest             latest              183de9cff59a        4 days ago          101.4 MB	
ubuntu              16.10               241684c864ba        2 weeks ago         101.4 MB
ubuntu              latest              56465e1e45d2        7 weeks ago         127.2 MB


root@ubuntu14-04:/home/kmaringanti/addtest# docker run -it addtest:1.0 /bin/bash	
root@c59155c39abd:/#


root@c59155c39abd:/# ls -lart
total 76
drwxr-xr-x   2 root root 4096 Oct  8 10:11 home
drwxr-xr-x   2 root root 4096 Oct  8 10:11 boot
drwxr-xr-x   2 root root 4096 Nov  4 20:30 srv
drwxr-xr-x   2 root root 4096 Nov  4 20:30 opt
drwxr-xr-x   2 root root 4096 Nov  4 20:30 mnt
drwxr-xr-x   2 root root 4096 Nov  4 20:30 media
drwxr-xr-x   2 root root 4096 Nov  4 20:30 lib64
drwxr-xr-x   8 root root 4096 Nov  4 20:31 lib
drwx------   2 root root 4096 Nov  4 20:31 root
drwxr-xr-x   2 root root 4096 Nov  4 20:31 bin
drwxrwxrwt   2 root root 4096 Nov  4 20:31 tmp
drwxr-xr-x  11 root root 4096 Nov 16 20:58 usr
drwxr-xr-x   2 root root 4096 Nov 16 20:58 sbin
drwxr-xr-x  13 root root 4096 Nov 16 20:58 var
drwxr-xr-x   5 root root 4096 Nov 16 20:58 run
dr-xr-xr-x  13 root root    0 Dec  5 09:20 sys
drwxr-xr-x   2 root root 4096 Dec  5 09:25 folderInDocker
drwxr-xr-x  35 root root 4096 Dec  5 09:26 etc
-rwxr-xr-x   1 root root    0 Dec  5 09:26 .dockerinit
-rwxr-xr-x   1 root root    0 Dec  5 09:26 .dockerenv
dr-xr-xr-x 127 root root    0 Dec  5 09:26 proc
drwxr-xr-x   5 root root  380 Dec  5 09:26 dev
drwxr-xr-x  35 root root 4096 Dec  5 09:26 ..
drwxr-xr-x  35 root root 4096 Dec  5 09:26 .


root@c59155c39abd:/# cd folderInDocker/
root@c59155c39abd:/folderInDocker#
root@c59155c39abd:/folderInDocker# ls -alrt
total 12
-rw-r--r--  1 root root   23 Dec  5 09:22 file1.txt
drwxr-xr-x  2 root root 4096 Dec  5 09:25 .
drwxr-xr-x 35 root root 4096 Dec  5 09:26 ..


root@c59155c39abd:/folderInDocker# cat file1.txt
this is file1 contents

Now after a new image is created addtest:1.0, a container is spun using that image and list in the folderInDocker, where you will see the file updated.

NOTE: To have the changes in source folder of the host into the docker in case of ADD, YOU MUST BUILD NEW IMAGE and run the container USING THE NEW IMAGE