无法在jenkins容器内执行docker命令(找不到docker命令)

时间:2020-05-10 09:56:24

标签: docker jenkins

我在EC2实例上运行一个jenkins容器并执行一个简单的管道来测试和部署一个简单的flask应用程序。我正在使用具有Jenkinsfile的git repo。 使用管道,我正在一个临时容器中构建和测试,然后将其部署为容器应用程序。 这是Jenkins文件。

pipeline {
  agent {
    dockerfile {
      filename "Dockerfile"
      args '-i --entrypoint='
               }
        }
  stages {
    stage('build') {
      steps {
        echo 'hello world'
      }
    }
    stage('test') {
      steps {
        sh 'python3 test.py'
      }
      post {
        always {
          junit 'test-results/*.xml'
        }
      }
    }
    stage('Deploy') {
      steps {
        sh 'echo $PATH'
        sh 'whoami'
      }
    }
 }

我正在尝试使用'sh'执行docker命令,但是jenkins生成日志说找不到命令,这很奇怪,因为我已经确认使用docker安装在jenkins容器中 sudo docker exec -it jenkins /bin/sh 接着 which docker 输出'/ usr / bin / docker'

经过一番调查后,由于$ PATH值与构建日志中的jenkins容器不同,因此似乎在容器内执行了“ sh”命令。 这是上面Jenkinsfile的构建日志。

Started by user admin
Obtained Jenkinsfile from git https://github.com/thelogical/webmf-python-flask.git
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/test
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://github.com/thelogical/webmf-python-flask.git # timeout=10
Fetching upstream changes from https://github.com/thelogical/webmf-python-flask.git
 > git --version # timeout=10
 > git fetch --tags --progress -- https://github.com/thelogical/webmf-python-flask.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision ac14a74f94e23fb5b1cd4434ded9d464e4e22022 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f ac14a74f94e23fb5b1cd4434ded9d464e4e22022 # timeout=10
Commit message: "ok"
 > git rev-list --no-walk 7d4f2857afa84e54d77ef4fea0aad0b7404f2415 # timeout=10
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Agent Setup)
[Pipeline] isUnix
[Pipeline] readFile
[Pipeline] sh
+ docker build -t 4f900583e018f99394f091d4af5db99e6cc803bf -f Dockerfile .
Sending build context to Docker daemon  102.4kB

Step 1/7 : FROM python:3.7.6
 ---> f66befd33669
Step 2/7 : WORKDIR /usr/src/app
 ---> Using cache
 ---> d9df4960eb00
Step 3/7 : ADD . /usr/src/app
 ---> 4769ae8f0fee
Step 4/7 : RUN pip install -r requirements.txt
 ---> Running in aefd5251b16a
Collecting Click==7.0
  Downloading Click-7.0-py2.py3-none-any.whl (81 kB)
Collecting Flask==1.1.1
  Downloading Flask-1.1.1-py2.py3-none-any.whl (94 kB)
Collecting itsdangerous==1.1.0
  Downloading itsdangerous-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting Jinja2==2.11.1
  Downloading Jinja2-2.11.1-py2.py3-none-any.whl (126 kB)
Collecting MarkupSafe==1.1.1
  Downloading MarkupSafe-1.1.1-cp37-cp37m-manylinux1_x86_64.whl (27 kB)
Collecting Werkzeug==1.0.0
  Downloading Werkzeug-1.0.0-py2.py3-none-any.whl (298 kB)
Collecting xmlrunner==1.7.7
  Downloading xmlrunner-1.7.7.tar.gz (5.6 kB)
Building wheels for collected packages: xmlrunner
  Building wheel for xmlrunner (setup.py): started
  Building wheel for xmlrunner (setup.py): finished with status 'done'
  Created wheel for xmlrunner: filename=xmlrunner-1.7.7-py3-none-any.whl size=6235 sha256=de9fcea7814bfad46e0cdb3e3bcfd2d7ef139e8b9e3eb781f5af15742146e748
  Stored in directory: /root/.cache/pip/wheels/bb/ae/64/7394a8365bd8e7bf4c49b01d80c0260d1c1ec975183ac1ce37
Successfully built xmlrunner
Installing collected packages: Click, MarkupSafe, Jinja2, Werkzeug, itsdangerous, Flask, xmlrunner
Successfully installed Click-7.0 Flask-1.1.1 Jinja2-2.11.1 MarkupSafe-1.1.1 Werkzeug-1.0.0 itsdangerous-1.1.0 xmlrunner-1.7.7
[91mWARNING: You are using pip version 20.0.2; however, version 20.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
[0mRemoving intermediate container aefd5251b16a
 ---> 9585ddbee5b7
Step 5/7 : EXPOSE 5000
 ---> Running in 856b365825b8
Removing intermediate container 856b365825b8
 ---> 7b1860335847
Step 6/7 : ENTRYPOINT ["python"]
 ---> Running in d4a68d6de72b
Removing intermediate container d4a68d6de72b
 ---> 7c83dea48a62
Step 7/7 : CMD ["app.py"]
 ---> Running in 536cf8410f05
Removing intermediate container 536cf8410f05
 ---> 90461bad0db4
Successfully built 90461bad0db4
Successfully tagged 4f900583e018f99394f091d4af5db99e6cc803bf:latest
[Pipeline] }
[Pipeline] // stage
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . 4f900583e018f99394f091d4af5db99e6cc803bf
.
[Pipeline] withDockerContainer
Jenkins seems to be running inside container 13258ed4dd2e1941f9e4867fb4872025bd5d3250b7188bb2d448aab62b145342
$ docker run -t -d -u 1000:1000 -i --entrypoint= -w /var/jenkins_home/workspace/test --volumes-from 13258ed4dd2e1941f9e4867fb4872025bd5d3250b7188bb2d448aab62b145342 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** 4f900583e018f99394f091d4af5db99e6cc803bf cat
$ docker top 85265dd4a4b76f7b8f57475428fb10eacc131ebe3b2a416444d31c81e8fd3612 -eo pid,comm
[Pipeline] {
[Pipeline] stage
[Pipeline] { (build)
[Pipeline] echo
hello world
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] sh
+ python3 test.py

Running tests...
----------------------------------------------------------------------
....
----------------------------------------------------------------------
Ran 4 tests in 0.009s

OK

Generating XML reports...
Post stage
[Pipeline] junit
Recording test results
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] sh
+ echo /usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[Pipeline] sh
+ whoami
whoami: cannot find name for user ID 1000
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
$ docker stop --time=1 85265dd4a4b76f7b8f57475428fb10eacc131ebe3b2a416444d31c81e8fd3612
$ docker rm -f 85265dd4a4b76f7b8f57475428fb10eacc131ebe3b2a416444d31c81e8fd3612
[Pipeline] // withDockerContainer
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

BOTTOMLINE:我想在成功测试后部署烧瓶容器,但是我不确定该怎么做。我尝试执行“码头工人”,但失败,并说“找不到命令”。

如果需要,这里是Dockerfile的内容。

FROM python:3.7.6

# Create app directory
WORKDIR /usr/src/app
ADD . /usr/src/app

# Install app dependencies
RUN pip install -r requirements.txt


EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app.py"]

在詹金斯容器中,$ PATH为 /usr/local/openjdk-8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

0 个答案:

没有答案