无法在Jenkins Execute Shell中的Docker容器内执行命令

时间:2018-11-20 07:26:20

标签: linux shell docker jenkins jenkins-pipeline

在Jenkins execute shell中使用docker run命令成功创建了Docker容器。 现在在docker容器中,我想运行一些命令。但是我注意到命令没有在容器内执行。

echo password | sudo -S docker pull image

sudo docker images

imageid=`echo password | sudo -S docker images|grep imagename|tr -s ' '|cut -d' ' -f3`

sudo docker run -i --name=test $imageid /bin/bash

container=`echo password | sudo -S docker ps -all|grep test|cut -d' ' -f1`

#First check user if its inside the docker container
whoami

#above showed that its not inside the container as showed the machine user name instead of root user
#As a work around i tried to restart and execute the container

rest=`sudo docker restart $container`

out=`sudo docker exec -i $container /bin/bash`

whoami

# Above still not shows the root user and i am not able to execute the commands in side docker container on Jenkins

2 个答案:

答案 0 :(得分:0)

我不熟悉您的情况,但是每次遇到类似问题时,我都会尝试手动执行命令以调试过程。使用以下命令进入docker容器:

docker exec -it <my_container> bash

在创建的容器中,就像在DockerFile中一样执行命令。这将改善您的调试过程。

答案 1 :(得分:0)

我可以看到您正在尝试执行docker命令,例如ex:容器内的docker image,因此首先检查容器内部是否安装了docker,否则您将无法在容器内使用docker命令。

例如: docker run -it ubuntu

执行完上述命令后,您将进入容器内部,如下所示。

root:泊坞窗图片

因此在这里,由于未在容器内安装docker,因此在容器内docker映像将无法工作。

相关问题