dockerインストール

Dockerをインストールする

まずは、Dockerをインストールインストールしましょう。

開発環境はローカル環境で行うとして、ローカル環境のWindowsPCにWSL2でUbutntuを入れてその上にDockerをローカルのPCにDockerをインストールします

参考資料:

Dockerのサービスを立ち上げる

Dockerサービスを起動するために以下のコマンドを実行しましょう。

$ sudo systemctl start docker

または

$ sudo service docker start

Dockerサービスが起動しているかは、以下のコマンドで確認できます。

$ sudo service docker status
 * Docker is running

次回から自動起動にする場合は以下のコマンドで設定できます。

$ sudo systemctl enable docker

hello-worldのDockerイメージをpullする

Dockerサービスが起動しましたので、次にDocker Hubからhello-worldのイメージを取得します。

難しく感じるかもしれませんが、コマンドは単純です。

$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:9572f7cdcee8591948c2963463447a53466950b3fc15a247fcad1917ca215a2f
Status: Downloaded newer image for hello-world:latest

特にエラーが出ていなければ成功していますが、以下のコマンドでhello-worldのDockerイメージが取得できたか確認しましょう

$ docker image ls
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
hello-world                latest              fce289e99eb9        13 months ago       1.84kB

Dockerイメージの一覧を確認するコマンドです。

左から

  • リポジトリ名(Dockerイメージ名)
  • タグ(バージョン) ※latestは最新バージョンのことです
  • イメージID
  • 作成日
  • サイズ

です。

docker run コマンドでhello-worldを実行

docker run には色々とオプションがありますが、hello-worldは特にオプションなど気にせず実行してみましょう。

$ 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 Hubのhello-worldのページの「Example output」にも同じ結果が出力されているのが確認できます。 

これで、Dockerの仕組みを理解したので、まずは動かしてみてください。

参考:

Windows に Docker Desktop をインストール

Docker Desktop インストール手順

【基本】Dockerのインストール方法を一から丁寧に解

nginx環境の場合は以下の記事を参考にしてください。

Docker学習 - nginxコンテナを起動してブラウザから確認しよう