You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在GitLab流水线中保留Docker镜像ENTRYPOINT并替换CMD

解决GitLab CI中使用CNES报告镜像的参数传递问题

现有Dockerfile

FROM ubuntu:latest

# Prepare the operating system
RUN apt-get update && \
apt-get install --yes openjdk-11-jre

# Copy the CNES pluging
COPY ./src/sonar-cnes-report-4.1.3.jar /opt/cnes-report/sonar-cnes-report-4.1.3.jar

WORKDIR /opt/cnes-report

ENTRYPOINT [ "java", "-jar", "sonar-cnes-report-4.1.3.jar" ]
CMD [ "-h" ]

本地运行状态

该镜像在本地使用完全正常:

  • 执行docker run <image>默认显示帮助信息
  • 传递额外参数(如docker run 8215b6dcc57b -v)可覆盖CMD,实现对应功能,也能正常生成SonarQube项目报告

GitLab CI中遇到的问题

尝试两种配置均失败:

  1. 直接在script中写参数-v,报错提示需提供-p参数
  2. 覆盖entrypoint为/bin/bash后执行完整命令,报错/usr/bin/sh: /usr/bin/sh: cannot execute binary file

解决方案

方案一:修改Dockerfile(优先推荐,兼顾本地与CI使用)

通过封装entrypoint脚本的方式,让参数传递更灵活,同时保留本地使用的便利性:

  1. 在项目根目录创建entrypoint.sh脚本,内容如下:
#!/bin/bash
exec java -jar /opt/cnes-report/sonar-cnes-report-4.1.3.jar "$@"

给脚本添加执行权限:chmod +x entrypoint.sh

  1. 修改Dockerfile,替换原有的ENTRYPOINT配置:
FROM ubuntu:latest

# Prepare the operating system
RUN apt-get update && \
apt-get install --yes openjdk-11-jre

# Copy entrypoint script and CNES plugin
COPY ./entrypoint.sh /opt/cnes-report/entrypoint.sh
COPY ./src/sonar-cnes-report-4.1.3.jar /opt/cnes-report/sonar-cnes-report-4.1.3.jar

WORKDIR /opt/cnes-report

ENTRYPOINT [ "./entrypoint.sh" ]
CMD [ "-h" ]
  1. GitLab CI配置示例:
job:
  image: your-custom-image:tag
  script:
    - "-t -s -p your-sonar-project-key"

本地使用方式和之前完全一致,无需调整。

方案二:不修改Dockerfile,调整GitLab CI配置

如果无法修改Dockerfile,可通过GitLab CI的entrypoint和script字段配合实现:

job:
  image: your-custom-image:tag
  entrypoint: ["java", "-jar", "/opt/cnes-report/sonar-cnes-report-4.1.3.jar"]
  script:
    - "-t -s -p your-sonar-project-key"

或者直接在script中写完整命令:

job:
  image: your-custom-image:tag
  script:
    - java -jar /opt/cnes-report/sonar-cnes-report-4.1.3.jar -t -s -p your-sonar-project-key

这种方式下本地使用不受影响,CI中也能正确传递参数。

内容的提问来源于stack exchange,提问作者user2448122

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 13:35:08