当我尝试运行container.inside()方法时,Jenkins挂起

时间:2019-02-12 21:10:09

标签: python docker jenkins

我已经建立了一个管道,其唯一目的是建立一个映像,然后在其中执行一个简单的命令。

我能够构建容器,但是,我无法在其中执行任何命令。当我执行“ sh”步骤时,它会无限期地挂起。

如果我运行container.run()方法或container.withRun,该步骤将执行,但实际上不在容器内部。

很明显,我在这里做错了-有人可以建议这样做的正确方法吗?我只希望能够在容器内执行步骤,或者可以在容器内执行命令,然后将该操作的结果读回到Jenkins。

node {
    String baseName = "salimfadhley/python_hello_world_server"
    String buildTag = "${baseName}:${env.BUILD_ID}".toString()
    def customImage

    stage('Build') {
        checkout scm
        customImage = docker.build(buildTag)
    }

    stage('Test') {
        customImage.inside('--entrypoint=/bin/bash') {
            sh "pwd"
        }
    }

}

Jenkins日志输出如下所示:

[Pipeline] withDockerContainer
Jenkins seems to be running inside container ded46c77c61f36b31ea0cd8a067b20ebf97c71daa36c9c18a0c83d784f5791c5
$ docker run -t -d -u 0:0 --entrypoint=/bin/bash -w /var/jenkins_home/jobs/python-hello-world-server/branches/master/workspace --volumes-from ded46c77c61f36b31ea0cd8a067b20ebf97c71daa36c9c18a0c83d784f5791c5 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** salimfadhley/python_hello_world_server:88 cat
$ docker top f5cd987963e5ea7c89cd70ddc6b2025d2bdab0ae1705c0b7a560fadc9aba0728 -eo pid,comm
ERROR: The container started but didn't run the expected command. Please double check your ENTRYPOINT does execute the command passed as docker run argument, as required by official docker images (see https://github.com/docker-library/official-images#consistency for entrypoint consistency requirements).
Alternatively you can force image entrypoint to be disabled by adding option `--entrypoint=''`.
[Pipeline] {
[Pipeline] echo
Hello Inside!
[Pipeline] sh
process apparently never started in /var/jenkins_home/jobs/python-hello-world-server/branches/master/workspace@tmp/durable-50fcbb38
[Pipeline] }
$ docker stop --time=1 f5cd987963e5ea7c89cd70ddc6b2025d2bdab0ae1705c0b7a560fadc9aba0728
$ docker rm -f f5cd987963e5ea7c89cd70ddc6b2025d2bdab0ae1705c0b7a560fadc9aba0728
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code -2
Finished: FAILURE

Dockerfile看起来像这样:

FROM salimfadhley/testpython:latest
COPY . /project
COPY src /src
RUN python -m pip install --upgrade pip setuptools
RUN python -m pip install -e /project/src
WORKDIR /project
#ENTRYPOINT ["/bin/bash"]
ENTRYPOINT ["uvicorn", "--port=80", "--host=0.0.0.0", "helloworld.main:app"]
EXPOSE 8080

因此,总结一下:我可以修复此管道,以使其实际上允许我在容器内运行进程吗?

1 个答案:

答案 0 :(得分:0)

我建议仅将dockerfile作为代理运行,并逐步运行您的东西。

stage {
  agent {
    dockerfile {
      filename 'Dockerfile'
      reuseNode true // <- this is if you need to access the workspace
    }
  }
  steps {
    sh "run whatever stuff you want"
  }
}