×
☰ See All Chapters

Docker COPY command

The docker COPY command can be used to copy the files or directories from the Docker host to the image file system.

Docker COPY command Syntax

COPY <src> <dst>

<src>: Source directory/file

<dst>: Destination directory/file

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

COPY sources/config.xml app.xml        

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

COPY sources /sources/

# Copy all contents from sources directory 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. 

COPY sources/config.xml config.xml

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

COPY sources/config.xml /config.xml

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

COPY 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.

COPY 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. 

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

COPY 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

COPY sources/config.xml sources/build.xml config

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

COPY sources/*.xml sources/build.xml config/

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

COPY sources/config?.xml config/

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

COPY sources/config??.xml config/

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

 


All Chapters
Author