akhy
akhy

Reputation: 5900

How to COPY/ADD file into current WORKDIR in Dockerfile

COPY/ADD statement requires 2 parameters. How can I add any file to current workdir that has been set in base image?

FROM company/app
COPY local.conf

Sure I can add WORKDIR statement before COPY to explicitly declare it. But that would be problematic if the workdir in company/app changes.

Upvotes: 29

Views: 33592

Answers (3)

Rotem jackoby
Rotem jackoby

Reputation: 22118

Addition to the accepted answer

The docs for COPY has a few use cases that worth extra attention.

One of them is relevant to our use case:

If multiple resources are specified, either directly or due to the use of a wildcard, then must be a directory, and it must end with a slash /.

So, for cases that more then one file is copied, the destination must be a directory and end with a /.

FROM company/app
WORKDIR app
COPY *.conf ./ # <-- Here

In case we'll write the last line has:

COPY *.conf .

We'll get the following error:

When using COPY with more than one source file, the destination must be a directory and end with a /

Upvotes: 8

akhy
akhy

Reputation: 5900

It turns out to be very simple. I just need to use dot to copy to current workdir.

COPY local.conf .

Still cannot figure out if this has some gotchas. But it just work as intended.

Upvotes: 41

VonC
VonC

Reputation: 1324148

But that would be problematic if the workdir in company/app changes.

Then you would need to pass that workdir as build-time parameter in order to be able to change it from one docker build to the next.
See docker build --build-arg

You would need first to docker inspect company/app (inspec the image) to see if there are any changes.

Upvotes: 1

Related Questions