Docker入門
Dockerを使ってHelloWorldを実行してみます。
HelloWorldを実行してみます
DockerコンテナにはHelloWorldというコンテナがあるので、
次のコマンドを実行します。
$ docker run hello-world
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
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コンテナを実行することができます。
メッセージにはコマンド実行後の説明が表示されています。
現在のイメージとコンテナを表示
$ docker images
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d2c94e258dcb 12 months ago 13.3kB
latestタグを持ったhello-worldイメージが確認できます。
起動中のコンテナを確認
$ docker ps
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
何も表示されていないのは、hello-worldコンテナはメッセージ出力が終わるとコンテナが停止します。
なので、起動中のhello-worldはありません。
停止中のコンテナを確認する
$ docker ps -a
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
71f2f67ea17b hello-world "/hello" 4 minutes ago Exited (0) 4 minutes ago infallible_proskuriakova
停止中のコンテナの中にhello-worldのイメージが確認できます。
コンテナを削除する
$ docker rm [コンテナID]
$ docker rm 71f2f67ea17b
71f2f67ea17b
docker ps -a で記載のある CONTAINER IDを指定して削除をします。
イメージを削除する
$ docker rmi hello-world
$ docker rmi hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
Deleted: sha256:d2c94e258dcb3c5ac2798d32e1249e42ef01cba4841c2234249495f87264ac5a
Deleted: sha256:ac28800ec8bb38d5c35b49d45a6ac4777544941199075dff8c4eb63e093aa81e
最後にイメージを削除すれば跡形なくコンテナが消えてしまいます。