Introduction of Dockerfile directive ADD and COPY
- 2020-06-12 11:10:54
- OfStack
1. ADD instructions
The FUNCTION of the ADD directive is to copy files and directories from the host build environment (context) directory, and 1 URL tagged file into the image.
The format is: ADD source path destination path
Such as:
#test
FROM ubuntu
MAINTAINER hello
ADD test1.txt test1.txt
ADD test1.txt test1.txt.bak
ADD test1.txt /mydir/
ADD data1 data1
ADD data2 data2
ADD zip.tar /myzip
There are the following notes:
1. If the source path is a file and the destination path ends in /, docker will treat the destination path as a directory and copy the source files to that directory.
If the target path does not exist, it is automatically created.
2. If the source path is a file and the destination path does not end in /, docker will treat the destination path as a file.
If the destination path does not exist, a file will be created with the name of the destination path.
If the target file is an existing file, it will be overridden with the source file, but only with the contents overridden. The file name will remain the target file name.
If the target file is actually an existing directory, the source file is copied to that directory. Note that in this case, it is best to display with/to avoid confusion.
3. If the source path is a directory and the target path does not exist, docker will automatically create a directory with the target path and copy in the files under the source path directory.
If the target path is an existing directory, docker copies files from the source path directory to that directory.
4. If the source file is an archive (compressed file), docker will unzip it automatically.
2. COPY instructions
The COPY directive and the ADD directive function and work in a similar way. It's just that the COPY directive doesn't do automatic decompression.