Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

docker
dockerfile
add
container

What is the Docker ADD command?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring