mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-08-10 16:37:34 +00:00
Add new content and update versions
This commit is contained in:
@@ -158,4 +158,41 @@ RUN --mount=type=cache,target=/go/pkg/mod \
|
||||
RUN --mount=type=secret,id=mysecret \
|
||||
cat /run/secrets/mysecret
|
||||
```
|
||||
|
||||
#### 3. Heredoc 语法
|
||||
|
||||
BuildKit 支持使用 heredoc 语法编写多行脚本,无需行末反斜杠 `\` 连接:
|
||||
|
||||
```docker
|
||||
RUN <<EOF
|
||||
apt-get update
|
||||
apt-get install -y \
|
||||
build-essential \
|
||||
curl \
|
||||
git
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
EOF
|
||||
```
|
||||
|
||||
优势:
|
||||
|
||||
- 可读性更强,不需要在每行末尾添加 `\`
|
||||
- 避免在脚本中转义引号
|
||||
- 支持多个 heredoc 块,可指定不同的 Shell
|
||||
|
||||
```docker
|
||||
RUN <<EOF
|
||||
echo "使用默认 /bin/sh"
|
||||
EOF
|
||||
|
||||
RUN <<'EOF'
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
echo "使用 bash"
|
||||
wget http://example.com/file.tar.gz
|
||||
EOF
|
||||
```
|
||||
|
||||
> 💡 使用 heredoc 需要在 Dockerfile 首行声明 BuildKit 语法版本:`# syntax=docker/dockerfile:1`
|
||||
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user