ubuntu 使用docker-compose而不使用sudo不起作用

sy5wg1nm  于 2023-06-05  发布在  Docker
关注(0)|答案(2)|浏览(399)

最近有人告诉我,使用sudo运行dockerdocker-compose是一个很大的nono,我必须创建/添加我的用户到docker组,以便在没有sudo的情况下运行dockerdocker-compose命令。我照做了,按照documentation here
现在,docker通过我的用户正常运行。例如:

~$ docker run hello-world

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:df5f5184104426b65967e016ff2ac0bfcd44ad7899ca3bbcf8e44e4461491a9e
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/

但是当我尝试运行docker-compose时,我得到一个Permission Denied

~$ docker-compose --help

-bash: /usr/local/bin/docker-compose: Permission denied

你能解释一下这是怎么回事吗?我以为有一个docker组就可以使用这些命令,因为二进制文件属于这个组,但实际上它们不属于这个组,它们只属于root……

~$ ls -al /usr/bin/docker*
-rwxr-xr-x 1 root root  71706288 Jul 23 19:36 /usr/bin/docker
-rwxr-xr-x 1 root root    804408 Jul 23 19:36 /usr/bin/docker-init
-rwxr-xr-x 1 root root   2944247 Jul 23 19:36 /usr/bin/docker-proxy
-rwxr-xr-x 1 root root 116375640 Jul 23 19:36 /usr/bin/dockerd
~$ ls -al /usr/local/bin/
total 12448
drwxr-xr-x  2 root root     4096 May 26 11:08 .
drwxr-xr-x 10 root root     4096 May 14 19:36 ..
-rwxr--r--  1 root root 12737304 May 26 11:08 docker-compose

那么,这是如何工作的?如何为属于docker组的用户启用docker-compose

thtygnil

thtygnil1#

sudo chmod a+x /usr/local/bin/docker-compose

截至2023年6月,docker-compose命令已被弃用,以支持compose插件。
https://docs.docker.com/compose/install/linux/
安装Compose插件

sudo apt-get update
sudo apt-get install docker-compose-plugin

...

docker compose ps # note that docker and compose are now two words.

将打开您的权限。
docker-compose只是一个 Package 器,它使用了一个外部的 docker daemon,就像docker命令实际上并不运行任何东西,而是给一个 docker daemon 一个命令。
您可以使用DOCKER_HOST变量更改与之通信的docker守护进程。默认情况下为空;当它为空时,dockerdocker-compose都假定它位于/var/run/docker.sock
根据dockerd文档:
默认情况下,unix域套接字(或IPC套接字)在/var/run/docker.sock创建,需要root权限或docker组成员资格。
这是通过向套接字授予对docker组的读写权限来实现的。

$ ls -l /var/run/docker.sock 
srw-rw---- 1 root docker 0 nov.  15 19:54 /var/run/docker.sock

https://docs.docker.com/engine/install/linux-postinstall/中所述,要将用户添加到docker组,您可以这样做:

sudo usermod -aG docker $USER # this adds the permissions
newgrp docker # this refreshes the permissions in the current session

也就是说,将dockersudo一起使用 * 与将其与docker组一起使用 * 相同,因为对/var/run/docker.sock进行访问 * 等同于对/var/run/docker.sock进行完全根访问:
https://docs.docker.com/engine/install/linux-postinstall/
docker组赠款与root用户等同的权限。有关这如何影响系统安全性的详细信息,请参阅Docker Daemon Attack Surface。
如果root权限是系统的安全问题,则会提到另一个页面:
要在没有root权限的情况下运行Docker,请参阅Run the Docker daemon as a non-root user (Rootless mode)
docker由多个元素组成:https://docs.docker.com/get-started/overview/
首先,有客户:

$ type docker
docker is /usr/bin/docker
$ dpkg -S /usr/bin/docker
docker-ce-cli: /usr/bin/docker

在安装docker-ce-cli软件包时,可以看到docker命令已安装。
在这里,ce 代表 * 社区版 *。
docker cli与docker守护进程(也称为dockerd)通信。
dockerd是一个守护进程(服务器),默认情况下暴露unix套接字/var/run/docker.sock;默认权限为root:docker
还涉及其他组件,例如dockerd使用containerdhttps://containerd.io/
剩下的就是基本的linux权限管理:

  • 操作docker守护进程与在该机器上拥有root权限是一样的。
  • 要操作docker守护进程,您需要能够从它监听的套接字读取和写入;在您情况下,它是/var/run/docker.sock无论你是不是sudoer都不会改变任何事情
  • 要能够从/var/run/docker.sock读取和写入,您必须是root或在docker组中。
  • docker-compose是另一个CLI,它与docker具有相同的要求。
2izufjch

2izufjch2#

对我有用的是通过运行(作为root,通过sudo)将自己添加到'docker'组:

# usermod -a -G docker` *myUserName*

您可能需要重新登录,因为当前shell可能还不“知道”被添加到docker组。
但如果您不想重新登录,可以运行以下命令

newgrp docker

https://docs.docker.com/engine/install/linux-postinstall/

相关问题