3
4

More than 5 years have passed since last update.

はじめてのDocker

Posted at

ほんとにDockerという言葉すらはじめてきいたというレベルから「まあこんなかんじのものだろう」くらいまでを目指したチュートリアルです。

対象

Docker未経験者
CUIコマンドならちょっとならわかるよってひと

概要

Dockerとは「コンテナ型仮想化技術」です。サーバーの仮想化という言葉はみなさんきいたことがあるんじゃにでしょうか。簡単に言うとパソコンの中でパソコン(仮想)を立ち上げるということです。
従来の仮想化というとホストOS(マシン自体にインストールされているOS)上にハイパーバイザとよばれる仮想化ソフトを利用して、ゲストOSを立ち上げるような構成でした。
Dockerの「コンテナ型仮想化」はゲストOSを立ち上げません。コンテナと呼ばれるミドルウェアなども構築されている実行環境を作成し、その中でアプリケーションを動かします。
先述のとおりゲストOSは起動させないので従来よりかなり軽い動作を実現します。

え?それだけ?
ちがいます。DockerはたちあげるコンテナをDockerfileやDocker-compose.ymlといった'設計書'に記述できます。つまりこの設計書をGitHubにあげてcloneしてもらったらそのひとも同じ環境のコンテナを立ち上げられるということです。
自分はMySQLが8系だったがチームメンバーは5系で開発してたからエラーがおこったとかいう問題がおこらないのです。
しかもDockerにはDocker専用のコンテナ共有サービス「DockerHub」も用意されます。使い方はGitHubを使ったことがある人ならそれなりに使えると思います。

詳細とかメリットとかはあげてたらきりがないので先人たちの記事を読んでください(読んでください)
とりあえず動かしてみましょう

準備

Dockerをインストールしてみよう
Mac:https://docs.docker.com/docker-for-mac/install/
Win10Pro:https://docs.docker.com/docker-for-windows/install/
Win10Home以下:https://github.com/docker/toolbox/releases

Win10Homeの方はgithubのDockerToolbox-18.09.3.exeをダウンロードしましょう。
Macの方はsudoコマンド必須です。

あとはいつもどおりのインストールです。

実践

HelloWorld!

とりあえずHelloWorld
Terminalかコマンドプロンプトを開きましょう。
Win10Homeはインストール後にデスクトップに現れたDocker Quickstart Terminal
次のコマンドを入力してみましょう

$ docker run 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:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe
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のimageないからプルしてくるね。ダウンロードできた。
コンテナ立ち上げるね。
Hello from Docker!
次は”$ docker run -it ubuntu bash”ってコマンド試してみて」
です。
言い忘れていました。imageというのは設計書とコンテナの間にはいるものです。文字通りコンテナのイメージです。一旦はそれでいいです。

いま実際に立ち上がっているコンテナを見てみましょう
$ docker container ls -aで確認できます。hello-worldは出力すると停止するコンテナなので全コンテナを表示する -a オプションがないとみられません。

 $ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
affc00b91aec        hello-world         "/hello"            5 seconds ago       Exited (0) 5 seconds ago                       affectionate_sinoussi

hello-worldというimageからつくられたaffc00b91aecというコンテナがありますね

次にイメージ一覧も見てみましょう

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

いらなくなったコンテナやイメージを削除することもできます。

$ docker container rm affc00b91aec

もしくは

$ docker container rm a

のように頭文字してでもできます。

同様にイメージも削除してみます。

$ docker image rm f
Untagged: hello-world:latest
Untagged: hello-world@sha256:0e11c388b664df8a27a901dce21eb89f11d8292f7fca1b3e3c4321bf7897bffe
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3

Ubuntu起動

つづいて、さきほどhello-worldに言われたコマンドを試してみましょう。

$ docker run -it ubuntu bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu
6abc03819f3e: Pull complete
05731e63f211: Pull complete
0bd67c50d6be: Pull complete
Digest: sha256:f08638ec7ddc90065187e7eabdfac3c96e5ff0f6b2f1762cf31a4f49b53000a5
Status: Downloaded newer image for ubuntu:latest
root@2e88c2d2aac5:/#

それっぽいですね。これはubuntuというOSが入ったコンテナです。最後にrootディレクトリにいたらログインできているということです。試しにlsしてみたらよりOSっぽいと思います

root@2e88c2d2aac5:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

ぽいですね。CUIでの操作ならここで一通りできます

root@2e88c2d2aac5:/# touch hoge
root@2e88c2d2aac5:/# ls
bin  boot  dev  etc  hoge  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

こんな感じに何かファイルをつくったとします。

コンテナから抜けるコマンド"ctrl+pq"をおして一度ぬけてもう一度入ってみましょう。

root@2e88c2d2aac5:/# ls
bin  boot  dev  etc  hoge  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@2e88c2d2aac5:/#
(省略) MINGW64 /c
$ docker container ls -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2e88c2d2aac5        ubuntu              "bash"              21 minutes ago      Up 21 minutes                           heuristic_hofstadter

keita@DESKTOP-MIO6JRP MINGW64 /c
$ docker container exec -it 2 bash
root@2e88c2d2aac5:/# ls
bin  boot  dev  etc  hoge  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

hogeが入っていますねね。先ほど編集したコンテナに再度ログインできたということです。ログインする方法としてexecのほかにattachというものがありますがこちらは親プロセスにつなぎに行くので極力つかわないようにしましょう。
execログインの際に抜けたいときはexitコマンドを
root@2e88c2d2aac5:/# exit
exit

ここまででdocker自体の大まかなできることは分かったと思います。

このままの流れでdockerfileとdocker-composeにも触れてみましょう

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4