我是码头工人的新手。我只是试着在我的本地机器(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/

其他回答

2018-08-19

我已经被这个问题困了好几天了,因为我还没有找到一个完整的答案,关于为什么和如何,我将为其他偶然发现同样问题的人发布一个答案,上面的答案不起作用。

以下是在docker中运行Jenkins时的3个关键步骤:

You mount the socket /var/run/docker.sock to the jenkins container in order to be able to use the docker from the host. You have to install docker inside the container in order to use it. This is a great and simple article on how to do that. Note that newer versions might already have docker installed You run sudo usermod -a -G docker jenkins in order to add jenkins to the docker group. However, here you might run into a permission problem if the host docker and the container docker don't have the same group id so it is very important to adjust the container docker's gid to be the same as the host docker gid

您可以将此作为启动脚本的一部分,也可以使用exec并手动执行:groupmod -g <YOUR_HOST_DOCKER_GID> docker。

另外,不要更改/var/run/docker的权限。Sock到777或类似的东西,因为这是一个很大的安全风险,你基本上允许每个人在你的机器上使用docker

希望这能有所帮助

2019-05-26

这对我很管用!

示例docker-compose:

version: "3"
services:
  jenkins:
    image: jenkinsci/blueocean
    privileged: true
    ports:
      - "8080:8080"
    volumes:
      - $HOME/learning/jenkins/jenkins_home:/var/jenkins_home
    environment:
      - DOCKER_HOST=tcp://socat:2375
    links:
      - socat

  socat:
     image: bpack/socat
     command: TCP4-LISTEN:2375,fork,reuseaddr UNIX-CONNECT:/var/run/docker.sock
     volumes:
        - /var/run/docker.sock:/var/run/docker.sock
     expose:
        - "2375"

我遇到过类似的问题,这是一个权限问题,这个问题的原因是Docker守护进程/服务器总是作为根用户运行,并希望您总是使用sudo来为Docker命令作序。

Docker守护进程绑定到Unix套接字而不是TCP端口。默认情况下,Unix套接字由root用户拥有,其他用户只能使用sudo访问它。

为了解决这个问题,以下是对我有效的方法:

首先,检查是否已经创建了docker组:

cat /etc/group

如果你在显示的列表中没有找到docker,那么你需要创建一个:

sudo groupadd docker

接下来,使用下面的命令确认您的用户和组:

cat /etc/group

滚动查看docker的组。应该是这样的格式

docker:x:140:promisepreston

docker是我的组,promisepreston是我的用户

现在我们可以将您的用户添加到docker组

将你的用户加入docker组,如果你想以非root用户的身份使用docker:

在你的终端上复制并运行下面的命令,而不需要以任何方式修改它,不管你想要运行或试图运行的docker image/container/命令是什么,或者是导致权限问题的:

sudo usermod -aG docker $USER

运行上面的命令后,您将需要注销并重新登录,以便重新评估您的组成员资格。然而,在Linux上,你也可以运行下面的命令来激活对组的更改(在你的终端上复制并运行下面的命令,而不以任何方式修改它,不管你想要运行或试图运行或导致权限问题的docker image/container/命令):

newgrp docker

OR

sudo systemctl restart docker

您现在可以验证您可以在没有sudo权限的情况下运行docker命令,通过再次运行导致权限问题的命令,例如(将my-command替换为您的image/container/命令的名称):

docker run my-command

对于Docker和Local文件系统文件:

如果你在本地文件系统上有一份文件的副本,那么你可以使用以下格式更改应用程序文件存储的应用程序目录的所有权:

sudo​​ ​ chown​​ ​ your_user:your_group​​ ​ -R​​ my-app-directory/

所以在我的例子中,它将是:

sudo chown promisepreston:docker -R my-app-directory/

注意:请在应用程序目录所在的父目录中运行此命令。

这是所有。

我希望这对你们有帮助

如果使用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

使用下面的dockerfile

FROM jenkins/jenkins

USER root

# Install Docker
RUN apt-get update && \
    apt-get -y install apt-transport-https \
    ca-certificates \
    curl \
    gnupg2 \
    software-properties-common && \
    curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
    add-apt-repository \
    "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
    $(lsb_release -cs) \
    stable" && \
    apt-get update && \
    apt-get -y install docker-ce


# Compose
RUN curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose



RUN usermod -aG docker jenkins
RUN usermod -aG root jenkins

USER jenkins