What is the Docker ADD command

The ADD command copies new files, directories or remote file URLs from source src and adds them to the filesystem of the image at the destination dest path.

Multiple src resources may be specified but if they are files or directories, their paths are interpreted as relative to the source of the context of the build.

The ADD command is used to copy files/directories into Docker filesystem of the image.

The ADD command copy data in three different ways:

  • Copy files from the local storage to a destination in the Docker image.
  • Copy a tarball archive from the local storage and extract it automatically inside a destination dest path in the Docker image.
  • Copy files from a URL to a dest inside the Docker image.

ADD Diagram

Syntax

ADD src dest
  • If src is a file, it is simply copied to the dest directory.
  • If src is a directory, its contents are copied to the dest, but the directory itself is not copied.
  • src can be either a tarball or the URL.
  • src needs to be within the directory where the docker build command was run.
  • You can specify multiple sources for on ADD command.

Code

Recently I had a problem with Linux Debian tool wkhtmltodpf which in Debian Bulleye version comes as QT unpatched and there was a need to install a QT patched version of wkhtmltodpf

Patched version of wkhtmltodpf can be downloaded from wkhtmltopdf

Let’s compose Docker ADD command for this:

ARG WKHTMLTOPDF_DEB=https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.bullseye_arm64.deb
ADD $WKHTMLTOPDF_DEB /code/wkhtmltopdf.deb

Later we will be using download file at /code/wkhtmltopdf.deb by ADD command like this:

RUN dpkg -i code/wkhtmltopdf.deb && rm -rf code/wkhtmltopdf.deb

When adding multiple dest files or directories, there must be a / at the end of the destination directory.

The same syntax is followed for tarballs and URLs.

Congratulations 🏆 on learning about Docker ADD command!

Written by

Sergii Demianchuk

Applications Architect