Steve Martin
Steve Martin

Reputation: 103

Dockerfile COPY creates undesired subdirectory in image

I want to create a Docker container and I wrote image. Everything works great except the COPY command where I got confused. My Dockerfile:

RUN HOME=/home/ros rosdep update

RUN mkdir -p /home/ros/workspace

# Copy the files
COPY $PWD/src/a_file /home/ros/workspace/src
COPY $PWD/src/b_file /home/ros/workspace/src

a_file is a directory like a b_file. When I try to copy these directories into a newly created directory called /home/ros/workspace/src I want a_file and b_file to be both inside /home/ros/workspace/src. Instead of this, I get another src directory /home/ros/workspace/src/src) and the contents of a_file and b_file are inside that directory.

What am I doing wrong?

Upvotes: 3

Views: 3044

Answers (2)

Mornor
Mornor

Reputation: 3783


As mentioned in other answers, $PWDrefers to the image context.
Try to use . instead.

To setup your working directory, use WORKDIR

Also, both a_file and b_file are in src/

All in all, this should work (not tested):

FROM <your-base-image>
WORKDIR /home/ros

RUN rosdep update
RUN mkdir -p workspace

# Copy the files
COPY ./src workspace/src

Upvotes: 3

Gonzalo Matheu
Gonzalo Matheu

Reputation: 10064

In your Dockerfile, PWD variable refers to image context (i.e: inside the image).

From COPY documentation:

paths of files and directories will be interpreted as relative to the source of the context of the build.

If src directories are in the root of your build context, your example it will be:

...
COPY src/a_file /home/ros/workspace/src
COPY src/b_file /home/ros/workspace/src
...

Upvotes: 1

Related Questions