多阶段构建 - 没有这样的文件或目录

时间:2017-07-14 17:50:50

标签: docker alpine

我的多阶段构建在复制它时没有找到/assets/css目录。我需要改变什么?

Docker版本

版本17.06.0-ce-mac17(18432)

频道:边缘

4bb7a7dfa0

myimage:sass我在多阶段构建中使用的图像

FROM ruby:2.4.1-slim

RUN mkdir /assets
VOLUME /assets

RUN gem install sass
ENTRYPOINT ["sass"]

多阶段构建Dockerfile

请注意我在第一张图片cd /assets/css && ls && pwd上运行的调试命令,结果会在构建阶段显示。

# Compile Sass
FROM myimage/sass AS builder
COPY app/assets/sass /assets/sass
RUN sass --update --force --sourcemap=none --stop-on-error /assets/sass:/assets/css &&\
  # sass directory isn't needed
  rm -r assets/sass &&\
  # debugging: check /assets/css exists inside the container
  cd /assets/css && ls && pwd

FROM alpine:3.6
WORKDIR /app
RUN mkdir /app/logs
VOLUME ["/app/logs"]
COPY --from=builder /assets/css /app/assets/css
EXPOSE 80
CMD ["./bin/bash"]
Raw

docker build -t myimage:css .

的输出

请注意第3/11页的cd /assets/css && ls && pwd输出,显示/assets/css目录存在且内部有main.css个文件

Step 1/11 : FROM myimage:sass AS builder
 ---> 7c6662186d55
Step 2/11 : COPY app/assets/sass /assets/sass
 ---> 76b5d86846b8
Removing intermediate container ee74d16617b4
Step 3/11 : RUN sass --update --force --sourcemap=none --stop-on-error /assets/sass:/assets/css &&  rm -r assets/sass &&  cd /assets/css && pwd
 ---> Running in 83dc591edc5c
  directory /assets/css
      write /assets/css/main.css
main.css
/assets/css
 ---> 3939f46fb355
Removing intermediate container 83dc591edc5c
Step 4/11 : FROM alpine:3.6
 ---> 7328f6f8b418
Step 5/11 : WORKDIR /app
 ---> 19ad596f9fc1
Removing intermediate container 790fac2040f1
Step 6/11 : RUN mkdir /app/logs
 ---> Running in cb66151a4694
 ---> 18d6c4970d04
Removing intermediate container cb66151a4694
Step 7/11 : VOLUME /app/logs
 ---> Running in b8a98a38a054
 ---> fa68603ccf30
Removing intermediate container b8a98a38a054
Step 8/11 : COPY --from=builder /assets/css /app/assets/css
COPY failed: stat /var/lib/docker/aufs/mnt/0ddcc250ed9c4eb1de46305e62cb2303274b027b2c9b0ddd09471fce3c3ed619/assets/css: no such file or directory

那么为什么不能将/assets/css复制到最终图像中?为什么Docker引擎无法找到它?

1 个答案:

答案 0 :(得分:3)

这是问题所在:

VOLUME /assets

每个构建步骤都是一个新的运行容器。每个新容器都会丢弃未命名卷中的任何内容。

用以下方法检查:

RUN sass --update --force --sourcemap=none --stop-on-error /assets/sass:/assets/css &&\
  # sass directory isn't needed
  rm -r assets/sass
RUN cd /assets/css && ls && pwd

(单独运行)