A. K.
A. K.

Reputation: 311

Docker not adding existing file

I have a simple setup that clones Git repo and compiles a binary and then copies it to the container

Dockerfile:

FROM ubuntu:16.04
RUN apt-get update && apt-get install -y <...>

RUN git clone <REPO>

WORKDIR <REPO>/build
RUN cmake ..
RUN make install

RUN ls bin
ADD bin/binaryname /

ENTRYPOINT <...>

Docker output:

[100%] Linking CXX executable bin/binaryname
...
 ---> 4a9c6bb40214
Removing intermediate container 00bf96c84f37
Step 7 : RUN ls bin
 ---> Running in c295c1fca462
binaryname
 ---> a418c1bfa6cd
Removing intermediate container c295c1fca462
Step 8 : ADD bin/binaryname /
lstat bin/binaryname: no such file or directory

So basically docker sees that file but won't add it, why?

Upvotes: 0

Views: 836

Answers (2)

BMitch
BMitch

Reputation: 264681

If you need the binary on the root of the filesystem instead of under <REPO>/build/bin, then do a copy as a RUN command. As Mano mentions, the ADD and COPY will copy contents from the context you send to the build host (usually the . directory at the end of your docker build . command). But what you're trying to do is copy from one directory inside the container to another. That would look like:

FROM ubuntu:16.04
RUN apt-get update && apt-get install -y <...>

RUN git clone <REPO>

WORKDIR <REPO>/build
RUN cmake ..
RUN make install \
 && cp bin/binaryname /

ENTRYPOINT <...>

Upvotes: 1

Mano Marks
Mano Marks

Reputation: 8819

Remove the ADD directive and it should work. Here's why:

All the RUN directives happen in the image that is being built, not on your local machine. The ADD directive tries to copy a file from your host file system to the image. Since you built the file bin/binaryname in the image, it doesn't exist at bin/binaryname on your host. It is already there in the image because that's where it was built.

Upvotes: 3

Related Questions