0

我会使用创建一个 docker 映像docker-compose并将其推送到gitlab container registry. 该docker-compose文件有一些build.args所以在编译期间我使用--env-file标志传递 env 文件。Dockerfile图像是一个两步:base这是production内容:

FROM node:17.0-alpine AS base

ARG GITLAB_ACCESS_TOKEN

WORKDIR /usr/src/app

COPY .npmrc ./
COPY package*.json ./

RUN npm i

COPY . .

FROM base AS production

RUN npm run build

这是docker-compose.yml

version: '3.8'
services: 
  aaa-bbb:
    container_name: ccc
    environment:
      - GITLAB_ACCESS_TOKEN=${GITLAB_ACCESS_TOKEN}
    build:
      context: .
      dockerfile: Dockerfile
      target: base
      args:
        GITLAB_ACCESS_TOKEN: ${GITLAB_ACCESS_TOKEN}
        PORT: ${PORT}
    image: aaa-bbb/ccc:${TAG_VERSION}
    restart: always
    ports:
      - 80:${PORT}
    command: yarn start:dev

这个是docker-compose.prod.yml

version: '3.8'
services: 
  aaa-bbb:
    container_name: ccc
    environment:
      - GITLAB_ACCESS_TOKEN=${GITLAB_ACCESS_TOKEN}
    build:
      context: .
      dockerfile: Dockerfile
      target: production
      args:
        GITLAB_ACCESS_TOKEN: ${GITLAB_ACCESS_TOKEN}
        PORT: ${PORT}
    image: aaa-bbb/ccc:${TAG_VERSION}
    restart: always
    ports:
      - 80:${PORT}
    command: yarn start:prd

要构建图像,我使用以下命令:docker-compose --env-file ./config/.production.env -f docker-compose.yml -f docker-compose.prod.yml build --no-cache

这是 gitlab 管道步骤,用于构建并存储到容器中,重新注册 docker 映像:

create_image:
  image: docker
  stage: create-image
  services:
    - docker:dind
  before_script:
    - TAG_VERSION=$(awk -F= '$1 == "TAG_VERSION" { print $2 }' ./config/.production.env)
    - apk add --no-cache docker-compose
    - docker image prune -f
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - echo " Image creation started..."
    - docker-compose --env-file ./config/.production.env -f docker-compose.yml -f docker-compose.prod.yml build --no-cache
    - docker-compose --env-file ./config/.production.env push aaa-bbb/ccc:${TAG_VERSION}
    - echo " Image created successfully!"

这是管道的输出:

Removing intermediate container 1187fec9a21e
 ---> b0c14918a07b
[Warning] One or more build-args [PORT] were not consumed
Successfully built b0c14918a07b
Successfully tagged aaa-bbb/ccc:1.0.0
$ docker-compose --env-file ./config/.production.env push aaa-bbb/ccc:${TAG_VERSION}
No such service: aaa-bbb/ccc:1.0.0
Cleaning up project directory and file based variables 00:00
ERROR: Job failed: exit code 1

如您所见,有这个警告:[Warning] One or more build-args [PORT] were not consumed 在我的本地测试期间它没有出现,其次我有一个服务标记为aaa-bbb/ccc:1.0.0但我无法推送它,因为没有该服务。

我该如何解决这个问题?

4

1 回答 1

1

您的第一个问题是在您docker-compose.yml定义了一个名为 PORT 的构建参数,但在您的Dockerfile. 因此 docker 警告它已定义但未使用。

您可以通过添加来解决这个问题

ARG PORT
EXPOSE ${PORT}

到最后Dockerfile。如果您想设置默认值(例如 HTTP port 80)作为端口,您可以使用以下形式:ARG PORT=80.

如果您需要有关Dockerfiles 的更多信息,可以在https://docs.docker.com/engine/reference/builder/的参考文档中阅读更多信息

但这不会导致错误。与您一起推送时,docker-compose请使用服务名称而不是图像。所以在你的情况下,正确的命令是

docker-compose --env-file ./config/.production.env push aaa-bbb

此外,您需要在docker-compose.yml. IE:

services: 
  aaa-bbb:
    [redacted]
    image: your.gitlab.instance/perhapssomegroup/aaa-bbb/ccc:${TAG_VERSION}
    [redacted]

docker-compose您可以在https://docs.docker.com/compose/reference/阅读更多关于它的 yml 文件,在https://docs.docker.com/compose/compose-file/compose-file-v3/

我建议拥有我在快速拨号(也称为书签)上发布的所有文档链接,因为您可能非常需要它们。

于 2021-11-09T13:39:31.643 回答