COPY in dockerfile

I am new to docker and i am wonder what this line COPY ./flag.txt /flag.txt would mean. I saw on some other posts that ./ means to copy to a directory but what would the last /flag.txt mean?

It’s good practive to take a look at the documentation first. This is the introduction text to the COPY instruction:

COPY

COPY has two forms. The latter form is required for paths containing whitespace.

COPY [OPTIONS] <src> ... <dest>
COPY [OPTIONS] ["<src>", ... "<dest>"]

The available [OPTIONS] are:

Option Minimum Dockerfile version
--from
--chown
--chmod 1.2
--link 1.4
--parents 1.7-labs
--exclude 1.7-labs

Thus, the last filename is the absolute path to the target path in the image. A container created from that image will have the file at the same location.

The source path must be within your build context, e.g. your Dockerfile is in the current folder and you run docker build -t myrepo:myversion ., then the build context is the current folder. You can only COPY files within the build context into the image.

Update: I forgot to mention, that if the source ends with a / a directory will be copied, if a file is specified, then only a single file will be copied into the image. ./ means in the current directory (inside the build context)