以下主要通过sysbench镜像构建来说明一下dockerfile的多阶段构建流程
准备工作
wget https://github.com/akopytov/sysbench/archive/refs/tags/1.0.20.tar.gz
mkdir sysbench
tar -zxf 1.0.20.tar.gz -C sysbench
cd sysbench
# 部分镜像如alpine没有bash,修改autogen.sh的首行,将bash改为sh
vim sysbench-1.0.20/autogen.sh
#!/usr/bin/env bash -> #!/usr/bin/env sh
vim Dockerfile
docker build -t sysbench:1.0.20 .
Dockerfile
FROM alpine:3.13
WORKDIR /sysbench
COPY sysbench-1.0.20/ /sysbench
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && apk update && apk add automake libtool autoconf pkgconfig make gcc g++ mysql-dev && cd /sysbench && ./autogen.sh && ./configure --prefix /sysbench && make -j && make install
FROM alpine:3.13
COPY --from=0 /sysbench/share /sysbench/share
COPY --from=0 /sysbench/bin/ /bin
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && apk update && apk add mysql-dev
CMD ["/bin/sh"]