首页 > 解决方案 > 无法使用 jenkinsci/jnlp-slave:4.3-1 从 docker run 执行命令

问题描述

问题

我正在使用 Docker 映像jenkinsci/jnlp-slave:4.3-1

我试图从docker run执行命令,如下所示:

docker run --rm jenkinsci/jnlp-slave:4.3-1 cat /etc/os-release

但是,该命令似乎没有以通常的方式执行。例如,上面的命令导致错误:

$docker run --rm jenkinsci/jnlp-slave:4.3-1 cat /etc/os-release
At least one -url option is required.
java -jar agent.jar [options...] <secret key> <agent name>
 -agentLog FILE                        : Local agent error log destination
                                         (overrides workDir)
 -cert VAL                             : Specify additional X.509 encoded PEM
                                         certificates to trust when connecting
                                         to Jenkins root URLs. If starting with
                                         @ then the remainder is assumed to be
                                         the name of the certificate file to
                                         read.
 -credentials USER:PASSWORD            : HTTP BASIC AUTH header to pass in for
                                         making HTTP requests.
 -direct (-directConnection) HOST:PORT : Connect directly to this TCP agent
                                         port, skipping the HTTP(S) connection
                                         parameter download. For example,
                                         "myjenkins:50000".
 -disableHttpsCertValidation           : Ignore SSL validation errors - use as
                                         a last resort only. (default: false)
 -failIfWorkDirIsMissing               : Fails the initialization if the
                                         requested workDir or internalDir are
                                         missing ('false' by default) (default:
                                         false)
 -headless                             : Run agent in headless mode, without
                                         GUI (default: false)
 -help                                 : Show this help message (default: false)
 -instanceIdentity VAL                 : The base64 encoded InstanceIdentity
                                         byte array of the Jenkins master. When
                                         this is set, the agent skips
                                         connecting to an HTTP(S) port for
                                         connection info.
 -internalDir VAL                      : Specifies a name of the internal files
                                         within a working directory ('remoting'
                                         by default) (default: remoting)
 -jar-cache DIR                        : Cache directory that stores jar files
                                         sent from the master
 -loggingConfig FILE                   : Path to the property file with
                                         java.util.logging settings
 -noKeepAlive                          : Disable TCP socket keep alive on
                                         connection to the master. (default:
                                         false)
 -noreconnect                          : If the connection ends, don't retry
                                         and just exit. (default: false)
 -protocols VAL                        : Specify the remoting protocols to
                                         attempt when instanceIdentity is
                                         provided.
 -proxyCredentials USER:PASSWORD       : HTTP BASIC AUTH header to pass in for
                                         making HTTP authenticated proxy
                                         requests.
 -tunnel HOST:PORT                     : Connect to the specified host and
                                         port, instead of connecting directly
                                         to Jenkins. Useful when connection to
                                         Jenkins needs to be tunneled. Can be
                                         also HOST: or :PORT, in which case the
                                         missing portion will be auto-configured
                                         like the default behavior
 -url URL                              : Specify the Jenkins root URLs to
                                         connect to.
 -version                              : Shows the version of the remoting jar
                                         and then exits (default: false)
 -webSocket                            : Make a WebSocket connection to Jenkins
                                         rather than using the TCP port.
                                         (default: false)
 -workDir FILE                         : Declares the working directory of the
                                         remoting instance (stores cache and
                                         logs by default)

预期行为

我使用不同的图像(例如ubuntu:21.04 )尝试了相同的操作。

该命令以正常方式执行,如下所示:

$docker run --rm ubuntu:21.04 cat /etc/os-release
NAME="Ubuntu"
VERSION="21.04 (Hirsute Hippo)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu Hirsute Hippo (development branch)"
VERSION_ID="21.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=hirsute
UBUNTU_CODENAME=hirsute

问题

  1. 我想知道为什么一个简单的 Linux 命令会根据图像生成不同的结果。该命令cat /etc/os-release在以不同方式执行时运行良好,如下所示:
    $docker run --rm -it jenkinsci/jnlp-slave:4.3-1 /bin/bash
    jenkins@4a7d91e19e3a:~$ cat /etc/os-release
    PRETTY_NAME="Debian GNU/Linux 10 (buster)"
    NAME="Debian GNU/Linux"
    VERSION_ID="10"
    VERSION="10 (buster)"
    VERSION_CODENAME=buster
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"
    
  2. 是否可以从docker runusing执行命令jenkinsci/jnlp-slave:4.3-1

标签: linuxdockerjenkins

解决方案


每个图像都有一个ENTRYPOINT. 这是一个在您启动容器时调用的可执行对象(带有可选参数)。您可以使用docker inspect来查找默认入口点值。这是docker.io/jenkinsci/jnlp-slave:4.3-1图像的入口点:

"Entrypoint": [
  "jenkins-agent"
],

这个是针对ubuntu的:

"Entrypoint": null,

当你使用cat /etc/os-release命令运行 jenkins image 时,你有效地执行了这个命令:

jenkins-agent cat /etc/os-release

当您在 ubuntu 中执行此操作时,它null + cat /etc/os-release就是cat /etc/os-release. 这就是错误发生的原因。

对于该使用--entrypoint=<value>选项,可以在运行时覆盖默认入口点。例如,要cat /etc/os-release在 jenkins 映像中运行,您可以尝试以下操作:

docker run --rm --entrypoint="" jenkinsci/jnlp-slave:4.3-1 cat /etc/os-release

也准备好接受一些图像除了应用程序的一个二进制文件之外什么都没有。在此类图像中,您不会找到shlscat等。


推荐阅读