IT技術で仕事を減らしたい!

ITエンジニアのメモ+α

Docker 一般ユーザーでのdockerコマンドの利用

どうも、nippa です。

Linux で docker をインストールすると初期設定では root ユーザーでのみでしか利用できません。

今回は、一般ユーザーで利用する方法をまとめておきたいと思います。

環境

  • ubuntu 20.04(VM 上にインストール)

docker のインストール

iso ディスクでの ubuntu 20.04 をインストールすると、インストール時のオプションとして docker をインストールすることができます。その場合、sanp でインストールされます。

snap でインストール場合は、以下のコマンドで docker をインストール可能です

sudo snap install docker

docker グループの作成・確認

docker グループが作成されているかを以下のコマンドで確認します。

cat /etc/group | grep docker

docker グループが作成されていない場合は、以下のコマンドで作成します。id は指定しなくても問題ありません。

sudo groupadd docker --gid [グループID]

グループの確認をします。

cat /etc/group | grep docker

ユーザーに docker グループを追加する

ユーザーのグループを確認します。

groups [ユーザー名]

docker グループに含まれてない場合は、

sudo usermod -aG docker [ユーザー名]

で追加します。

一般ユーザーで docker を実行する

グループ情報を変更したので、セッションを接続したまま情報を以下のコマンドで更新します。

newgrp docker

docker を実行します。

docker run hello-world

以下のようなエラーが出る場合は、sock の権限が正しくありません。

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.24/containers/create": dial unix /var/run/docker.sock: connect: permission denied.

以下のコマンドで sock の権限を変更してください。

sudo chmod 666 /var/run/docker.sock

権限変更後、docker のテストコマンドを実行し、以下の結果が表示されれば、一般ユーザーでの実行が可能となったことになります。

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/

感想

Linux を docker をインストールするといつも設定を忘れてしまうので、記事にまとめておきました。

一般ユーザーで使えた方が便利ですので、設定してみてください。

ではでは、また次回。