【问题标题】:Supervisord as CMD in docker image is not run inside gitlab job在 docker 映像中作为 CMD 的 Supervisord 不在 gitlab 作业中运行
【发布时间】:2022-01-04 10:08:27
【问题描述】:

我有一个以以下结尾的 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作业中自动执行图片的cmd

【问题讨论】:

    标签: docker gitlab-ci supervisord


    【解决方案1】:

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

    如何实现在gitlab作业中自动执行图片的cmd

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2021-03-08
        • 1970-01-01
        • 2021-07-22
        • 2019-10-04
        • 1970-01-01
        • 2022-06-22
        • 2021-09-11
        • 2021-11-18
        • 2015-01-05
        相关资源
        最近更新 更多