Dockerfile Reference – ADD & COPY Instructions

Hello Everyone,
Hope you’re doing good and utilizing your time in learning new things. In this article today, we’ll learn about ADD and COPY instructions used in Dockerfile. To find other articles on Docker and Containerization, please check index page.

ADD Instruction :
ADD instruction copies new files, directories or remote urls to the container at given destination. Supoorted Forms,

ADD [--chown=<user>:group] <src>...<dst>

# when file path contains whitespaces,
ADD [--chown=<user>:<group>] ["<src>",...,"<dst>"]
  • –chown parameter is supported when buiding Dockerfile for Linux containers, it does not work on Windows containers.
  • Multiple <src> can be specified, the path can be absolute or relative to WORKDIR.
  • <dst> can also be a file or directory, absolute or relative to WORKDIR.
  • Each src may contain wild card, matching will be done using Go’s filepath.Match rules.
  • All new files and directories will be created with a UID and GID of 0, unless explicitly specified –chown paramter.
  • –chown can contain, username / groupname or UID / GID or any combination of the same. when using username / groupname, /etc/passwd and / etc/group are used to find mapping to UID and GID, if those files doesn’t exist it will fail and will expect UID and GID instead of username and groupname.
  • If your src contains URL, the file will be downloaded and will have permission set as 600.
  • If your file is protected and requires to pass authentication, you’ll need to use RUN wget or RUN curl command as ADD instruction doesn’t support authentication.
  • ADD instruction invalidates the cache for following instructions. It includes the cache for RUN instruction we saw earlier.
# Rules of using ADD Command
# If <src> is URL and <dst> doesn't end with / then the contents of url is downloaded and copied to <dst>.
# If <src> is URL and <dst> is a directory then the filename is inferred from the URL.
# If <src> is a directory, entire conent of <src> will be copied including filesystem metadata.
# if <src> is a compressed file with a known format, it will be decompressed as a directory. But if a <src> is URL which contains compressed file, it will be stored as compressed file only.
# If multiple <src> are mentioned, <dst> must be a directory.
# If <dst> doesn't exist, it will be created including all the directories in it's path.

# Examples of using ADD instruction - There are many ways you can use this instruction, here are some examples.

# Adding all files starting with somefi
ADD somefi* /absolute/path/

# Adding files which has one character after somefile
ADD somefile?.txt relative/path/

# Adding specific file
ADD package.json .

# specify user / group or any combination
ADD --chown=1001:0 files* /absolute/path/
ADD --chown=dockeruser:dockergroup files* relative/path/
ADD --chown=10 files* /absolute/path/

COPY Instruction :
COPY instruction copies given files or directories to containers at given destination. Supported formats

COPY [--chown=<user>:group] <src>...<dst>

# when file path contains whitespaces,
COPY [--chown=<user>:<group>] ["<src>",...,"<dst>"]
  • All rules which we just discussed for ADD instructions are same for COPY instruction except URL, you can not specify a URL to COPY instruction.
  • In addition to it, COPY instruction can have additional flag –from=<name> which can be used while creating multi stage build, where you can set source location from previous build. (you can use the name same as you specified in FROM instruction like, FROM … AS <name>)
# same examples as we saw in ADD instruction, just change ADD to COPY
# Adding all files starting with somefi
COPY somefi* /absolute/path/

# Adding files which has one character after somefile
COPY somefile?.txt relative/path/

# Adding specific file
COPY package.json .

# specify user / group or any combination
COPY --chown=1001:0 files* /absolute/path/
COPY --chown=dockeruser:dockergroup files* relative/path/
COPY --chown=10 files* /absolute/path/

That’s it for this article, hope it was informative for you and you got to know something new. If you want to check other similar articles please check index page. Also please let me know your feedback / suggestions / queries in comments. Keep growing and Happy Learning !

Reference : https://docs.docker.com

Sanket Modi

Working in Information Technology since 2012. Started my career as Java Developer and now working in multiple different technologies like nodejs, Python, Docker, Kubernetes, Azure etc. I like to explore new technologies and read books.

You may also like...

1 Response

  1. February 23, 2021

    […] Dockerfile Reference – ADD & COPY Instructions […]

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: