Docker 技术入门与实战
  • 前言
  • 修订记录
  • 如何贡献
  • Docker 简介
    • 什么是 Docker
    • 为什么要用 Docker
  • 基本概念
    • 镜像
    • 容器
    • 仓库
  • 安装 Docker
    • Ubuntu
    • Debian
    • Fedora
    • CentOS
    • Raspberry Pi
    • Linux 离线安装
    • macOS
    • Windows 10
    • 镜像加速器
    • 开启实验特性
  • 使用镜像
    • 获取镜像
    • 列出镜像
    • 删除本地镜像
    • 利用 commit 理解镜像构成
    • 使用 Dockerfile 定制镜像
    • Dockerfile 指令详解
      • COPY 复制文件
      • ADD 更高级的复制文件
      • CMD 容器启动命令
      • ENTRYPOINT 入口点
      • ENV 设置环境变量
      • ARG 构建参数
      • VOLUME 定义匿名卷
      • EXPOSE 暴露端口
      • WORKDIR 指定工作目录
      • USER 指定当前用户
      • HEALTHCHECK 健康检查
      • ONBUILD 为他人作嫁衣裳
      • LABEL 为镜像添加元数据
      • SHELL 指令
      • 参考文档
    • Dockerfile 多阶段构建
      • 实战多阶段构建 Laravel 镜像
    • 构建多种系统架构支持的 Docker 镜像
    • 其它制作镜像的方式
    • 实现原理
  • 操作容器
    • 启动
    • 守护态运行
    • 终止
    • 进入容器
    • 导出和导入
    • 删除
  • 访问仓库
    • Docker Hub
    • 私有仓库
    • 私有仓库高级配置
    • Nexus 3
  • 数据管理
    • 数据卷
    • 挂载主机目录
  • 使用网络
    • 外部访问容器
    • 容器互联
    • 配置 DNS
  • 高级网络配置
    • 快速配置指南
    • 容器访问控制
    • 端口映射实现
    • 配置 docker0 网桥
    • 自定义网桥
    • 工具和示例
    • 编辑网络配置文件
    • 实例:创建一个点到点连接
  • Docker Buildx
    • BuildKit
    • 使用 buildx 构建镜像
    • 使用 buildx 构建多种系统架构支持的 Docker 镜像
  • Docker Compose
    • 简介
    • Compose v2
    • 安装与卸载
    • 使用
    • 命令说明
    • Compose 模板文件
    • 实战 Django
    • 实战 Rails
    • 实战 WordPress
    • 实战 LNMP
  • Swarm mode
    • 基本概念
    • 创建 Swarm 集群
    • 部署服务
    • 使用 compose 文件
    • 管理密钥
    • 管理配置信息
    • 滚动升级
  • 安全
    • 内核命名空间
    • 控制组
    • 服务端防护
    • 内核能力机制
    • 其它安全特性
    • 总结
  • 底层实现
    • 基本架构
    • 命名空间
    • 控制组
    • 联合文件系统
    • 容器格式
    • 网络
  • Etcd 项目
    • 简介
    • 安装
    • 集群
    • 使用 etcdctl
  • Fedora CoreOS
    • 简介
    • 安装
  • Kubernetes - 开源容器编排引擎
    • 简介
    • 基本概念
    • 架构设计
  • 部署 Kubernetes
    • 使用 kubeadm 部署 kubernetes(CRI 使用 containerd)
    • 在 Docker Desktop 使用
    • 一步步部署 kubernetes 集群
    • 部署 Dashboard
  • Kubernetes 命令行 kubectl
  • 容器与云计算
    • 简介
    • 腾讯云
    • 阿里云
    • 亚马逊云
    • 小结
  • 实战案例 - 操作系统
    • Busybox
    • Alpine
    • Debian Ubuntu
    • CentOS Fedora
    • 本章小结
  • 实战案例 - CI/CD
    • GitHub Actions
    • Drone
      • 部署 Drone
  • 在 IDE 中使用 Docker
    • VS Code
  • podman - 下一代 Linux 容器工具
  • 附录
    • 附录一:常见问题总结
    • 附录二:热门镜像介绍
      • Ubuntu
      • CentOS
      • Nginx
      • PHP
      • Node.js
      • MySQL
      • WordPress
      • MongoDB
      • Redis
      • Minio
    • 附录三:Docker 命令查询
      • 客户端命令 - docker
      • 服务端命令 - dockerd
    • 附录四:Dockerfile 最佳实践
    • 附录五:如何调试 Docker
    • 附录六:资源链接
