Trusted answers to developer questions

What is the Docker ADD command?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

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 ©2024 Educative, Inc. All rights reserved
Did you find this helpful?