Which instruction is used in Dockerfile to copy the files from remote URL to Docker container?

COPY and ADD are both Dockerfile instructions that serve similar purposes. They let you copy files from a specific location into a Docker image.

COPY

The COPY instruction copies new files or directories from and adds them to the filesystem of the container at the path .

COPY has two forms:

  • COPY [--chown=:]

When creating a Dockerfile, there are two commands that you can use to copy files/directories into it – ADD and COPY. Although there are slight differences in the scope of their function, they essentially perform the same task.

So, why do we have two commands, and how do we know when to use one or the other?

DOCKER ADD COMMAND

===

Let’s start by noting that the ADD command is older than COPY. Since the launch of the Docker platform, the ADD instruction has been part of its list of commands.

The command copies files/directories to a file system of the specified container.

The basic syntax for the ADD command is:

ADD

It includes the source you want to copy () followed by the destination where you want to store it (). If the source is a directory, ADD copies everything inside of it (including file system metadata).

For instance, if the file is locally available and you want to add it to the directory of an image, you type:

ADD /source/file/path /destination/path

ADD can also copy files from a URL. It can download an external file and copy it to the wanted destination. For example:

ADD http://source.file/url /destination/path

An additional feature is that it copies compressed files, automatically extracting the content to the given destination. This feature only applies to locally stored compressed files/directories.

ADD source.file.tar.gz /temp

Bear in mind that you cannot download and extract a compressed file/directory from a URL. The command does not unpack external packages when copying them to the local filesystem.

DOCKER COPY COMMAND

===

Due to some functionality issues, Docker had to introduce an additional command for duplicating content – COPY.

Unlike its closely related ADD command, COPY only has only one assigned function. Its role is to duplicate files/directories in a specified location in their existing format. This means that it doesn’t deal with extracting a compressed file, but rather copies it as-is.

The instruction can be used only for locally stored files. Therefore, you cannot use it with URLs to copy external files to your container.

To use the COPY instruction, follow the basic command format:

Type in the source and where you want the command to extract the content as follows:

COPY

For example:

COPY /source/file/path /destination/path

Which command to use? (Best Practice)

Considering the circumstances in which the COPY command was introduced, it is evident that keeping ADD was a matter of necessity. Docker released an official document outlining best practices for writing Dockerfiles, which explicitly advises against using the ADD command.

Docker’s official documentation notes that COPY should always be the go-to instruction as it is more transparent than ADD.

If you need to copy from the local build context into a container, stick to using COPY.

The Docker team also strongly discourages using ADD to download and copy a package from a URL. Instead, it’s safer and more efficient to use wget or curl within a RUN command. By doing so, you avoid creating an additional image layer and save space.

Ref: https://phoenixnap.com/kb/docker-add-vs-copy

It is the usual requirement of a developer to copy a few files into the docker image so that every container spawned from this image has the required files.

Docker allows us to do this using two isntructions in a Dockerfile:

  • ADD
  • COPY

ADD instruction

This lets us copy our files/directories from a source (lying on the local filesystem of base system or at a remote site) to the destination filesystem of the image. For copying files present at some remote site, provide URL in source field. Multiple sources can be specified.

ADD instruction takes the following format:

ADD [--chown=user:group] (source1) (source2) ... (destination) OR ADD [--chown=user:group] ("source"), ("source2"), ... ("destination")

The second form is preferred when paths contain whitespaces.

The option ‘chown’ lets us control the user and group for the destination filesystem. This option is applicable only to Linux containers.

If the destination path doesn’t exist on the base image, it is created along with all parent directories.

Usage:

ADD hello.py /root/home/hello.py

Pattern Matching in Source Names

Wildcards are used for creating patterns in source names.

Examples:

ADD myfile* /home/ #This would add all files starting with "myfile" - like myfile1.txt, myfile.py, myfilefinal.sh, etc.

ADD myfile?.txt /home/ #This would match only a single character after "myfile" - like myfile1.txt

Wildcard matching rules are in accordance with Go language, the one in which Docker tool was built.

Destination paths

Destination can be an absolute path or a path relative to WORKDIR.

Note: We can specify a working directory for the image using WORKDIR. This working directory corresponds to instructions like RUN, CMD, ENTRYPOINT, ADD and COPY.

Example:

Let the WORKDIR be /home

ADD test user/ # The file test will be added to /home/user/ ADD test /user/ # The file test will be added to /user/

chown flag

This is optional and applies only to Linux containers. In case this flag is not encountered, files/directories added to the image have UID and GID set to 0, that is their owner and group is set to ‘root’.

This option can take usernames or groupnames as strings or can even take their respective numeric values as UID or GID.

If groupname is skipped, GID is set same as UID, that is default group of the user specifies is used.

The usernames and the groupnames provided are matched against /etc/passwd and /etc/group files of the container filesystem. So, make sure that you add the necessary users using RUN instruction if they don’t exist by default.

If source is some remote URL, default permissions for it are 600.

Rule for ADD

1) Source files must be inside the context of the build. This is because the command ‘docker build’ first provides the context directory to the docker daemon and then initiates the build process.

2) If source is a directory, all its files and metadata is copied to destination, but not the directory itself.

3) If the destination has a trailing slash, it is considered a directory.

4) If the destination does not has a trailing slash, it is considered a file and the source’s contents are written on it.

5) In case of multiple sources, provide a directory as a destination (with a trailing slash)

6) In case the source is a local tar archive, it is decompressed and files are copied into the destination directory.

7) Tar archives at remote URL are not decompressed.

8) A source URL must be a non trivial reference to a file. Example: http://www.abc.com/hello.txt is allowed, but http://www.abc.com/ won’t work.

9) If STDIN method is used to pass a Dockerfile, there is no build context for this. So, we need to provide a remote URL as a source for this case.

ADD vs COPY

COPY is another method for performing similar work as ADD and obeys the same rules.

The difference between these two instructions lies as follows:

1) COPY can’t take remote URLs as source, whereas ADD can.

2) COPY can’t extract a tar file directly into the destination.

Best practice is to use COPY for copying local filesystem’s files, an operation which doesn’t require the extra magic of ADD. Also, when you need to keep the tar archive intact, use COPY instead of ADD.

Remote file downloading via ADD is also discouraged in best practices. It is recommended to use curl or wget commands using RUN instruction.

So, preferrably, ADD should be used only for auto-tar extraction capabilities.

Contributor

Prashansa Kulshrestha (https://github.com/Prashansa-K/)

How do I copy a file into a docker container?

Solution.
To copy a file from the local file system to a container, run the command for Docker container or Kubernetes pod, respectively: docker cp : ... .
To copy a file from the container to the local file system, use: docker cp : .

Which docker command is used to copy files in and out of the container?

Description. The docker cp utility copies the contents of SRC_PATH to the DEST_PATH . You can copy from the container's file system to the local machine or the reverse, from the local filesystem to the container.

Which Dockerfile instruction should be used to extract a tar file into another directory?

If you want to extract a TAR file inside a Docker Container or copy files from a URL or local directory, you can specify ADD Instructions inside your Dockerfile. This is different from COPY instruction because COPY instruction only allows you to copy files and directories from the local machine.

Which instruction in Dockerfile is used to execute a command with container?

When building a Dockerfile, the CMD instruction specifies the default program that will execute once the container runs. A quick point to note: CMD commands will only be utilized when command-line arguments are missing.