mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-08-10 08:27:25 +00:00
docs: refine Buildx guidance
This commit is contained in:
@@ -15,7 +15,7 @@ BuildKit 引入了多项新指令,旨在优化构建缓存和安全性。以
|
||||
要使用最新的 Dockerfile 语法特性,建议在 Dockerfile 开头添加语法指令:
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
```
|
||||
这将使用最新的稳定版语法解析器,确保你可以使用所有最新特性。
|
||||
@@ -51,7 +51,7 @@ COPY --from=builder /app/dist /app/dist
|
||||
`BuildKit` 提供了 `RUN --mount=type=cache` 指令,可以实现上边的设想。
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
FROM node:alpine as builder
|
||||
|
||||
@@ -105,7 +105,7 @@ RUN --mount=type=cache,target=/tmp/dist,from=builder,source=/app/dist \
|
||||
该指令可以将一个镜像 (或上一构建阶段) 的文件挂载到指定位置。
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
RUN --mount=type=bind,from=php:alpine,source=/usr/local/bin/docker-php-entrypoint,target=/docker-php-entrypoint \
|
||||
cat /docker-php-entrypoint
|
||||
@@ -116,7 +116,7 @@ RUN --mount=type=bind,from=php:alpine,source=/usr/local/bin/docker-php-entrypoin
|
||||
该指令可以将一个 `tmpfs` 文件系统挂载到指定位置。
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
RUN --mount=type=tmpfs,target=/temp \
|
||||
mount | grep /temp
|
||||
@@ -127,10 +127,10 @@ RUN --mount=type=tmpfs,target=/temp \
|
||||
该指令可以将一个文件 (例如密钥) 挂载到指定位置。
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
|
||||
cat /root/.aws/credentials
|
||||
test -s /root/.aws/credentials && echo "credentials mounted"
|
||||
```
|
||||
```bash
|
||||
$ docker build -t test --secret id=aws,src=$HOME/.aws/credentials .
|
||||
@@ -141,7 +141,7 @@ $ docker build -t test --secret id=aws,src=$HOME/.aws/credentials .
|
||||
该指令可以挂载 `ssh` 密钥。
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
FROM alpine
|
||||
RUN apk add --no-cache openssh-client
|
||||
|
||||
@@ -13,6 +13,8 @@ $ docker buildx build .
|
||||
```
|
||||
Buildx 使用 [BuildKit 引擎](10.1_buildkit.md)进行构建,支持许多新的功能,具体参考 [Buildkit](10.1_buildkit.md) 一节。
|
||||
|
||||
需要注意的是,默认 `docker` driver 会把构建结果加载到本地镜像存储;使用 `docker-container`、remote、cloud 等 builder 时,如未指定 `--load`、`--push` 或 `--output`,结果通常只保留在构建缓存中。
|
||||
|
||||
#### 构建前检查
|
||||
|
||||
Buildx 0.15 起支持构建检查:常规构建会默认检查 Dockerfile 与构建参数;如果只想做检查而不真正构建,可以使用 `--check`:
|
||||
|
||||
Reference in New Issue
Block a user