经验-docker 相关使用实践

一、Harbor 仓库 SSL 证书问题解决(依赖加证书文件实在解决不了的情况下)

 1# 添加自签名证书到系统信任链
 2echo "-----BEGIN CERTIFICATE-----
 3MIID... (证书内容) ...
 4-----END CERTIFICATE-----" | sudo tee -a /etc/ssl/certs/harbor.crt > /dev/null
 5
 6# CentOS/RHEL 更新证书信任库
 7sudo update-ca-trust force-enable
 8sudo update-ca-trust extract
 9
10# Ubuntu/Debian 更新证书
11sudo update-ca-certificates

验证方法

1curl -v https://your-harbor-domain

二、镜像优化分析工具

使用 Dive 进行镜像层分析:

1# 安装 Dive
2wget https://github.com/wagoodman/dive/releases/download/v0.11.0/dive_0.11.0_linux_amd64.deb
3sudo dpkg -i dive*.deb
4
5# 分析镜像
6dive your-image:tag

关键功能

  • 可视化每层文件变化
  • 计算浪费的空间(重复/删除的文件)

三、Dockerfile 最佳实践

  1. 基础规范

    1FROM bitnami/python:3.7.17-debian-11-r0 AS builder
    2WORKDIR /app
    3COPY . .
    
  2. 健康检查配置

    1HEALTHCHECK --interval=30s --timeout=3s --retries=3 \
    2  CMD curl -f http://localhost:8080/health || exit 1
    
  3. 安全增强

    1RUN groupadd -r appuser && useradd -r -g appuser appuser
    2USER appuser
    
  4. 构建优化

    1# 使用 BuildKit 缓存加速
    2RUN --mount=type=cache,target=/go/pkg/mod \
    3    go mod download
    
  5. 编写 .dockerignore 文件, 将不相关的文件忽略, 不发送到 docker 环境

  6. 一个容器运行一个应用

  7. 镜像不要在生产环境中不要使用 latest 标签

  8. 优先使用 copy 而不是 add

  9. 设置默认的环境变量

  10. 使用 label 设置镜像元数据

四、多架构镜像构建

 1# 创建构建器实例
 2docker buildx create --use --name multiarch-builder
 3
 4# 构建并推送多平台镜像
 5docker buildx build --platform linux/arm64,linux/amd64 \
 6  -t your-registry/image:tag --push .
 7
 8# 创建统一 manifest
 9docker manifest create your-registry/image:tag \
10  your-registry/image:tag-arm64 \
11  your-registry/image:tag-amd64

五、网络与安全管控

  1. 动态防火墙管理

    1# 监控 Docker 端口并更新 iptables 规则
    2*/5 * * * * root /path/to/monitor_docker_ports.sh(一个检查 docker 开发了哪些端口的工具)
    
  2. 安全端口暴露策略

    1# 仅允许指定 IP 段访问
    2iptables -A DOCKER-USER -p tcp --dport 8080 \
    3  -s 10.0.0.0/8 -j ACCEPT
    4iptables -A DOCKER-USER -p tcp --dport 8080 -j DROP
    

六、CI/CD 集成

  1. GitLab Runner 配置

    1gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
    2gitlab-runner register --url https://gitlab.com --registration-token $REG_TOKEN
    
  2. 高效缓存配置

    1variables:
    2  DOCKER_BUILDKIT: 1
    3build:
    4  script:
    5    - docker build --cache-from=your-image:cache --tag=your-image:latest .
    6    - docker push your-image:latest
    

七、高级技巧

  • Root 权限进入容器
1   docker exec -it --user root <container> /bin/bash
  • 代理网络配置
1   # 创建代理配置文件
2   cat <<EOF | sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf
3   [Service]
4   Environment="HTTP_PROXY=http://proxy.example.com:8080"
5   Environment="NO_PROXY=localhost,.internal"
6   EOF
7
8   systemctl daemon-reload
9   systemctl restart docker
  • 镜像压缩优化
1   # 多线程压缩加速
2   docker save your-image | pigz -9 > image.tar.gz
  • 多阶段构建
1   FROM golang:1.19 AS build
2   COPY . .
3   RUN go build -o /app
4
5   FROM alpine:3.15
6   COPY --from=build /app /app
7   CMD ["/app"]
  • 标签管理策略
    • 使用语义化版本标签(v1.2.3)
    • 为最新稳定版本维护 latest 标签
    • 包含构建元数据(commit hash + 日期)
  • 日志管理, 防止磁盘撑爆
    1{
    2  "log-driver": "json-file",
    3  "log-opts": {
    4    "max-size": "10m",
    5    "max-file": "3"
    6  }
    7}
    
  • 资源限制
    1docker run -it --cpus=2 --memory=512m --blkio-weight=500 your-image