Search code examples
dockerdockerfilecontainers

Dockerfile: COPY does not work (even tho he can find the file)


I am trying to build a docker image.

I want to pull files from gitlab and copy them to another directory.

Here is the relevant part of my Dockerfile:

ENV RALPH_LOCAL_DIR="/var/local/ralph"
ENV RALPH_IMAGE_TMP_DIR="/tmp"

RUN mkdir -p $RALPH_LOCAL_DIR
RUN cd $RALPH_LOCAL_DIR
WORKDIR $RALPH_LOCAL_DIR
RUN git clone <OMITTED_FOR_THIS_POST>
WORKDIR project-ralph/ralph

COPY README.md $RALPH_IMAGE_TMP_DIR/

I get this error:

COPY failed: stat /var/lib/docker/tmp/docker-builder094767244/README.md: no such file or directory

So the copying fails. But I can list the file in the container with ls. So if I run RUN ls -la README.md he can find the file. So why can't he copy the file?


Solution

  • COPY copies the file from the host system. You need to run git clone there, before you start running Docker operations. (This also simplifies the case where you need credentials of some sort to run git clone: getting an ssh private key into an image to run git clone, without leaking it into the final image, is kind of tricky, and you don't really want to be doing kind of tricky things with ssh keys.)

    FROM ...
    WORKDIR /var/local/ralph
    COPY README.md .
    
    git clone <OMITTED_FOR_THIS_POST>
    docker build -t ... .
    

    If the file is already in the image, as in your existing Dockerfile, then you need to RUN cp or RUN mv to put it somewhere else.

    RUN cp README.md /tmp