docker命令默认只能由root用户使用,但可以赋权给其他用户。
官方说明如下:

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 access it with sudo. For this reason, docker daemon always runs as the root user. To avoid having to use sudo when you use the docker command, create a Unix group called docker and add users to it. When the docker daemon starts, it makes the ownership of the Unix socket read/writable by the docker group.
docker守护程序绑定到Unix套接字而不是TCP端口。 默认情况下,Unix套接字由用户root拥有,其他用户可以通过sudo访问它。 因此,docker守护程序始终以root用户身份运行。 为了避免在使用docker命令时必须使用sudo,请创建一个名为docker的Unix组并将用户添加到其中。 docker守护程序启动时,它将使docker组可以读取/写入Unix套接字的所有权。

通过官方说明可以了解到: 只需要创建一个名为docker的用户组,然后把非root用户放入这个组中,最后重启docker,就可以使非root用户使用docker。

通过root用户创建docker用户组

  1. groupadd docker

将非root用户加入docker用户组

  1. usermod -G docker root用户名

假设我们想让名为ys的用户使用docker,则只需将ys加入docker用户组,如下:
usermod -G docker ys

重启docker服务

  1. service docker restart

验证

  1. [root@localhost application]# su ys // 切换到ys用户
  2. [ys@localhost application]$ docker images // ys用户可以使用docker命令了
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. web 1.0 0d1593ee1661 19 hours ago 168MB
  5. jdk-centos v2 7468a63acbb2 23 hours ago 740MB
  6. centos 7 8652b9f0cb4c 3 months ago 204MB
  7. java 8-alpine 3fd9dd82815c 3 years ago 145MB