×
☰ See All Chapters

Docker ADD command

The docker ADD command can be used to copy the files or directories from the Docker host to the image file system. The ADD instruction is similar to the COPY instruction. However, in addition to the functionality supported by the COPY instruction, the ADD instruction can handle the TAR files and the remote URLs. If source is given with TAR files either from file system or from remote URLs, ADD command will not just copy that TAR file but automatically uploads, uncompresses, and puts the resulting folders and files of TAR file at the destination. If you source is a TAR file for a COPY command, COPY command just copy that TAR file to destination, it will not uncompress and extract the files from it.

Docker ADD command Syntax

ADD <src> <dst>

<src>: Source file/directory

<dst>: Destination

  • <src> can be either file or can be directory.  

ADD sources/config.xml app.xml        

# Copy file config.xml and rename it to app.xml

ADD sources /sources/

# Copy all contents from sources directory to sources directory in image

ADD sources/deploy.tar.gz /sources/

# Extract all contents from sources/deploy.tar.gz file to sources directory in image

ADD https://www.java4coding.com/deploy.tar.gz /sources/

# Download the deploy.tar.gz from https://www.java4coding.com/ and extract the contents of deploy.tar.gz to sources directory in image

  • If <dst> not starts with / then it is considered as absolute path. 

  • If <dst> starts with / then it is considered as relative path to the work directory. 

  • If <dst> ends with / the it is considered as directory. 

  • If <dst> does not ends with / the it is considered as file. A file without any extension will be created. 

ADD sources/config.xml config.xml

# Copy file sources/config.xml to root/config.xml

ADD sources/config.xml /config.xml

# Copy file sources/config.xml to /<workdirectory>/config.xml

ADD sources/config.xml config.xml/

 

# Copy file sources/config.xml to root/config.xml/config.xml. Note that a directory with name config.xml is also created and inside this file config.xml is created.

ADD sources/config.xml config

# Copy file sources/config.xml to root/config, a file without extension.

  • <src> not necessary to start with / and always considered as relative to the directory where dockerfile is located. If your docker file is located in C:/docker/test/dockerfile then for the command “COPY sources/config.xml app.xml” config.xml is located from C:/docker/test/sources/config.xml         

sources, /sources, sources/, /sources/ these all are considered to be same sources directory.

  • <src> is considered as file only if it has file extension. 

  • <src> can have multiple files. You can specify multiple files either directly or through wildcards. When <src> has multiple files then <dst> should be directory. 

  • If <src> is tar file specified either form file system or from remote URL, then <dst> should be directory. 

  • <src> is directory then <dst> should be directory. 

ADD sources/config.xml sources/build.xml config/

# Copy file sources/config.xml and sources/build.xml to root/config directory. <dst> should end with / to consider it as directory

ADD sources/config.xml sources/build.xml config

# Error <dst> should be directory. <dst> does not end with / hence it is considered as file.

ADD sources/deploy.tar.gz sources/build.xml config

# Error <dst> should be directory. <dst> does not end with / hence it is considered as file.

ADD sources/*.xml sources/build.xml config/

# Copy all file with xml extension to root/config directory.

ADD sources/config?.xml config/

# ? is replaced with any single character, e.g., config1.xml, config2.xml, configZ.xml, config#.xml

ADD sources/config??.xml config/

# ?? is replaced with any two character, e.g., config11.xml, config32.xml, configZZ.xml, config$#.xml

 

 


All Chapters
Author