loading...

Docker – The ADD instruction

How to set a static IP address on Windows Server 2019

The ADD instruction is used to copy files and folders into the Docker image being built. The syntax for the ADD instruction is as follows:

# ADD instruction syntax
ADD [--chown=<user>:<group>] <src>... <dest>
# Use double quotes for paths containing whitespace)
ADD [--chown=<user>:<group>] ["<src>",... "<dest>"]

About now, you are thinking that the ADD instruction seems to be just like the COPY instruction that we just reviewed. Well, you are not wrong. Basically, all of the things we saw the COPY instruction do, the ADD instruction can do as well. It uses the same syntax as the COPY instruction and the effects of WORKDIR instructions are the same between the two. So, why do we have two commands that do the same thing?

The difference between COPY and ADD

The answer is that the ADD instruction can actually do more than the COPY instruction. The more is dependent upon the values used for the source input. With the COPY instruction, the source can be files or folders. However, with the ADD instruction, the source can be files, folders, a local .tar file, or a URL.

When the ADD instruction has a source value that is a .tar file, the contents of that TAR file are extracted into a corresponding folder inside the image.

When you use a .tar file as the source in an ADD instruction and include the --chown parameter, you might expect the owner and group in the image to be set on the files extracted from the archive. This is currently not the way it works. Unfortunately, the owner, group, and permissions on the extracted contents will match what is contained within the archive in spite of the --chown parameter. When you use a .tar file, you will probably want to include RUN chown -R X:X after the ADD.

As mentioned, the ADD instruction can use a URL as the source value. Here is an example Dockerfile that includes an ADD instruction using a URL:

# ADD instruction Dockerfile for Docker Quick Start
FROM alpine
LABEL maintainer="Earl Waud <earlwaud@mycompany.com>"
LABEL version=3.0
ADD https://github.com/docker-library/hello-world/raw/master/amd64/hello-world/hello /
RUN chmod +x /hello
CMD ["/hello"]

While using a URL in an ADD instruction works, downloading the file into the image, this feature is not recommended, even by Docker. Here is what the Docker documentation has to say about using ADD:

So, generally speaking, whenever you can get the desired content into the image using a COPY instruction, then you should choose to use COPY instead of ADD.

Comments are closed.

loading...