mirror of
https://github.com/yeasy/docker_practice.git
synced 2026-03-24 10:45:23 +00:00
Add blank lines before headers
This commit is contained in:
@@ -67,6 +67,7 @@ $ sudo systemctl restart docker
|
||||
我们可以运行一个简单的容器并执行 `sleep` 命令,同时在宿主机上观察进程的所有者:
|
||||
|
||||
```bash
|
||||
|
||||
## 在容器内以 root 身份运行
|
||||
$ docker run -d --name userns_test alpine sleep 3600
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ $ docker run --rm -it \
|
||||
[Trivy](https://github.com/aquasecurity/trivy) 是由 Aqua Security 发行的一款针对容器技术的快速镜像漏洞扫描利器。在分发应用前通过它的扫描可以规避绝大多数风险。
|
||||
|
||||
```bash
|
||||
|
||||
## 如果使用本地命令行扫描容器镜像
|
||||
$ trivy image alpine:3.10
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ Trivy 是由 Aqua Security 开发的开源漏洞扫描器,以其轻量级、
|
||||
**安装与基本使用:**
|
||||
|
||||
```bash
|
||||
|
||||
# 安装 Trivy
|
||||
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
|
||||
|
||||
@@ -37,6 +38,7 @@ trivy repo https://github.com/aquasecurity/trivy
|
||||
**在 CI/CD 中集成:**
|
||||
|
||||
```bash
|
||||
|
||||
# 设置严重程度过滤
|
||||
trivy image --severity HIGH,CRITICAL \
|
||||
--exit-code 1 \
|
||||
@@ -56,6 +58,7 @@ Grype 由 Anchore 开发,支持更广泛的软件包管理器和语言。
|
||||
**安装与使用:**
|
||||
|
||||
```bash
|
||||
|
||||
# 安装 Grype
|
||||
curl -sSfL https://raw.githubusercontent.com/anchore/grype/main/install.sh | sh -s -- -b /usr/local/bin
|
||||
|
||||
@@ -83,6 +86,7 @@ Snyk 提供了商业级的安全扫描服务,特别适合企业环境。
|
||||
**基本使用:**
|
||||
|
||||
```bash
|
||||
|
||||
# 安装 Snyk CLI
|
||||
npm install -g snyk
|
||||
|
||||
@@ -124,6 +128,7 @@ curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -
|
||||
**生成 SBOM:**
|
||||
|
||||
```bash
|
||||
|
||||
# 从镜像生成 SBOM(多种格式)
|
||||
syft docker:nginx:latest -o json > sbom.json
|
||||
syft docker:nginx:latest -o spdx > sbom.spdx
|
||||
@@ -188,6 +193,7 @@ syft oci-archive:image.tar -o json > sbom.json
|
||||
当新的 CVE 被发现时,可快速查询受影响的应用:
|
||||
|
||||
```bash
|
||||
|
||||
# 使用 Grype 针对 SBOM 进行漏洞扫描
|
||||
grype sbom:sbom.json --add-cpes-if-none
|
||||
```
|
||||
@@ -220,12 +226,14 @@ sudo mv cosign-linux-amd64 /usr/local/bin/cosign
|
||||
|
||||
```bash
|
||||
cosign generate-key-pair
|
||||
|
||||
# 生成 cosign.key 和 cosign.pub
|
||||
```
|
||||
|
||||
**签名镜像:**
|
||||
|
||||
```bash
|
||||
|
||||
# 使用私钥签名(推送到仓库前)
|
||||
cosign sign --key cosign.key myregistry.com/myapp:v1.0.0
|
||||
|
||||
@@ -235,6 +243,7 @@ cosign sign --key cosign.key myregistry.com/myapp:v1.0.0
|
||||
**验证签名:**
|
||||
|
||||
```bash
|
||||
|
||||
# 使用公钥验证
|
||||
cosign verify --key cosign.pub myregistry.com/myapp:v1.0.0
|
||||
|
||||
@@ -253,6 +262,7 @@ cosign verify --key cosign.pub myregistry.com/myapp:v1.0.0
|
||||
**Keyless 签名(推荐用于 CI/CD):**
|
||||
|
||||
```bash
|
||||
|
||||
# 在 GitHub Actions 等 CI 中无需存储密钥
|
||||
cosign sign --yes myregistry.com/myapp:v1.0.0
|
||||
|
||||
@@ -269,11 +279,13 @@ Docker Content Trust 使用 Notary 实现镜像签名,是 Docker 官方的签
|
||||
**启用 DCT:**
|
||||
|
||||
```bash
|
||||
|
||||
# 在环境中启用 DCT
|
||||
export DOCKER_CONTENT_TRUST=1
|
||||
|
||||
# 此后所有 docker push/pull 都需要签名
|
||||
docker push myregistry.com/myapp:v1.0.0
|
||||
|
||||
# 如果镜像未签名,操作会被拒绝
|
||||
|
||||
# 禁用 DCT(仅用于特定操作)
|
||||
@@ -283,6 +295,7 @@ docker push --disable-content-trust myregistry.com/myapp:v1.0.0
|
||||
**签名密钥管理:**
|
||||
|
||||
```bash
|
||||
|
||||
# 首次推送时会提示创建 Delegation Key
|
||||
# 密钥存储在 ~/.docker/trust/private/root_keys/ 和 ~/.docker/trust/private/tuf_keys/
|
||||
|
||||
@@ -295,6 +308,7 @@ docker inspect --format='{{.RepoDigests}}' myregistry.com/myapp:v1.0.0
|
||||
#### 1. 基础镜像安全
|
||||
|
||||
```dockerfile
|
||||
|
||||
# ❌ 不推荐:使用 latest 标签
|
||||
FROM ubuntu:latest
|
||||
RUN apt-get update && apt-get install -y curl
|
||||
@@ -327,6 +341,7 @@ COPY --from=builder /app/app /app
|
||||
#### 3. 运行时镜像扫描策略
|
||||
|
||||
```bash
|
||||
|
||||
# 镜像构建完成后立即扫描
|
||||
trivy image --severity HIGH,CRITICAL \
|
||||
--exit-code 1 \
|
||||
@@ -342,6 +357,7 @@ trivy image --scanners vuln,misconfig registry:5000/myapp:latest
|
||||
**Harbor(私有镜像仓库)的安全扫描:**
|
||||
|
||||
```yaml
|
||||
|
||||
# harbor.yml 配置示例
|
||||
trivy:
|
||||
enabled: true
|
||||
|
||||
Reference in New Issue
Block a user