loading...

Docker – The COPY instruction

How to create virtual machines with VirtualBox

You have already seen an example of using the COPY instruction in the hello-world Dockerfile shown in The FROM instruction section. The COPY instruction is used to copy files and folders into the Docker image being built. The syntax for the COPY instruction is as follows:

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

Note that the --chown parameter is only valid for Linux-based containers. Without the --chown parameter, the owner ID and group ID will both be set to 0.

The <src> or source is a filename or folder path and is interpreted to be relative to the context of the build. We will talk more about the build context later in this chapter, but for now, think of it as where the build command is run. The source may include wildcards.

The <dest> or destination is a filename or path inside of the image being created. The destination is relative to the root of the image’s filesystem unless there is a preceding WORKDIR instruction. We will discuss the WORKDIR instruction later, but for now, just think of it as a way to set the current working directory. When the COPY command comes after a WORKDIR instruction in a Dockerfile, the file or folders being copied into the image will be placed in the destination relative to the current working directory. If the destination includes a path with one or more folders, all of the folders will be created if they don’t already exist.

In our earlier hello-world Dockerfile example, you saw a COPY instruction that copied an executable file, named hello, into the image at the filesystem’s root location. It looked like this: COPY hello /. That is about as basic a COPY instruction as can be used. Here are some other examples:

# COPY instruction Dockerfile for Docker Quick Start
FROM alpine:latest
LABEL maintainer="Earl Waud <earlwaud@mycompany.com>"
LABEL version=1.0
# copy multiple files, creating the path "/theqsg/files" in the process
COPY file* theqsg/files/
# copy all of the contents of folder "folder1" to "/theqsg/" 
# (but not the folder "folder1" itself)
COPY folder1 theqsg/
# change the current working directory in the image to "/theqsg"
WORKDIR theqsg
# copy the file special1 into "/theqsg/special-files/"
COPY --chown=35:35 special1 special-files/
# return the current working directory to "/"
WORKDIR /
CMD ["sh"]

We can see what the resulting image’s filesystem would get using the preceding Dockerfile by running a container from the image, and executing an ls command, which would look like this:

You can see that folders specified in the destination path were created during the COPY. You will also notice that providing the --chown parameter sets the owner and group on the destination files. An important distinction is that when the source is a folder, the contents of the folder are copied but not the folder itself. Notice that using a WORKDIR instruction changes the path in the image filesystem and following COPY instructions will now be relative to the new current working directory. In this example, we returned the current working directory to / so that commands executed in containers will run relative to /.

Comments are closed.

loading...