mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-03-27 20:25:48 +00:00
Fix and update
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
|
||||
### `Dockerfile` 新增指令详解
|
||||
|
||||
BuildKit 引入了多项新指令,旨在优化构建缓存和安全性。以下将详细介绍这些指令的用法。
|
||||
|
||||
使用 BuildKit 后,我们可以使用下面几个新的 `Dockerfile` 指令来加快镜像构建。
|
||||
|
||||
要使用最新的 Dockerfile 语法特性,建议在 Dockerfile 开头添加语法指令:
|
||||
@@ -51,6 +53,7 @@ COPY --from=builder /app/dist /app/dist
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
|
||||
FROM node:alpine as builder
|
||||
|
||||
WORKDIR /app
|
||||
@@ -65,6 +68,7 @@ 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
|
||||
@@ -72,6 +76,7 @@ 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
|
||||
@@ -101,6 +106,7 @@ RUN --mount=type=cache,target=/tmp/dist,from=builder,source=/app/dist \
|
||||
|
||||
```docker
|
||||
## 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
|
||||
```
|
||||
@@ -111,6 +117,7 @@ RUN --mount=type=bind,from=php:alpine,source=/usr/local/bin/docker-php-entrypoin
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
|
||||
RUN --mount=type=tmpfs,target=/temp \
|
||||
mount | grep /temp
|
||||
```
|
||||
@@ -121,6 +128,7 @@ RUN --mount=type=tmpfs,target=/temp \
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
|
||||
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
|
||||
cat /root/.aws/credentials
|
||||
```
|
||||
@@ -135,6 +143,7 @@ $ docker build -t test --secret id=aws,src=$HOME/.aws/credentials .
|
||||
|
||||
```docker
|
||||
## syntax=docker/dockerfile:1
|
||||
|
||||
FROM alpine
|
||||
RUN apk add --no-cache openssh-client
|
||||
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
|
||||
@@ -150,6 +159,8 @@ $ docker build -t test --ssh default=$SSH_AUTH_SOCK .
|
||||
|
||||
### docker compose build 使用 BuildKit
|
||||
|
||||
Docker Compose 同样支持 BuildKit,这使得多服务应用的构建更加高效。
|
||||
|
||||
自 Docker 23.0 起,BuildKit 已默认启用,无需额外配置。如果使用旧版本,可设置 `DOCKER_BUILDKIT=1` 环境变量启用。
|
||||
|
||||
### 官方文档
|
||||
|
||||
Reference in New Issue
Block a user