首页 > 解决方案 > 在 Jenkins 构建中运行 docker 脚本时出现编译错误

问题描述

我一直在阅读 docker book,现在正在学习 CI。我试图在我的构建的执行 shell 中运行这个脚本:

# Build the image to be used for this job.
IMAGE=$(sudo docker build . | tail -1 | awk '{ print $NF }') 
# Build the directory to be mounted into Docker.
MNT="$WORKSPACE/.."
# Execute the build inside Docker.
CONTAINER=$(sudo docker run -d -v $MNT:/opt/project/ $IMAGE /bin/ bash -c 'cd /opt/project/workspace; rake spec')
# Attach to the container so that we can see the output.
   sudo docker attach $CONTAINER
# Get its exit code as soon as the container stops.
RC=$(sudo docker wait $CONTAINER)
# Delete the container we've just used.
   sudo docker rm $CONTAINER
# Exit with the same value as that with which the process exited.
exit $RC

运行此脚本以构建失败告终。它显示了这两个错误: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?sudo docker run -d -v /private/var/jenkins_home/jobs/${Docker_test_job}/workspace/..:/opt/project/ /bin/ bash -c cd /opt/project/workspace; rake spec docker: invalid reference format. See 'docker run --help'. + CONTAINER= Build step 'Execute shell' marked build as failure Recording test results ERROR: Step ‘Publish JUnit test result report’ failed: No test report files were found. Configuration error? Finished: FAILURE

我不明白如何修复它,因为我一直按照书中的说明进行操作。我尝试使用 $PWD 来尝试解决我的问题,但这也不起作用。

标签: dockershelljenkins

解决方案


实际上 jenkins 用户没有运行 docker 命令的权限。为此,请将您的 jenkins 用户添加到 docker 组:

sudo usermod -aG docker jenkins

然后重新启动您的 jenkins 服务器以刷新组。请注意,有一条警告“docker 组授予与 root 用户等效的权限。有关这如何影响系统安全性的详细信息。”


推荐阅读