What is the Docker ADD command?
The ADD command is used to copy files/directories into a Docker image. It can copy data in three ways:
-
Copy files from the local storage to a destination in the Docker image.
-
Copy a tarball from the local storage and extract it automatically inside a destination in the Docker image.
-
Copy files from a URL to a destination inside the Docker image.
Syntax
The ADD command requires a source and a destination.
ADD source destination
-
If
sourceis a file, it is simply copied to thedestinationdirectory. -
If
sourceis a directory, its contents are copied to thedestination, but the directory itself is not copied. -
sourcecan be either a tarball or a URL (as well). -
sourceneeds to be within the directory where thedocker buildcommand was run. -
Multiple sources can be used in one
ADDcommand.
Code
Let’s suppose that, in the Dockerfile directory, we have a folder called codes which contains multiple C++ files.
Here’s how we can add all the files of the folder to a test directory in our Docker image:
FROM ubuntu:latestRUN mkdir rootRUN cd rootWORKDIR /rootRUN mkdir testADD codes /root/test
To add specific files from the codes folder, we can specify their names in the ADD command:
ADD codes/file1.cpp codes/file2.cpp root/test/
When adding multiple source files or directories, there must be a
/at the end of the destination directory.
The same syntax is followed for tarballs and URLs.
Free Resources