我的多阶段构建在需要复制时找不到/assets/css
目录。我需要更改什么?
码头工人版本
版本 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 引擎找不到它?
这是问题所在:
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
(单独运行)