Trusted answers to developer questions

What is the Docker ADD command?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.

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.

svg viewer

Syntax

The ADD command requires a source and a destination.

ADD source destination
  • If source is a file, it is simply copied to the destination directory.

  • If source is a directory, its contents are copied to the destination, but the directory itself is not copied.

  • source can be either a tarball or a URL (as well).

  • source needs to be within the directory where the docker build command was run.

  • Multiple sources can be used in one ADD command.

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:latest
RUN mkdir root
RUN cd root
WORKDIR /root
RUN mkdir test
ADD 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.

RELATED TAGS

docker
dockerfile
add
container
Copyright ©2023 Educative, Inc. All rights reserved
Did you find this helpful?