Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with COPY when using multistage Dockerfile builds -- no such file or directory

I'm trying to convert my project to use multi-stage builds. However, the final step always fails with an error:

Step 11/13 : COPY --from=build /bin/grafana-server /bin/grafana-server
COPY failed: stat /var/lib/docker/overlay2/xxxx/merged/bin/grafana-server: no such file or directory

My Dockerfile looks like this:

FROM golang:latest AS build

ENV SRC_DIR=/go/src/github.com/grafana/grafana/
ENV GIT_SSL_NO_VERIFY=1

COPY . $SRC_DIR
WORKDIR $SRC_DIR

# Building of Grafana
RUN \
  npm run build && \
  go run build.go setup && \
  go run build.go build

# Create final stage containing only required artifacts
FROM scratch
COPY --from=build /bin/grafana-server /bin/grafana-server

EXPOSE 3001

CMD ["./bin/grafana-server"]

The build.go build step will output artifacts to ./bin/ -- The error is pretty unhelpful other than telling me the files don't exist where I think they should exist.

My folder structure on my machine is:

--| ~/Documents/dev/grafana/src/grafana/grafana
--------| bin
------------| <grafan-server builds to here>

--------| deploy
------------| docker
----------------| Dockerfile

From ~/Documents/dev/grafana/src/grafana/grafana is where I issue: docker build -t grafana -f deploy/docker/Dockerfile .

like image 610
MrDuk Avatar asked Apr 27 '18 19:04

MrDuk


1 Answers

To follow-up my comment, the path you set with the WORKDIR is absolute and should be specified in the same way in the COPY --from=build command.

So this could lead to the following Dockerfile:

FROM golang:latest AS build

ENV SRC_DIR=/go/src/github.com/grafana/grafana/
ENV GIT_SSL_NO_VERIFY=1

COPY . $SRC_DIR
WORKDIR $SRC_DIR

# Building of Grafana
RUN \
  npm run build && \
  go run build.go setup && \
  go run build.go build

# Create final stage containing only required artifacts
FROM scratch

ENV SRC_DIR=/go/src/github.com/grafana/grafana/
WORKDIR $SRC_DIR

COPY --from=build ${SRC_DIR}/bin/grafana-server ${SRC_DIR}/bin/grafana-server

EXPOSE 3001

CMD ["./bin/grafana-server"]

(only partially tested)

like image 86
ErikMD Avatar answered Nov 15 '22 06:11

ErikMD