LoginSignup
43
51

More than 3 years have passed since last update.

【docker】docker事始め

Last updated at Posted at 2019-12-27

目的

職場で「docker何それおいしいの?」状態に陥ったので基礎から調べて、使ってみる。

そもそもdockerってなに?

dockerとは、コンテナ型の仮想化サービスのことである。うーん、わからん。

ざっくり言うと、自分のマシンの中で別のマシンが動いてるっぽく動かすことができる優れもの。昔は、パソコン1台につき1つのサーバを動かしていて、もし他のサーバ使いたいってなったら、別の個体を用意する必要があった。例えば月に一回しか動かさないバッチがあったとしたら、月に1回しか稼働しないのに、24時間動きっぱなしのサーバをバッチ専用に用意しないといけなかった。

「場所も管理もコストもかかって困ったな〜」となったときに登場したのがdocker。わざわざ個体を用意しなくても、1台で仮想化することで複数サーバを扱えるようになった。24時間稼働していたサーバたちが「外からポチッとトリガーを与えてくれたら、その時だけ動きますよ」みたいな動きをできるようになった。環境も、ひとつのサーバに設定しておけば、それをインストールして使うことで、開発者たちの間で最低限の環境を統一することができるようになった。

<dockerを使うメリット>
1. コード化されたファイルを共有することで、どこでも誰でも同じ環境が作れる。
2. 作成した環境を配布しやすい。
3. 24時間稼働しなくても任意のタイミングで起動できる。

用語解説

dockerについて調べていて頻繁に出てきた用語たち。
これがわからないとお話にならないぞって言うくらい出てきた。

Dockerイメージ
コンテナをたちあげるために必要な設定やコマンドがひとまとめになったテンプレート。

コンテナ
dockerイメージをもとに作られたwebサーバやDBサーバなどが動く仮想環境のこと。

Dockerサーバ
コンテナとDockerイメージを管理するサーバ。

Dockerクライアント
利用者がDockerを使うために操作するコマンドやツール群のこと。

Docker Hub (レジストリ)
Dockerのイメージが公開されているサイト。コンテナからDocker Hubにアクセスしてイメージをダウンロードすると、ソフトウェアを使ったシステム構築がしやすくなる。

dockerインストール

1.公式サイトからDockerのアカウントを作ってログインする。
2.DockerHubからダウンロードしてインストールする。

3.正常にインストールされたか確認する。

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

インストールできたーーー

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/

なんかいっぱいでてきた!
runで起動した時、以下のことをしているって書いてある!

① ローカルにhello-worldというdockerイメージがあるか探す。
② なかったら DockerHub に hello-world というdockerイメージがあるか探す。
③ 見つかったらイメージをダウンロード、それを使ってコンテナを起動する。

ふむふむなるほど。やっぱり動くと嬉しい…!

docker コマンド集

動かしてみてコマンド知らないと何もできないなと思ったから、必要最低限のものを調べて叩いてみた。

docker images
Dockerイメージの一覧が確認できる。

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
hello-world         latest              fce289e99eb9        12 months ago       1.84kB

docker ps -a
ローカルのDockerコンテナの一覧が確認できる。
(-aをつけないと起動していないコンテナは表示されない。)

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                         PORTS               NAMES
943011a41e68        hello-world         "/hello"            About an hour ago   Exited (0) About an hour ago                       goofy_chebyshev

docker run #{dockerイメージ名}
ローカルに指定したイメージ名があるかどうか探しに行く。もしローカルになかったらDockerHubに探しに行く。見つかったらDockerイメージをダウンロード、それを使ってコンテナを起動する。

$ 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/

docker pull #{dockerイメージ名}
イメージのダウンロードのみ実行する。

$ docker pull hello-world
Using default tag: latest
latest: Pulling from library/hello-world
Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Status: Image is up to date for hello-world:latest
docker.io/library/hello-world:latest

docker rm #{コンテナID}
コンテナを削除する。

$ docker rm 943011a41e68
943011a41e68

docker rmi #{dockerイメージID}
イメージを削除する。

$ docker rmi fce289e99eb9
Untagged: hello-world:latest
Untagged: hello-world@sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064
Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e
Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3

DockerFile

自分でカスタマイズしたDockerイメージを作って使いたい時、DockerFileを作成、使用すると必要なDockerイメージを作ることができる。DockerFileの書き方はリファレンスがわかりやすかった。

 http://docs.docker.jp/engine/reference/builder.html
「Dockerfile リファレンス — Docker-docs-ja 17.06.Beta ドキュメント」

Dockerネットワーク

複数のコンテナ間でやりとりしたい時、Dockerネットワークを作成する。
例えば、アプリケーションを起動してDBからデータを取ってくるとき、アプリケーションのコンテナとDBのコンテナはそれぞれ独立してしまっているから接点がない。これらをつなぐためにDockerネットワークを作成し橋渡しをすると、複数のコンテナ間でやりとりすることができる。

docker network create #{ネットワーク名}
Dockerネットワークを作成できる。

$ docker network create #{ネットワーク名}

docker network inspeck #{ネットワーク名}
Dockerネットワークの詳細を確認することができる。

$ docker network inspeck #{ネットワーク名}

まとめ

手当たり次第調べてまとめてみた。コマンドも便利そうだけど、もっとDockerFileについての理解を深めたいなと思った。

43
51
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
43
51