使用多级Dockerfile构建时COPY的问题 - 没有这样的文件或目录

时间:2018-04-27 19:33:09

标签: docker dockerfile

我试图将我的项目转换为使用多阶段构建。但是,最后一步总是失败并出现错误:

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

我的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
COPY --from=build /bin/grafana-server /bin/grafana-server

EXPOSE 3001

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

build.go build步骤会将工件输出到./bin/ - 除了告诉我文件不存在我认为它们应该存在的地方之外,这个错误非常无用。

我机器上的文件夹结构是:

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

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

~/Documents/dev/grafana/src/grafana/grafana我发出的地方:docker build -t grafana -f deploy/docker/Dockerfile .

1 个答案:

答案 0 :(得分:3)

要跟进我的评论,您使用WORKDIR设置的路径是绝对的,应该在COPY --from=build命令中以相同的方式指定。

所以这可能导致以下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"]

(仅部分测试)