Share feedback
Answers are generated based on the documentation.

Linux における Docker Engine インストール後の作業

ここに示すインストール後の手順は任意の作業であって、ホストマシンに Linux を用いている場合に Docker をより効率よく動作させる方法について説明しています。

root ユーザー以外での Docker 管理

Docker デーモンは TCP ポートではなく Unix ソケットにバインドされます。 デフォルトでその所有者は root ユーザーであるため、他のユーザーは sudo を使ってアクセスすることになります。 Docker デーモンは常に root ユーザーが起動しています。

When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group. On some Linux distributions, the system automatically creates this group when installing Docker Engine using a package manager. In that case, you don't need to create the group manually.

There are two ways to run docker commands without sudo while the Docker daemon runs as root:

警告

docker グループは、そのユーザーに対してルートレベルの権限を付与します。 このことがシステムセキュリティ上でどのような意味を持つのかについては Docker Daemon Attack Surface を参照してください。

メモ

ルート権限なしに Docker をインストールする場合は 非ルートユーザーとして Docker デーモンを起動する (rootless モード) を参照してください。

Add your user to the docker group

docker グループを生成してユーザーを追加します。

  1. docker グループを生成します。

    $ sudo groupadd docker
    
  2. ユーザーを docker グループに追加します。

    $ sudo usermod -aG docker $USER
    
  3. いったんログアウトしてからログインし直してください。 グループに属することが認識されるようにするためです。

    仮想マシン上で Linux を稼働させている場合、仮想マシンを再起動して設定を有効にする必要があるかもしれません。

    グループ変更を有効にするために、以下のコマンドを実行することもできます。

    $ newgrp docker
    
  4. sudo がなくても docker コマンドが実行できることを確認します。

    $ docker run hello-world
    

    このコマンドはテストイメージをダウンロードして、コンテナー内で実行します。 コンテナーが起動すると、メッセージを表示して終了します。

    docker グループへのユーザー追加を行わずに sudo を使って Docker CLI コマンドを実行していたときは、以下のエラーが出ていたかも知れません。

    WARNING: Error loading config file: /home/user/.docker/config.json -
    stat /home/user/.docker/config.json: permission denied

    このエラーは ~/.docker/ ディレクトリのパーミッションが不適切であることを示しています。 これは上において sudo コマンドを用いていたからです。

    この問題を解消するには、1 つには ~/.docker/ ディレクトリをいったん削除することです。 (このディレクトリは自動的に再作成されます。ただし追加設定している内容は失われます。) あるいは以下のコマンドのようにして、所有者とパーミッションを変更することです。

    $ sudo chown "$USER":"$USER" /home/"$USER"/.docker -R
    $ sudo chmod g+rwx "$HOME/.docker" -R
    

Access the docker group on demand

Group passwords are a legacy Unix access-control mechanism, but they can be useful for gating Docker access on a single-user workstation. Like sudo, this method adds an explicit password step before privileged access. Unlike running sudo docker, newgrp keeps the Docker CLI running under your user ID and grants access through the shell's primary group, so the CLI doesn't access its configuration as root.

Permanent membership in the docker group gives every process in your login session access to the Docker socket. The Docker-enabled shell and its descendants inherit access to the Docker socket. This reduces ambient access from applications running elsewhere in your login session. To configure this access, keep your user out of the group, set a group password, and use newgrp to start a Docker-enabled shell.

警告

A group password reduces ambient access to the Docker socket, but it doesn't reduce the root-level privileges granted after access is authorized. Group passwords are also shared secrets and don't provide per-user accountability. This method isn't a security boundary against malicious code running as your user. Such code can modify user-writable shell configuration, commands, or scripts that you later use from the Docker-enabled shell and gain Docker access after you authenticate. Don't rely on a group password to contain untrusted code or protect a compromised login session. This configuration is most suitable for a single-user workstation. For stronger isolation, use Rootless mode or run Docker in a virtual machine.

