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

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

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

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

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


当前回答

在我的例子中,它只是启动docker服务:

sudo service docker start

其他回答

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

错误消息

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/

方法01:—更安全的方法

sudo usermod -aG docker ${USER}

若要应用新的组成员资格,请从服务器注销并返回,或键入以下内容:

su - ${USER}

系统将提示您输入用户密码继续。 确认您的用户现在已添加到docker组,输入以下命令:

id -nG

方法02:-不建议用于公共部署(不安全)

chmod 777 /var/run/docker.sock

或使用

sudo chown root:docker /var/run/docker.sock

我的成功

sudo usermod -a -G docker $USER
reboot

我使用的是官方的jenkins docker映像(https://hub.docker.com/r/jenkins/jenkins),但我认为这个解决方案适用于我们想要在docker容器中运行docker的大多数用例。

在Docker容器中使用Docker的推荐方法是使用主机系统的Docker守护进程。这方面的好文章:https://itnext.io/docker-in-docker-521958d34efd。

处理权限问题的秘诀是在容器内为容器的用户添加权限,而不是为主机系统添加权限。默认情况下只有root用户有权限这样做,所以

docker exec -it -u root <container-name> bash
usermod -a -G docker <username>

我会做的。记得重新启动容器。

我想最简单的方法是创建一个定制的Dockerfile:

# Official jenkins image
FROM jenkins/jenkins:lts
# Swith to root to be able to install Docker and modify permissions
USER root
RUN apt-get update
# Install docker
RUN curl -sSL https://get.docker.com/ | sh
# Add jenkins user to docker group
RUN usermod -a -G docker jenkins
# Switch back to default user
USER jenkins

# Bild the image:
# sudo docker build -t yourusername/imagename .
# Run the image and mount with the followin bind mount option:
# sudo docker run --name imagename -d -p8080:8080 -v /var/run/docker.sock:/var/run/docker.sock yourusername/imagename

如果使用jenkins

用户jenkins需要添加到docker组中:

sudo usermod -a -G docker jenkins

然后重启詹金斯。

否则

如果你收到来自docker的消息而遇到堆栈溢出的问题,但你没有使用jenkins,那么错误很可能是一样的:你的非特权用户不属于docker组。

你可以:

sudo usermod -a -G docker [user]

在[user]所在的位置插入用户名。

你可以通过执行grep docker /etc/group命令来检查它是否成功,如下所示:

docker:x:998:[user]

在其中一行。

然后将您的用户组ID更改为docker(以避免不得不退出并再次登录):

newgrp docker