【Docker】MacにDockerをインストールしてみた & 少し操作してみた
必要に迫られたのでそろそろdockerを勉強してみた結果を記します。
今回の環境
やってみる
Docker for Macをインストールする
Docker for Macは公式サイトでもインストールできるみたいだけど、今回はhomebrewを利用してインストールしてみます。
$ brew search docker ==> Formulae docker docker-compose docker-gen docker-machine-driver-hyperkit docker-machine-parallels docker-clean docker-compose-completion docker-ls docker-machine-driver-vultr docker-squash docker-cloud docker-credential-helper docker-machine docker-machine-driver-xhyve docker-swarm docker-completion docker-credential-helper-ecr docker-machine-completion docker-machine-nfs docker2aci ==> Casks docker docker-toolbox homebrew/cask-versions/docker-edge $ brew cask install docker Updating Homebrew... ==> Satisfying dependencies ==> Downloading https://download.docker.com/mac/stable/28905/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.appを起動し、ステータスが「Docker Desktop is running」となれば起動完了です。
念の為Terminalからコマンドの確認がてらバージョンを表示してみます。
$ docker --version Docker version 18.09.0, build 4d60db4 $ docker-machine version docker-machine version 0.16.0, build 702c267f $ docker-compose version docker-compose version 1.23.1, build b02f1306 docker-py version: 3.5.0 CPython version: 3.6.6 OpenSSL version: OpenSSL 1.1.0h 27 Mar 2018
Hello Worldしてみる
公式サイトにある手順に沿い、Terminalでhello-world
というDocker Imageをインストールします。
docker run
で起動するDocker Imageがローカルに無い場合、Docker Hubから自動的にPullしてくれるようですね、便利。
$ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world d1725b59e92d: Pull complete Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788 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
のDocker Imageをインストールします
$ docker run -d -p 80:80 --name webserver nginx Unable to find image 'nginx:latest' locally latest: Pulling from library/nginx a5a6f2f73cd8: Pull complete 67da5fbcb7a0: Pull complete e82455fa5628: Pull complete Digest: sha256:31b8e90a349d1fce7621f5a5a08e4fc519b634f7d3feb09d53fac9b12aa4d991 Status: Downloaded newer image for nginx:latest 11b1835537b3da82eeb655a4c43ad5010eeb4fcb3fff626501ba795e9469fa0d
【構文】
docker run -d -p 80:80 --name {エイリアス名} nginx
コマンド, オプション | 内容 |
---|---|
docker run | Docker Imageからコンテナを起動する |
-d | コンテナをデタッチド・モードで起動する |
-p 80:80 | ポートフォワーディングの設定を行う。この例ではホスト側80番ポートの通信をコンテナ側80番ポートに転送する |
--name | コンテナのエイリアス名を設定する |
nginx | この部分はDocker Image名を設定する |
nginx
が起動したらブラウザを開き、http://localhost
にアクセスしてみます。
「Welcome to nginx!」が表示されれば完了です。
ちなみにdocker container ls
を利用することで現在コンテナが起動しているかも確認できるぽい。
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 11b1835537b3 nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp webserver
コンテナを確認したり止めたりイメージを削除したり
コンテナの確認
基本はdocker container ls
を利用します。
-a
オプションをつけることで起動していないコンテナも一覧表示します。
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 11b1835537b3 nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp webserver $ $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 11b1835537b3 nginx "nginx -g 'daemon of…" About an hour ago Up About an hour 0.0.0.0:80->80/tcp webserver 2a79441c51b8 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago adoring_volhard
コンテナの停止
docker container stop {コンテナ名}
でコンテナを停止することができます。
$ docker container stop webserver webserver # 停止したか確認 $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 11b1835537b3 nginx "nginx -g 'daemon of…" 2 hours ago Exited (0) 20 seconds ago webserver
コンテナの削除
docker container rm {コンテナ名}
でコンテナを削除することができます。
$ docker container rm webserver webserver # webserverコンテナが削除されていることを確認 $ docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2a79441c51b8 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago adoring_volhard
イメージの確認
docker image ls
でローカルにあるイメージを一覧表示します。
Owner-no-MacBook-Air:docker_sample owner$ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest e81eb098537d 3 days ago 109MB hello-world latest 4ab4c602aa5e 2 months ago 1.84kB
イメージの削除
docker image rm {イメージ名}
でローカルにあるイメージを削除できます。
$ docker image rm nginx Untagged: nginx:latest Untagged: nginx@sha256:31b8e90a349d1fce7621f5a5a08e4fc519b634f7d3feb09d53fac9b12aa4d991 Deleted: sha256:e81eb098537d6c4a75438eacc6a2ed94af74ca168076f719f3a0558bd24d646a Deleted: sha256:7055505a92c39c6f943403d54a1cda020bfeb523b55d9d78bfe1dad0dd32bb2d Deleted: sha256:378a8fcc106dc4d3a9f2dc0b642b164e25de3aab98a829e72b2d8c0cf0bad8ee Deleted: sha256:ef68f6734aa485edf13a8509fe60e4272428deaf63f446a441b79d47fc5d17d3 # nginxが削除されていることを確認 $ docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest 4ab4c602aa5e 2 months ago 1.84kB
やってみて
初心者でも割とすんなり出来ました。 これだけでは案件に全然使えないので、もっと色々調べなきゃですね...。
参考サイト
- Get started with Docker for Mac | Docker Documentation
- Mac に homebrew で docker 入れたい 2017年末版 - Qiita
- DockerをMacにインストールする (更新:2018/5/14) - Qiita
- 作者: 山田明憲
- 出版社/メーカー: 技術評論社
- 発売日: 2018/08/25
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (1件) を見る
Docker実践入門――Linuxコンテナ技術の基礎から応用まで (Software Design plus)
- 作者: 中井悦司
- 出版社/メーカー: 技術評論社
- 発売日: 2015/09/26
- メディア: 大型本
- この商品を含むブログ (2件) を見る