Skip to content

镜像仓库

腾讯镜像仓库

登录腾讯云容器镜像服务 Docker Registry

sh
# docker login -u $HARBOR_USER -p $HARBOR_PASSWD $REGISTRY
docker login -u=[username] -p=[password] ccr.ccs.tencentyun.com

username:腾讯云账号 ID,开通时已注册,可在 账号信息 页面获取。

从 Registry 拉取镜像

sh
docker pull ccr.ccs.tencentyun.com/jolylai/running:[tag]

向 Registry 中推送镜像

sh
docker tag [imageId] ccr.ccs.tencentyun.com/jolylai/running:[tag]

docker push ccr.ccs.tencentyun.com/jolylai/running:[tag]

其中 [ImageId] 请替换为您所要推送的实际镜像ID,或使用本地镜像的完整路径,[tag] 请替换为您期待的镜像版本。

构建 Docker 镜像

Dockerfile

Dockerfile
# Install dependencies only when needed
FROM node:16-alpine AS builder
WORKDIR /app

# Install dependencies based on the preferred package manager
COPY package.json pnpm-lock.yaml* .npmrc ./
RUN yarn global add pnpm && pnpm i --frozen-lockfile;

COPY . .

RUN pnpm build

# Production image, copy all the files and run next
FROM node:16-alpine AS runner
WORKDIR /app

ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

COPY --from=builder /app/.output ./nuxt

EXPOSE 3000

ENV PORT 3000

CMD ["node", "nuxt/server/index.mjs"]

构建一个名为 api 的镜像

sh
# docker build -t [ImageName]:[镜像版本号] .
docker build -t api:latest .

# docker tag [ImageName] ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[镜像版本号]
docker tag api ccr.ccs.tencentyun.com/repository/api:latest

# docker push ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[镜像版本号]
docker push ccr.ccs.tencentyun.com/repository/api:latest
  • ImageId:本地需上传镜像的 Id,可通过 docker image ls 查看。
  • 镜像版本:将本地镜像上传至镜像仓库时,该镜像的镜像版本或标签(Tag)。
  • namespace:开通镜像仓库时填写的命名空间。
  • ImageName:在控制台创建的镜像名称。

使用镜像

sh
docker login --username=[username] ccr.ccs.tencentyun.com

# docker pull ccr.ccs.tencentyun.com/[namespace]/[ImageName]:[镜像版本号]
docker pull ccr.ccs.tencentyun.com/repository/api:latest
sh
docker run --rm -d \
  -p 5000:3000 \
  --name nuxt-api ccr.ccs.tencentyun.com/gh-registry/api:latest

GitHub Action 自动构建

创建 github 镜像构建脚本 .github/workflows/docker.yml

yml
name: Publish Docker image
on:
  push:
    tags:
      - v*
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Tencent Cloud Registry
        uses: docker/login-action@v3
        with:
          registry: ccr.ccs.tencentyun.com
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_PASSWORD }}

      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ccr.ccs.tencentyun.com/${{ github.repository }}

      - name: Build and push
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: |
            linux/amd64
            linux/arm64
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

创建脚本所需的环境变量

DOCKER_USERNAME=用户名
DOCKER_PASSWORD=密码
DOCKER_IMAGE=镜像名称