mirror of
https://github.com/yeasy/docker_practice.git
synced 2025-08-02 14:01:49 +00:00
@@ -1,178 +0,0 @@
|
||||
## 使用 `BuildKit` 构建镜像
|
||||
|
||||
**BuildKit** 是下一代的镜像构建组件,在 https://github.com/moby/buildkit 开源。
|
||||
|
||||
**注意:如果您的镜像构建使用的是云服务商提供的镜像构建服务(Docker Hub 自动构建、腾讯云容器服务、阿里云容器服务等),由于上述服务提供商的 Docker 版本低于 18.09,BuildKit 无法使用,将造成镜像构建失败。建议使用 BuildKit 构建镜像时使用一个新的 Dockerfile 文件(例如 Dockerfile.buildkit)**
|
||||
|
||||
**注意:docker-compose build 命令暂时不支持 BuildKit**
|
||||
|
||||
下面介绍如何在 Docker CE 18.09 版本中使用 `BuildKit` 提供的 `Dockerfile` 新指令来更快、更安全的构建 Docker 镜像。
|
||||
|
||||
### 启用 `BuildKit`
|
||||
|
||||
启用 `BuildKit` 必须先设置 **环境变量**。
|
||||
|
||||
Linux、macOS 执行如下命令:
|
||||
|
||||
```bash
|
||||
$ export DOCKER_BUILDKIT=1
|
||||
```
|
||||
|
||||
Windows 执行如下命令:
|
||||
|
||||
```powershell
|
||||
$ set $env:DOCKER_BUILDKIT=1
|
||||
```
|
||||
|
||||
> 以上是设置环境变量的临时方法,若使环境变量永久生效请读者自行设置。
|
||||
|
||||
### `Dockerfile` 新增指令详解
|
||||
|
||||
启用 `BuildKit` 之后,我们可以使用下面几个新的指令来加快镜像构建。
|
||||
|
||||
#### `RUN --mount=type=cache`
|
||||
|
||||
目前,几乎所有的程序都会使用依赖管理工具,例如 `Go` 中的 `go mod`、`Node.js` 中的 `npm` 等等,当我们构建一个镜像时,往往会重复的从互联网中获取依赖包,难以缓存,大大降低了镜像的构建效率。
|
||||
|
||||
例如一个前端工程需要用到 `npm`:
|
||||
|
||||
```docker
|
||||
FROM node:alpine as builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json /app/
|
||||
|
||||
RUN npm i --registry=https://registry.npm.taobao.org \
|
||||
&& rm -rf ~/.npm
|
||||
|
||||
COPY src /app/src
|
||||
|
||||
RUN npm run build
|
||||
|
||||
FROM nginx:alpine
|
||||
|
||||
COPY --from=builder /app/dist /app/dist
|
||||
```
|
||||
|
||||
使用多阶段构建,构建的镜像中只包含了目标文件夹 `dist`,但仍然存在一些问题,当 `package.json` 文件变动时,`RUN npm i && rm -rf ~/.npm` 这一层会重新执行,变更多次后,生成了大量的中间层镜像。
|
||||
|
||||
为解决这个问题,进一步的我们可以设想一个类似 **数据卷** 的功能,在镜像构建时把 `node_modules` 文件夹挂载上去,在构建完成后,这个 `node_modules` 文件夹会自动卸载,实际的镜像中并不包含 `node_modules` 这个文件夹,这样我们就省去了每次获取依赖的时间,大大增加了镜像构建效率,同时也避免了生成了大量的中间层镜像。
|
||||
|
||||
`BuildKit` 提供了 `RUN --mount=type=cache` 指令,可以实现上边的设想。
|
||||
|
||||
```docker
|
||||
# syntax = docker/dockerfile:experimental
|
||||
FROM node:alpine as builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json /app/
|
||||
|
||||
RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
|
||||
--mount=type=cache,target=/root/.npm,id=npm_cache \
|
||||
npm i --registry=https://registry.npm.taobao.org
|
||||
|
||||
COPY src /app/src
|
||||
|
||||
RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
|
||||
# --mount=type=cache,target=/app/dist,id=my_app_dist,sharing=locked \
|
||||
npm run build
|
||||
|
||||
FROM nginx:alpine
|
||||
|
||||
# COPY --from=builder /app/dist /app/dist
|
||||
|
||||
# 为了更直观的说明 from 和 source 指令,这里使用 RUN 指令
|
||||
RUN --mount=type=cache,target=/tmp/dist,from=builder,source=/app/dist \
|
||||
# --mount=type=cache,target/tmp/dist,from=my_app_dist,sharing=locked \
|
||||
mkdir -p /app/dist && cp -r /tmp/dist/* /app/dist
|
||||
```
|
||||
|
||||
**由于 `BuildKit` 为实验特性,每个 `Dockerfile` 文件开头都必须加上如下指令**
|
||||
|
||||
```docker
|
||||
# syntax = docker/dockerfile:experimental
|
||||
```
|
||||
|
||||
第一个 `RUN` 指令执行后,`id` 为 `my_app_npm_module` 的缓存文件夹挂载到了 `/app/node_modules` 文件夹中。多次执行也不会产生多个中间层镜像。
|
||||
|
||||
第二个 `RUN` 指令执行时需要用到 `node_modules` 文件夹,`node_modules` 已经挂载,命令也可以正确执行。
|
||||
|
||||
第三个 `RUN` 指令将上一阶段产生的文件复制到指定位置,`from` 指明缓存的来源,这里 `builder` 表示缓存来源于构建的第一阶段,`source` 指明缓存来源的文件夹。
|
||||
|
||||
上面的 `Dockerfile` 中 `--mount=type=cache,...` 中指令作用如下:
|
||||
|
||||
|Option |Description|
|
||||
|---------------------|-----------|
|
||||
|`id` | `id` 设置一个标志,以便区分缓存。|
|
||||
|`target` (必填项) | 缓存的挂载目标文件夹。|
|
||||
|`ro`,`readonly` | 只读,缓存文件夹不能被写入。 |
|
||||
|`sharing` | 有 `shared` `private` `locked` 值可供选择。`sharing` 设置当一个缓存被多次使用时的表现,由于 `BuildKit` 支持并行构建,当多个步骤使用同一缓存时(同一 `id`)会发生冲突。`shared` 表示多个步骤可以同时读写,`private` 表示当多个步骤使用同一缓存时,每个步骤使用不同的缓存,`locked` 表示当一个步骤完成释放缓存后,后一个步骤才能继续使用该缓存。|
|
||||
|`from` | 缓存来源(构建阶段),不填写时为空文件夹。|
|
||||
|`source` | 来源的文件夹路径。|
|
||||
|
||||
#### `RUN --mount=type=bind`
|
||||
|
||||
该指令可以将一个镜像(或上一构建阶段)的文件挂载到指定位置。
|
||||
|
||||
```docker
|
||||
# syntax = docker/dockerfile:experimental
|
||||
RUN --mount=type=bind,from=php:alpine,source=/usr/local/bin/docker-php-entrypoint,target=/docker-php-entrypoint \
|
||||
cat /docker-php-entrypoint
|
||||
```
|
||||
|
||||
#### `RUN --mount=type=tmpfs`
|
||||
|
||||
该指令可以将一个 `tmpfs` 文件系统挂载到指定位置。
|
||||
|
||||
```docker
|
||||
# syntax = docker/dockerfile:experimental
|
||||
RUN --mount=type=tmpfs,target=/temp \
|
||||
mount | grep /temp
|
||||
```
|
||||
|
||||
#### `RUN --mount=type=secret`
|
||||
|
||||
该指令可以将一个文件挂载到指定位置。
|
||||
|
||||
```docker
|
||||
# syntax = docker/dockerfile:experimental
|
||||
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
|
||||
cat /root/.aws/credentials
|
||||
```
|
||||
|
||||
```bash
|
||||
$ docker build -t test --secret id=aws,src=$HOME/.aws/credentials .
|
||||
```
|
||||
|
||||
#### `RUN --mount=type=ssh`
|
||||
|
||||
该指令可以挂载 `ssh` 密钥。
|
||||
|
||||
```docker
|
||||
# syntax = docker/dockerfile:experimental
|
||||
FROM alpine
|
||||
RUN apk add --no-cache openssh-client
|
||||
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
|
||||
RUN --mount=type=ssh ssh git@gitlab.com | tee /hello
|
||||
```
|
||||
|
||||
```bash
|
||||
$ eval $(ssh-agent)
|
||||
$ ssh-add ~/.ssh/id_rsa
|
||||
(Input your passphrase here)
|
||||
$ docker build -t test --ssh default=$SSH_AUTH_SOCK .
|
||||
```
|
||||
|
||||
### 清理构建缓存
|
||||
|
||||
执行以下命令清理构建缓存
|
||||
|
||||
```bash
|
||||
$ docker builder prune
|
||||
```
|
||||
|
||||
### 官方文档
|
||||
|
||||
* https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md
|
148
image/buildx.md
148
image/buildx.md
@@ -1,148 +0,0 @@
|
||||
## 使用 `Buildx` 构建镜像
|
||||
|
||||
**BuildKit** 是下一代的镜像构建组件,在 https://github.com/moby/buildkit 开源。
|
||||
|
||||
**注意:如果您的镜像构建使用的是云服务商提供的镜像构建服务(Docker Hub 自动构建、腾讯云容器服务、阿里云容器服务等),由于上述服务提供商的 Docker 版本低于 18.09,BuildKit 无法使用,将造成镜像构建失败。建议使用 BuildKit 构建镜像时使用一个新的 Dockerfile 文件(例如 Dockerfile.buildkit)**
|
||||
|
||||
下面介绍如何在 Docker CE 19.03+ 版本中使用 `BuildKit` 提供的 `Dockerfile` 新指令来更快、更安全的构建 Docker 镜像。
|
||||
|
||||
### `Dockerfile` 新增指令详解
|
||||
|
||||
启用 `BuildKit` 之后,我们可以使用下面几个新的指令来加快镜像构建。为了使用 `BuildKit` 我们 **必须** 使用新的 `$ docker buildx build` 命令来构建 Docker 镜像。
|
||||
|
||||
#### `RUN --mount=type=cache`
|
||||
|
||||
目前,几乎所有的程序都会使用依赖管理工具,例如 `Go` 中的 `go mod`、`Node.js` 中的 `npm` 等等,当我们构建一个镜像时,往往会重复的从互联网中获取依赖包,难以缓存,大大降低了镜像的构建效率。
|
||||
|
||||
例如一个前端工程需要用到 `npm`:
|
||||
|
||||
```docker
|
||||
FROM node:alpine as builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json /app/
|
||||
|
||||
RUN npm i --registry=https://registry.npm.taobao.org \
|
||||
&& rm -rf ~/.npm
|
||||
|
||||
COPY src /app/src
|
||||
|
||||
RUN npm run build
|
||||
|
||||
FROM nginx:alpine
|
||||
|
||||
COPY --from=builder /app/dist /app/dist
|
||||
```
|
||||
|
||||
使用多阶段构建,构建的镜像中只包含了目标文件夹 `dist`,但仍然存在一些问题,当 `package.json` 文件变动时,`RUN npm i && rm -rf ~/.npm` 这一层会重新执行,变更多次后,生成了大量的中间层镜像。
|
||||
|
||||
为解决这个问题,进一步的我们可以设想一个类似 **数据卷** 的功能,在镜像构建时把 `node_modules` 文件夹挂载上去,在构建完成后,这个 `node_modules` 文件夹会自动卸载,实际的镜像中并不包含 `node_modules` 这个文件夹,这样我们就省去了每次获取依赖的时间,大大增加了镜像构建效率,同时也避免了生成了大量的中间层镜像。
|
||||
|
||||
`BuildKit` 提供了 `RUN --mount=type=cache` 指令,可以实现上边的设想。
|
||||
|
||||
```docker
|
||||
FROM node:alpine as builder
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json /app/
|
||||
|
||||
RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
|
||||
--mount=type=cache,target=/root/.npm,id=npm_cache \
|
||||
npm i --registry=https://registry.npm.taobao.org
|
||||
|
||||
COPY src /app/src
|
||||
|
||||
RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
|
||||
# --mount=type=cache,target=/app/dist,id=my_app_dist,sharing=locked \
|
||||
npm run build
|
||||
|
||||
FROM nginx:alpine
|
||||
|
||||
# COPY --from=builder /app/dist /app/dist
|
||||
|
||||
# 为了更直观的说明 from 和 source 指令,这里使用 RUN 指令
|
||||
RUN --mount=type=cache,target=/tmp/dist,from=builder,source=/app/dist \
|
||||
# --mount=type=cache,target/tmp/dist,from=my_app_dist,sharing=locked \
|
||||
mkdir -p /app/dist && cp -r /tmp/dist/* /app/dist
|
||||
```
|
||||
|
||||
第一个 `RUN` 指令执行后,`id` 为 `my_app_npm_module` 的缓存文件夹挂载到了 `/app/node_modules` 文件夹中。多次执行也不会产生多个中间层镜像。
|
||||
|
||||
第二个 `RUN` 指令执行时需要用到 `node_modules` 文件夹,`node_modules` 已经挂载,命令也可以正确执行。
|
||||
|
||||
第三个 `RUN` 指令将上一阶段产生的文件复制到指定位置,`from` 指明缓存的来源,这里 `builder` 表示缓存来源于构建的第一阶段,`source` 指明缓存来源的文件夹。
|
||||
|
||||
上面的 `Dockerfile` 中 `--mount=type=cache,...` 中指令作用如下:
|
||||
|
||||
|Option |Description|
|
||||
|---------------------|-----------|
|
||||
|`id` | `id` 设置一个标志,以便区分缓存。|
|
||||
|`target` (必填项) | 缓存的挂载目标文件夹。|
|
||||
|`ro`,`readonly` | 只读,缓存文件夹不能被写入。 |
|
||||
|`sharing` | 有 `shared` `private` `locked` 值可供选择。`sharing` 设置当一个缓存被多次使用时的表现,由于 `BuildKit` 支持并行构建,当多个步骤使用同一缓存时(同一 `id`)会发生冲突。`shared` 表示多个步骤可以同时读写,`private` 表示当多个步骤使用同一缓存时,每个步骤使用不同的缓存,`locked` 表示当一个步骤完成释放缓存后,后一个步骤才能继续使用该缓存。|
|
||||
|`from` | 缓存来源(构建阶段),不填写时为空文件夹。|
|
||||
|`source` | 来源的文件夹路径。|
|
||||
|
||||
#### `RUN --mount=type=bind`
|
||||
|
||||
该指令可以将一个镜像(或上一构建阶段)的文件挂载到指定位置。
|
||||
|
||||
```docker
|
||||
RUN --mount=type=bind,from=php:alpine,source=/usr/local/bin/docker-php-entrypoint,target=/docker-php-entrypoint \
|
||||
cat /docker-php-entrypoint
|
||||
```
|
||||
|
||||
#### `RUN --mount=type=tmpfs`
|
||||
|
||||
该指令可以将一个 `tmpfs` 文件系统挂载到指定位置。
|
||||
|
||||
```docker
|
||||
RUN --mount=type=tmpfs,target=/temp \
|
||||
mount | grep /temp
|
||||
```
|
||||
|
||||
#### `RUN --mount=type=secret`
|
||||
|
||||
该指令可以将一个文件挂载到指定位置。
|
||||
|
||||
```docker
|
||||
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
|
||||
cat /root/.aws/credentials
|
||||
```
|
||||
|
||||
```bash
|
||||
$ docker buildx build -t test --secret id=aws,src=$HOME/.aws/credentials .
|
||||
```
|
||||
|
||||
#### `RUN --mount=type=ssh`
|
||||
|
||||
该指令可以挂载 `ssh` 密钥。
|
||||
|
||||
```docker
|
||||
FROM alpine
|
||||
RUN apk add --no-cache openssh-client
|
||||
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
|
||||
RUN --mount=type=ssh ssh git@gitlab.com | tee /hello
|
||||
```
|
||||
|
||||
```bash
|
||||
$ eval $(ssh-agent)
|
||||
$ ssh-add ~/.ssh/id_rsa
|
||||
(Input your passphrase here)
|
||||
|
||||
$ docker buildx build -t test --ssh default=$SSH_AUTH_SOCK .
|
||||
```
|
||||
|
||||
### 清理构建缓存
|
||||
|
||||
执行以下命令清理构建缓存
|
||||
|
||||
```bash
|
||||
$ docker builder prune
|
||||
```
|
||||
|
||||
### 官方文档
|
||||
|
||||
* https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md
|
@@ -1,78 +0,0 @@
|
||||
# 使用 buildx 构建多种系统架构支持的 Docker 镜像
|
||||
|
||||
在之前的版本中构建多种系统架构支持的 Docker 镜像,要想使用统一的名字必须使用 [`$ docker manifest`](manifest.md) 命令。
|
||||
|
||||
在 Docker 19.03+ 版本中可以使用 `$ docker buildx build` 命令使用 `BuildKit` 构建镜像。
|
||||
|
||||
该命令支持 `--platform` 参数可以同时构建支持多种系统架构的 Docker 镜像,大大简化了构建步骤。
|
||||
|
||||
## 设置环境变量
|
||||
|
||||
`buildx` 命令属于实验特性,必须设置环境变量以使用该命令。
|
||||
|
||||
Linux/macOS
|
||||
|
||||
```bash
|
||||
$ export DOCKER_CLI_EXPERIMENTAL=enabled
|
||||
```
|
||||
|
||||
Windows
|
||||
|
||||
```bash
|
||||
$ set $env:DOCKER_CLI_EXPERIMENTAL=enabled
|
||||
```
|
||||
|
||||
## 新建 `builder` 实例
|
||||
|
||||
Docker for Linux 不支持构建 `arm` 架构镜像,我们可以运行一个新的容器让其支持该特性,Docker 桌面版无需进行此项设置。
|
||||
|
||||
```bash
|
||||
$ docker run --rm --privileged docker/binfmt:820fdd95a9972a5308930a2bdfb8573dd4447ad3
|
||||
```
|
||||
|
||||
由于 Docker 默认的 `builder` 实例不支持同时指定多个 `--platform`,我们必须首先创建一个新的 `builder` 实例。
|
||||
|
||||
```bash
|
||||
$ docker buildx create --name mybuilder
|
||||
|
||||
$ docker buildx use mybuilder
|
||||
```
|
||||
|
||||
## 构建镜像
|
||||
|
||||
新建 Dockerfile 文件。
|
||||
|
||||
```docker
|
||||
FROM --platform=$TARGETPLATFORM alpine
|
||||
|
||||
RUN uname -a > /os.txt
|
||||
|
||||
CMD cat /os.txt
|
||||
```
|
||||
|
||||
使用 `$ docker buildx build` 命令构建镜像,注意将 `myusername` 替换为自己的 Docker Hub 用户名。
|
||||
|
||||
`--push` 参数表示将构建好的镜像推送到 Docker 仓库。
|
||||
|
||||
```bash
|
||||
$ docker buildx build --platform linux/arm,linux/arm64,linux/amd64 -t myusername/hello . --push
|
||||
|
||||
# 查看镜像信息
|
||||
$ docker buildx imagetools inspect myusername/hello
|
||||
```
|
||||
|
||||
在不同架构运行该镜像,可以得到该架构的信息。
|
||||
|
||||
```bash
|
||||
# arm
|
||||
$ docker run -it --rm myusername/hello
|
||||
Linux buildkitsandbox 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 armv7l Linux
|
||||
|
||||
# arm64
|
||||
$ docker run -it --rm myusername/hello
|
||||
Linux buildkitsandbox 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 aarch64 Linux
|
||||
|
||||
# amd64
|
||||
$ docker run -it --rm myusername/hello
|
||||
Linux buildkitsandbox 4.9.125-linuxkit #1 SMP Fri Sep 7 08:20:28 UTC 2018 x86_64 Linux
|
||||
```
|
Reference in New Issue
Block a user