Dockerfile 多阶段构建
Dockerfile1 内容:
FROM alpine
RUN mkdir -pv /baimei-dashu && \
cp /etc/hosts /baimei-dashu
RUN touch /baimei-xixi/apps.log
RUN mkdir /baimei-haha
FROM alpine
RUN mkdir /baimei-xixi
CMD ["tail","-f","/etc/hosts"]
上边用了2个FROM , 请问 上边 最终的 目录有什么?
我们来执行一下:
build.sh 内容:
#!/bin/bash
docker build -t baimei-games:v3.$1 .
docker run -d baimei-games:v3.$1
docker ps -l
运行:
sh build.sh 1
运行容器后进入容器:
docker exec -it hopeful_zhukovsky sh
说明上述 dockfile 里的第一个FROM 第一阶段 创建的文件夹 失效, 只有第2个FROM 的才生效
Dockerfile2 内容:
# FROM alpine
FROM alpine AS haha
RUN mkdir -pv /baimei-dashu && \
cp /etc/hosts /baimei-dashu
RUN mkdir /baimei-haha
RUN dd if=/dev/zero of=/baimei-dashu/bigfile.log bs=1M count=100
# FROM alpine
FROM alpine AS xixi
RUN mkdir /baimei-xixi
RUN touch /baimei-xixi/apps.log
FROM alpine
RUN mkdir /baimei-hehe
# COPY --from=0 /baimei-dashu/hosts /baimei-hehe
# COPY --from=1 /baimei-xixi/apps.log /baimei-hehe
COPY --from=haha /baimei-dashu/hosts /baimei-hehe
COPY --from=xixi /baimei-xixi/apps.log /baimei-hehe
CMD ["tail","-f","/etc/hosts"]