Dockerコマンドをsudoなしで実行する方法

この記事は最終更新日から3年以上が経過しています。

なぜDockerコマンドにはsudoが必要なのか?

https://docs.docker.com/installation/ubuntulinux/#create-a-docker-group

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という名のグループを作ってユーザをそこに所属させればOKです。

やり方

# dockerグループがなければ作る
sudo groupadd docker

# 現行ユーザをdockerグループに所属させる
sudo gpasswd -a $USER docker

# dockerデーモンを再起動する (CentOS7の場合)
sudo systemctl restart docker

# exitして再ログインすると反映される。
exit


+ Recent posts