ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

Mac に docker をインストールする方法

Mac に Docker をインストールした時の作業の流れをまとめます。
流れとしては、以下の通りです。

  • Dockerをインストール
  • Nginxコンテナ起動
  • Nginxコンテナ停止
  • Nginxコンテナ削除
  • Macのローカルにあるイメージ確認

Mac の環境

今回構築するMacの環境は以下の通りです。

  • macOS 10.15.7 (19H2)
  • brewコマンド設定済み



作業内容

docker インストール

Homebrewでdockerをインストールします。

$ brew install docker

実行結果は以下のような感じです。

==> Downloading https://homebrew.bintray.com/bottles/docker-19.03.13.catalina.bo
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/d71d12ae8813da2487122
######################################################################## 100.0%
==> Pouring docker-19.03.13.catalina.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
🍺  /usr/local/Cellar/docker/19.03.13: 14 files, 71.7MB

Docker の GUI アプリケーションも以下のコマンドでインストールします。

$ brew cask install docker

実行結果は以下のような感じです。

==> Tapping homebrew/cask
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 484218 (delta 2), reused 2 (delta 0), pack-reused 484212
Receiving objects: 100% (484218/484218), 220.69 MiB | 4.42 MiB/s, done.
Resolving deltas: 100% (343609/343609), done.
Tapped 1 command and 3675 casks (3,793 files, 236.4MB).
==> Downloading https://desktop.docker.com/mac/stable/48506/Docker.dmg
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'docker'.
==> Installing Cask docker
==> Moving App 'Docker.app' to '/Applications/Docker.app'.
🍺  docker was successfully installed!

Docker 起動確認

docker が起動しているか以下のコマンドで確認します。

$ docker --version
Docker version 19.03.13, build 4484c46d9d

GUI側でもクジラのマークが出て来ることを確認します。
クジラのマークをクリックして「Docker Desktop is runnning」となっていれば起動中です。

hello-world で動作確認

hello-world コンテナイメージ取得して動作確認をしてみます。

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete 
Digest: sha256:4cf9c47f86df71d48364001ede3a4fcd85ae80ce02ebad74156906caff5378bc
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/

Nginx の image の取得

DockerHub から Nginx のコンテナイメージを取得できるか確認します。

$ docker pull nginx:latest
latest: Pulling from library/nginx
d121f8d1c412: Pull complete 
66a200539fd6: Pull complete 
e9738820db15: Pull complete 
d74ea5811e8a: Pull complete 
ffdacbba6928: Pull complete 
Digest: sha256:fc66cdef5ca33809823182c9c5d72ea86fd2cef7713cf3363e1a0b12a5d77500
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest

Nginx コンテナ起動

Nginx コンテナを起動してみる。

$ docker run -d -p 8080:80 --name webserver nginx
242999e7671f1f5a02911073a76cb5945267d280c31f69e0bc8f1836c1fa219c



docker プロセス確認

 docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
242999e7671f        nginx               "/docker-entrypoint.…"   3 minutes ago       Up 3 minutes        0.0.0.0:8080->80/tcp   webserver

Web ブラウザで nginx の起動確認



Nginx コンテナ停止

プロセス停止
$ docker stop webserver
webserver
プロセス停止確認

Nginxコンテナのプロセスを停止します。

$ docker ps -a   
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
08f297033d94        nginx               "/docker-entrypoint.…"   25 seconds ago      Exited (0) 2 seconds ago                       webserver

Nginx コンテナ削除

Nginx コンテナ削除
$ docker rm webserver  
webserver
Nginx コンテナ削除確認

Nginx のコンテナのプロセスが無事になくなったことを確認します。

$ docker ps -a       
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Docker コンテナのイメージ確認

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              992e3b7be046        4 days ago          133MB
hello-world         latest              bf756fb1ae65        9 months ago        13.3kB
メモ

以下のコマンドでリポジトリの詳細が確認できます。

$ docker inspect [REPOSITORY]