Powered by GitBook
On this page
  • 之前的做法
  • 全部放入一个 Dockerfile
  • 分散到多个 Dockerfile
  • 使用多阶段构建
  • 只构建某一阶段的镜像
  • 构建时从其他镜像复制文件
  1. 使用镜像

Dockerfile 多阶段构建

之前的做法

在 Docker 17.05 版本之前,我们构建 Docker 镜像时,通常会采用两种方式:

全部放入一个 Dockerfile

一种方式是将所有的构建过程编包含在一个 Dockerfile 中,包括项目及其依赖库的编译、测试、打包等流程,这里可能会带来的一些问题:

  • 镜像层次多,镜像体积较大,部署时间变长

  • 源代码存在泄露的风险

例如,编写 app.go 文件,该程序输出 Hello World!

package main

import "fmt"

func main(){
    fmt.Printf("Hello World!");
}

编写 Dockerfile.one 文件

FROM golang:alpine

RUN apk --no-cache add git ca-certificates

WORKDIR /go/src/github.com/go/helloworld/

COPY app.go .

RUN go get -d -v github.com/go-sql-driver/mysql \
  && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app . \
  && cp /go/src/github.com/go/helloworld/app /root

WORKDIR /root/

CMD ["./app"]

构建镜像

$ docker build -t go/helloworld:1 -f Dockerfile.one .

分散到多个 Dockerfile

另一种方式,就是我们事先在一个 Dockerfile 将项目及其依赖库编译测试打包好后,再将其拷贝到运行环境中,这种方式需要我们编写两个 Dockerfile 和一些编译脚本才能将其两个阶段自动整合起来,这种方式虽然可以很好地规避第一种方式存在的风险,但明显部署过程较复杂。

例如,编写 Dockerfile.build 文件

FROM golang:alpine

RUN apk --no-cache add git

WORKDIR /go/src/github.com/go/helloworld

COPY app.go .

RUN go get -d -v github.com/go-sql-driver/mysql \
  && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

编写 Dockerfile.copy 文件

FROM alpine:latest

RUN apk --no-cache add ca-certificates

WORKDIR /root/

COPY app .

CMD ["./app"]

新建 build.sh

#!/bin/sh
echo Building go/helloworld:build

docker build -t go/helloworld:build . -f Dockerfile.build

docker create --name extract go/helloworld:build
docker cp extract:/go/src/github.com/go/helloworld/app ./app
docker rm -f extract

echo Building go/helloworld:2

docker build --no-cache -t go/helloworld:2 . -f Dockerfile.copy
rm ./app

现在运行脚本即可构建镜像

$ chmod +x build.sh

$ ./build.sh

对比两种方式生成的镜像大小

$ docker image ls

REPOSITORY      TAG    IMAGE ID        CREATED         SIZE
go/helloworld   2      f7cf3465432c    22 seconds ago  6.47MB
go/helloworld   1      f55d3e16affc    2 minutes ago   295MB

使用多阶段构建

为解决以上问题,Docker v17.05 开始支持多阶段构建 (multistage builds)。使用多阶段构建我们就可以很容易解决前面提到的问题,并且只需要编写一个 Dockerfile:

例如,编写 Dockerfile 文件

FROM golang:alpine as builder

RUN apk --no-cache add git

WORKDIR /go/src/github.com/go/helloworld/

RUN go get -d -v github.com/go-sql-driver/mysql

COPY app.go .

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .

FROM alpine:latest as prod

RUN apk --no-cache add ca-certificates

WORKDIR /root/

COPY --from=0 /go/src/github.com/go/helloworld/app .

CMD ["./app"]

构建镜像

$ docker build -t go/helloworld:3 .

对比三个镜像大小

$ docker image ls

REPOSITORY        TAG   IMAGE ID         CREATED            SIZE
go/helloworld     3     d6911ed9c846     7 seconds ago      6.47MB
go/helloworld     2     f7cf3465432c     22 seconds ago     6.47MB
go/helloworld     1     f55d3e16affc     2 minutes ago      295MB

很明显使用多阶段构建的镜像体积小,同时也完美解决了上边提到的问题。

只构建某一阶段的镜像

我们可以使用 as 来为某一阶段命名,例如

FROM golang:alpine as builder

例如当我们只想构建 builder 阶段的镜像时,增加 --target=builder 参数即可

$ docker build --target builder -t username/imagename:tag .

构建时从其他镜像复制文件

上面例子中我们使用 COPY --from=0 /go/src/github.com/go/helloworld/app . 从上一阶段的镜像中复制文件,我们也可以复制任意镜像中的文件。

$ COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf
Previous参考文档Next实战多阶段构建 Laravel 镜像

Last updated 3 years ago