我是码头工人的新手。我只是试着在我的本地机器(Ubuntu 16.04)上使用docker和Jenkins。

我用下面的管道脚本配置了一个新作业。

node {
    stage('Build') {
      docker.image('maven:3.3.3').inside {
        sh 'mvn --version'
      }
    }
}

但是它失败了,错误如下:

在unix:///var/run/ Docker .sock试图连接到Docker守护进程套接字时被拒绝


当前回答

在做生产配置时,我得到了权限问题。我尝试下面的解决方案来解决这个问题。

错误消息

ubuntu@node1:~$ docker run hello-world
docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/create: dial unix /var/run/docker.sock: connect: permission denied.
See 'docker run --help'.

解决方法:错误消息/var/run/docker.sock中socket的权限:

ubuntu@ip-172-31-21-106:/var/run$ ls -lrth docker.sock
srw-rw---- 1 root root 0 Oct 17 11:08 docker.sock
ubuntu@ip-172-31-21-106:/var/run$ sudo chmod 666 /var/run/docker.sock
ubuntu@ip-172-31-21-106:/var/run$ ls -lrth docker.sock
srw-rw-rw- 1 root root 0 Oct 17 11:08 docker.sock

更改docket权限后。Sock然后执行以下命令检查权限。

ubuntu@ip-172-31-21-106:/var/run$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:c3b4ada4687bbaa170745b3e4dd8ac3f194ca95b2d0518b417fb47e5879d9b5f
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

其他回答

我有Jenkins在Docker中运行,并连接Jenkins从主机Ubuntu 16.04通过卷到/var/run/docker.sock使用Docker套接字。

对我来说,解决方案是:

1) Jenkins的Docker容器内部(Docker exec - Jenkins bash on host machine)

usermod -a -G docker jenkins
chmod 664 /var/run/docker.sock
service jenkins restart (or systemctl restart jenkins.service)
su jenkins

2)主机上:

sudo service docker restart

664表示-对组中的所有者和用户进行读写(但不执行)。

2019-02-16

对我来说,大多数步骤都和其他人写的一样。 然而,我无法使用usermod和上述解决方案将jenkins添加到组docker中。

我从docker主机和运行中的docker容器中尝试了以下命令:

sudo usermod -a -G docker jenkins

(我从docker主机输入以下命令进入正在运行的docker容器:

docker exec -t -i my_container_id_or_name /bin/bash

)

从docker主机收到:

Usermod:用户jenkins不存在

从docker容器收到:

我们相信你已经接受了当地体制的常规教育 管理员。通常可以归结为以下三点: 1)尊重他人的隐私。 #2)三思而后行。 #3)能力越大,责任越大。 [sudo] jenkins密码:

我不知道密码。

没有sudo部分的命令,在docker容器中我收到:

usermod:拒绝权限。Usermod:无法锁定/etc/passwd;试一试 稍后再。

解决方案: 我从docker主机输入以下命令进入正在运行的docker容器:

docker exec -t -i -u root my_container_id_or_name /bin/bash

现在,我以root身份输入,并发出以下命令:

usermod -a -G docker jenkins

然后,从docker主机,我重新启动我的运行docker容器与以下命令:

docker restart my_container_id_or_name

在那之后,我开始了jenkins的工作,它成功地完成了。

我只使用根用户为用户jenkins发出usermod命令。

我将jenkins用户添加到根组,并重新启动jenkins,它开始工作。

sudo usermod -a -G root jenkins
sudo service jenkins restart

我可以在不更改本地unix套接字/var/run/docker.sock上的权限的情况下使其工作。我需要做的是在构建代理节点上启用tcp连接,然后在Jenkins云配置中指定docker主机。

假设您正在使用aws映像,ssh到构建代理节点,并使用/etc/sysconfig/docker文件启用tcp端口

增加-H tcp://0.0.0.0:2376选项。

# Additional startup options for the Docker daemon, for example:
# OPTIONS="--ip-forward=true --iptables=true"
# By default we limit the number of open files per container
OPTIONS="-H tcp://0.0.0.0:2376 --default-ulimit nofile=1024:4096"

确保使用sudo service docker restart重新启动守护进程

最后,您需要告诉Jenkins使用云节点配置中配置的DOCKER_HOST环境变量默认使用tcp。注意,这不是Jenkins管道环境中的配置。

导航到Jenkins ->管理Jenkins ->管理云和节点->部分配置-> . 节点属性->环境变量

然后添加DOCKER_HOST环境变量。

注意:我通过ssh使用启动代理来实现这一工作。

似乎没有人提到,根据官方来源:https://docs.docker.com/engine/install/linux-postinstall/#manage-docker-as-a-non-root-user -你可以用sudo“pre”你的命令,它将在不创建用户或其他操作的情况下工作。例如sudo docker拉mongo -这是有效的。