Get started with Docker Desktop for Macをやってみる


Get started with Docker Desktop for Macをやってみる

Get started with Docker Desktop for Mac のサイトを参考に環境を用意していきます。
https://docs.docker.com/docker-for-mac/

このサイトでは、Docker Desktop for Macのインストールが完了したところからスタートしているようです。
Docker Desktop for Mac のインストール手順については、Install Docker Desktop on Mac を参考に事前にインストールしておく必要があります。

1. インストールしたDockerのバージョンを確認

$ docker --version
Docker version 19.03.5, build 633a0ea

2. Dockerコンテナ hello-world を実行して正常に機能するか確認

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete 
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Downloaded newer image for hello-world:latest

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/

ちなみにhello-worldコンテナは、メッセージの出力が完了すると勝手にコンテナを停止するため、上記動作確認をしてから、コンテナを停止するコマンドを意図的に実行して停止処理を行う必要はありません。

3. Dockerized web serverなるものを起動して動作を確認

nginxのDockerイメージをDocker Hubから取得して、80ポートで接続できるように起動して、Webブラウザから動作が確認できるか検証するようです。

$ docker run --detach --publish=80:80 --name=webserver nginx
99484ed681dce86188f5e3be3b862cbfdcea3085ccb4a5b02079c0724f28b356

コマンドが実行できたら、
http://localhost
にアクセスして、Webサーバーが起動していることを確認します。

4. Webサーバーが起動している状態でコンテナの詳細を確認

$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
99484ed681dc        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        0.0.0.0:80->80/tcp   webserver

5. 動作検証で利用したコンテナなどを削除

$ docker container stop webserver
webserver

$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
99484ed681dc        nginx               "nginx -g 'daemon of…"   3 minutes ago       Exited (0) 9 seconds ago                        webserver
5bc2d6b367f9        hello-world         "/hello"                 16 minutes ago      Exited (0) 16 minutes ago                       dreamy_noether

$ docker container rm webserver
webserver

Get started with Docker Desktop for Macでは、Docker Imageのnginxも削除していますが、私の環境では他の開発環境で利用しており、必要なため削除はしません。

1-6. Kubernetesサーバーをインストール(有効化)

Docker Desktop for Macには、デフォルトでKubernetesサーバーが含まれています。
ただ、インストール直後の状態では、Kubernetesサーバーが有効化されていないため、利用するためにはいくつかの手順を踏む必要があります。

6-1. Preferencesのメニューを開く

6-2. KubernetesタブでEnable Kubernetesにチェックを付けてApply⇨Install

画面右下に「Kubernetes is running」と表示されればOKです。
5分ぐらい時間が掛かった。。

6-3. Kubernetes -> docker-desktopにチェックを付ける

6-4. Restart

以上でDocker Desktop for MacでのKubernetes環境が整いました。