docker 图像中的 Supervisord 作为 CMD 不在 gitlab 作业中 运行

Supervisord as CMD in docker image is not run inside gitlab job

我有一张 docker 图片,其结尾为:

CMD ["/usr/bin/supervisord"]

据我所知,CMD 函数应该在运行 docker 图像时执行。在当地是这样。但是当我像这样在 gitlab 作业中使用它时:

supervisor-test:
  image: some-image-name
  script:
    - sleep 10
    - supervisorctl status
  only:
    refs:
      - merge_requests

它给出:

unix:///var/run/supervisor.sock no such file

我不确定 gitlab 是否以某种方式覆盖了图像的 cmd?以及如何实现在gitlab job

中自动执行一张图片的cmd

是的,GitLab 覆盖了提供给容器的命令;您图像中定义的 CMD 将不会被执行。该命令是 GitLab 设置作业容器以执行作业脚本的方式。

how to achieve to execute cmd of an image automatically in gitlab job

您可以使用图像的 ENTRYPOINT 来确保在 GitLab CI 中自动执行命令,但是您的入口点必须准备好 运行 shell作为命令传递的脚本(例如,如果入口点是 bash 脚本,则在入口点末尾使用 exec /bin/bash)。

如果在 CI 中执行时需要图像的不同行为,您可以根据 $CI 环境变量或任何其他 pre-defined 或用户的存在来调节它在你的工作中定义环境变量。

#!/usr/bin/env bash

# my-entrypoint script
echo "doing something before running commands"

if [[ -n "$CI" ]]; then
    echo "this block will only execute in a CI environment"
    supervisorctl restart all || echo "could not restart" # or whatever you want
    echo "now running script commands"
    # this is how GitLab expects your entrypoint to end, if provided
    # will execute scripts from stdin
    exec /bin/bash

else
    echo "this block will only execute in NON-CI environments"
    # execute the command as if passed to the container normally
    exec "$@"
fi

然后在你的 dockerfile 中你可以做这样的事情:

COPY my-entrypoint /my-entrypoint
RUN chmod +x /my-entrypoint # Optional if you set executable bit in filesystem
ENTRYPOINT ["/my-entrypoint"]
CMD ["/usr/bin/supervisord"] # the default command, when no other is provided

请注意,也可以将入口点作为 GitLab 配置的一部分进行覆盖:

my_job:
  image:
    name: "python:3.9-slim"
    entrypoint: ["/bin/bash", "-c", "echo this executed before the job; exec /bin/bash"]
  script:
    - echo "hello"
    - "echo 123"

可以在 运行ner 配置中禁用覆盖入口点的能力。

您可以将 supervisord 添加为您工作的服务。