This procedure requires gpasswd and newgrp. The package names for these commands vary by Linux distribution. Verify that both commands are available:

$ command -v gpasswd newgrp
/usr/bin/gpasswd
/usr/bin/newgrp

To require a password for Docker access:

  1. Create the docker group if it doesn't exist:

    $ sudo groupadd --force docker
    

    The --force option makes the command succeed when the group already exists.

  2. If your user is a member of the docker group, remove the membership:

    $ sudo gpasswd --delete "$USER" docker
    

    Sign out of the desktop or SSH session completely, then sign back in. Group membership remains in the credentials of existing processes, so opening a new terminal isn't sufficient.

    Verify that docker is absent from the group list before continuing:

    $ id -nG
    user wheel
    

    Your group list varies by system, but it must not include docker.

  3. Set a dedicated password for the docker group:

    $ sudo gpasswd docker
    Changing the password for group docker
    New Password:
    Re-enter new password:
    

    Don't add your user back to the group. Users configured as group members can use newgrp without entering the group password.

  4. Start a child shell with docker as its primary group:

    $ newgrp docker
    Password:
    

    Verify that the shell still uses your user ID and has docker as its primary group, then test Docker access:

    $ id -un
    user
    $ id -gn
    docker
    $ docker run --rm hello-world
    

    Commands and applications started from this shell inherit access to the Docker socket. Applications that were already running outside the shell don't gain access.

    注意

    Files and directories created from this shell normally have docker as their group owner. Use this shell only for Docker-related commands, or verify the group ownership of files you create.

  5. Exit the Docker-enabled shell when you finish:

    $ exit
    

    Verify that docker is no longer in the original shell's group list:

    $ id -nG
    user wheel
    
注意

Exiting the shell doesn't revoke access from background, detached, or daemonized processes started inside it. Those processes retain the docker group until they exit.

To change the group password, run sudo gpasswd docker again.

Disable password-based entry

To stop using the shared password and require configured group membership, run:

$ sudo gpasswd --restrict docker

This reverses the password-gated setup. Afterward, only users configured as members of the docker group can enter it. The change affects new authorization attempts; it doesn't terminate existing Docker-enabled shells or their descendant processes.

gpasswd manages local /etc/group and /etc/gshadow files. Systems using LDAP, NIS, or another identity service require that service's group-management mechanism.

ブート時の Docker 起動設定

最近の Linux ディストリビューションでは systemd を使ってシステムブート時のサービス起動の管理を行っています。 Debian や Ubuntu では、デフォルトで Docker サービスがブート時に起動するように設定されています。 他のディストリビューションにおいて Docker や Containerd をブート起動するには、以下のコマンドを用います。

$ sudo systemctl enable docker.service
$ sudo systemctl enable containerd.service

この動作を停止させるには、逆に disable を用います。

$ sudo systemctl disable docker.service
$ sudo systemctl disable containerd.service

systemd におけるユニットファイルを使って Docker サービスの自動起動を設定します。 そこではたとえば HTTP プロキシーを設定したり、Docker ランタイムファイル群が別ディレクトリや別パーティションにある場合にはそれを指定するなど、カスタマイズ状況に応じて設定を行います。 具体的な例は プロキシーを利用するデーモン設定 を参照してください。

デフォルトのログドライバー設定

Docker では ログドライバー を通じて、ホスト上に稼動するすべてのコンテナーからログを収集して参照する機能が提供されています。 デフォルトのログドライバーはjson-fileであり、ホスト上のファイルシステム内に JSON 形式のファイルとしてログデータを保存します。 時間の経過とともにログファイルはサイズが増大するため、気づかないうちにディスクリソースを浪費することにつながります。

ログデータによりディスクが浪費されるこの問題を解消するには、以下のいずれかを検討してください。

次のステップ

  • Docker を使ってはじめよう を確認してみてください。 そこではイメージのビルド方法や、コンテナー化アプリケーションとしてそれを実行する方法について説明しています。