Dockerのインストール方法からコンテナの立ち上げまで解説

dockerのインストール方法

Dockerは、「コンテナ仮想化」を用いたOSレベルの仮想化により仮想環境を構築するソフトウェアです。
ホストとなるPCやサーバー上に仮想的なOSを立ち上げることで、新規でOS・環境を立ち上げられます。

目次

Dockerのインストール

既存のパッケージのリストを更新します。

$ sudo apt update

aptがHTTPS経由でパッケージを使用できるように、必要となるパッケージをインストールします。

$ sudo apt install apt-transport-https ca-certificates curl software-properties-common
実行に失敗した場合

上記コマンドを実行した際に以下のエラーが確認されることがあります。

Waiting for cache lock: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 2739 (unattended-upgr).

「ロックファイル」がLockされている状態なので、プロセスをkillします。

$ sudo kill -9 2739

公式DockerリポジトリのGPGキーをシステムに追加します。

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
GPGキーとは

「GnuPG」(GNU Privacy Guard)という暗号化ソフトで生成される公開鍵です。Linuxの場合、aptコマンドやyumコマンドを使ってインターネットから入手できるパッケージが正しい配布先のものかどうかのチェックなどに利用しています。

DockerリポジトリをAPTソースに追加します。

$ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

追加されたリポジトリからDockerパッケージでパッケージデータベースを更新します。

$ sudo apt update

Dockerをインストールします。

$ sudo apt install docker-ce

dockerデーモンが起動し、サービスが立ち上がっていることを確認してください。

$ sudo systemctl status docker
Active: inactive (dead)になっている場合
$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: inactive (dead)
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com

上記のようにインアクティブの場合はサービスの再起動を実施してください。

$ sudo systemctl restart docker

dockerコマンドが正常に実行できているか確認します。

$ docker run hello-world

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のインストールは以上になります!お疲れさまでした。

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

ユーザーをdockerグループに追加します。

$ sudo usermod -aG docker ${USER}

上記の設定を適用するためには、サーバーからログアウトして再度ログインするか、以下を入力します。

$ su - ${USER}

追加できているかは以下のコマンドで確認できます

$ id -nG

\ より詳しく知りたい方はこちらの本がおすすめです /

¥2,992 (2022/10/24 23:22時点 | Amazon調べ)
\楽天ポイント5倍セール!/
楽天市場
¥2,090 (2022/10/24 23:42時点 | Amazon調べ)
\楽天ポイント5倍セール!/
楽天市場
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次