无法在 unix:///var/run/docker.sock.( Gitlab ) 连接到 Docker 守护进程

Cannot connect to the Docker daemon at unix:///var/run/docker.sock.( Gitlab )

我有一个安装了 Docker 的 AWS 实例。有些容器 running.I 在 docker.

中设置了一个 Laravel 项目

我可以通过 AWS IP 地址和 DNS 地址 (GoDaddy) 访问此 Web 应用程序。

我还设计了gitlabCI/CO来发布代码到AWS实例

当我尝试通过 Gitlab 管道推送代码时,我在管道中遇到以下错误。

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

我检查了 docker,正确的是 运行。请提供任何线索。

.gitlab-ci.yml

http://pastie.org/p/7ELo6wJEbFoKaz7jcmJdDp

管道在部署时失败-api-staging: -> 脚本 -> scripts/ci/build

构建脚本

http://pastie.org/p/1iQLZs5GqP2m5jthB4YCbh

部署脚本

http://pastie.org/p/2ho6ElfN2iWRcIZJjQGdmy

据我所知,您已经在 EC2 实例上直接安装并注册了 GitLab runner。

我认为问题在于您尚未授予 GitLab Runner 用户使用 Docker.

的权限

来自官方Docker文档:

The Docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The Docker daemon always runs as the root user.

If you don’t want to preface the docker command with sudo, create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group.

好吧,GitLab Runners 在 运行 任何 CI/CD 管道时默认使用用户 gitlab-runner,并且该用户不会使用 sudo(也不是应该在 sudoers 文件中!)所以我们必须正确配置它。

首先,在注册GitLan Runner的EC2上创建一个Docker组:

sudo groupadd docker

然后,我们要将用户 gitlab-runner 添加到该组:

sudo usermod -aG docker gitlab-runner

我们将验证 gitlab-runner 用户是否确实可以访问 Docker:

sudo -u gitlab-runner -H docker info

现在你的流水线应该可以毫无问题地访问 unix:///var/run/docker.sock 下的 Unix 套接字了。

如果使用 Docker Runners

的额外步骤

如果您在运行器中使用 Docker 执行器,您现在必须将 Unix 套接字安装到您正在使用的 Docker 图像上。

[[runners]]
    url = "https://gitlab.com/"
    token = REGISTRATION_TOKEN
    executor = "docker"
    [runners.docker]
        tls_verify = false
        image = "docker:19.03.12"
        privileged = false
        disable_cache = false
        volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]

请特别注意 volume 子句中的内容。