技术 · 思考 · 实践 · 成长

首页/Dockerfile/正文

从 Dockerfile 到镜像仓库:打标签、多架构与 CI 推送

Dockerfile8 次阅读

镜像构建出来只是第一步,还要按规范打标签、推到仓库、支持多架构,才能在测试和生产环境稳定拉取。这篇把「从 Dockerfile 到镜像仓库」的完整流程走一遍。

一、镜像命名规则

[仓库地址/][命名空间/]仓库名[:标签][@摘要]

registry.cn-hangzhou.aliyuncs.com/company/erp-api:1.4.0
docker.io/library/nginx:1.27-alpine
harbor.example.com/erp/api@sha256:abc123
组成说明
仓库地址不写默认 Docker Hub
命名空间公司/团队名
仓库名小写、中划线
标签版本号或 git sha

二、标签策略

docker build -t erp-api:1.4.0 .              # 语义化版本
docker build -t erp-api:1.4.0-8f3c9a1 .      # 版本 + git 短 sha
docker build -t erp-api:latest .             # 最新(生产慎用)
标签用途建议
1.4.0正式版本生产使用
1.4小版本滚动自动更新到最新补丁
sha-8f3c9a1精确追溯排障时用
latest开发最新禁止用于生产

推荐同时打三个标签:1.4.0、1.4、sha-xxx,兼顾可读与可追溯。

三、登录仓库

docker login                                  # Docker Hub
docker login registry.cn-hangzhou.aliyuncs.com # 阿里云
docker login harbor.example.com               # 自建 Harbor

# CI 里用 --password-stdin,避免密码进历史
echo "$REGISTRY_PASSWORD" | docker login -u "$REGISTRY_USER" --password-stdin harbor.example.com

四、构建并推送

IMAGE=harbor.example.com/erp/api:1.4.0

docker build -t $IMAGE .
docker push $IMAGE

docker pull $IMAGE
docker run --rm $IMAGE --version

五、多架构镜像

# 一次性构建 amd64 与 arm64 并推送
docker buildx create --name mybuilder --use
docker buildx inspect --bootstrap

docker buildx build \
  --platform linux/amd64,linux/arm64 \
  -t harbor.example.com/erp/api:1.4.0 \
  --push .
docker buildx imagetools inspect harbor.example.com/erp/api:1.4.0
# 能看到两个平台的 manifest,拉取时自动选匹配架构

Apple Silicon 开发、x86 服务器部署时,多架构镜像能避免 exec format error。

六、CI 示例

GitHub Actions

name: build-and-push
on:
  push:
    tags: ['v*']

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: docker/login-action@v3
        with:
          registry: harbor.example.com
          username: ${{ secrets.REGISTRY_USER }}
          password: ${{ secrets.REGISTRY_PASSWORD }}
      - uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: harbor.example.com/erp/api:${{ github.ref_name }}
          cache-from: type=registry,ref=harbor.example.com/erp/api:cache
          cache-to: type=registry,ref=harbor.example.com/erp/api:cache,mode=max

GitLab CI

build:
  image: docker:27
  services:
    - docker:27-dind
  script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
    - docker build -t "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG" .
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"
  only:
    - tags

七、镜像清理

docker image prune -f                 # 清理悬空镜像
docker system prune -a --volumes      # 清理所有未使用资源(慎用)

# Harbor 里配置保留策略:只保留最近 10 个 tag、保留带 v 前缀的正式版

八、常见错误

报错原因解决
denied: requested access to the resource is denied未登录或没权限重新 login,确认命名空间
manifest unknown标签不存在检查 tag 拼写
exec format error架构不匹配用 buildx 构建对应平台
https 证书错误私有仓库自签证书配置 CA 或 daemon.json insecure-registries
推送很慢镜像大或网络差瘦身、就近地域仓库

九、总结

一条完整的发布链路是:Dockerfile → build(固定版本 + 多标签)→ login → push(多架构)→ 部署拉取(固定 digest)。标签策略定好,CI 串起来,镜像就变成可追溯、可回滚的交付物,而不是手工打包的压缩包。

人各有命,但命运也从来不是随机发牌,而是每个人主动选择的结果。

评论交流

0 条评论 · 支持多级回复
验证码
暂无评论,来说说你的想法吧

相关文章

继续阅读