🐳
Docker コンテナの操作について
はじめに
ここでは、Dockerの基本的な流れと主なコマンドについて記載します。
Dockerのコンテナとイメージの基本的な流れ
Dockerのコマンドにより、Dockerのコンテナとイメージは以下のように遷移します。
docker image pull(短縮形 docker pull)コマンド
Docker Hubで公開されているイメージの中からイメージをダウンロードします。
(例)
hello-worldはお試し用のイメージです。
端末
docker pull hello-world
実行結果
latest: Pulling from library/hello-world
2db29710123e: Pull complete
Digest: sha256:4e83453afed1b4fa1a3500525091dbfca6ce1e66903fd4c01ff015dbcb1ba33e
Status: Downloaded newer image for hello-world:latest
docker.io/library/hello-world:latest
docker image build(短縮形 docker build)コマンド
Dockerfileを使ってイメージを作成します。
buildコマンドのオプション(主なもの)
オプション | 説明 |
---|---|
-t | 作成するコンテナイメージの「リポジトリ:タグ名」を指定する |
docker image lsコマンド
ダウンロードしたイメージの一覧を出力します。
端末
docker image ls
実行結果
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 19 months ago 13.3kB
docker container ps -a(短縮形 docker ps -a)コマンド
コンテナの情報を出力します。
-aオプションで実行中ではないコンテナ情報も出力します。
端末
docker ps -a
実行結果
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57e464690a31 hello-world "/hello" 14 minutes ago Exited (0) 14 minutes ago youthful_moser
docker container run(短縮形 docker run)コマンド
コンテナを起動します。
(例)
端末
docker run hello-world
実行結果
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コマンドのオプション(主なもの)
オプション | 説明 |
---|---|
-it | コンテナ内で起動したbashコマンドを操作できるようにする |
-d | コンテナをバックグラウンドで起動する |
--name [コンテナ名] | コンテナ名を指定する |
--rm | コンテナを停止した際にすぐに破棄する |
-p | ポートフォワーディングを明示的に設定する |
-v [ホスト側のディレクトリパス]:[コンテナ内のディレクトリパス] | dockerコンテナとホスト間でファイルを共有する |
-pオプションの例
「-p 8080:80」とした場合、
DockerホストのTCP8080番をコンテナのTCP80番にポートフォワーディングする。
ホストのブラウザからアクセする場合は、アドレスのポート番号に8080番を入力する。
docker container stop(短縮形 docker stop)コマンド
コンテナを停止します。
docker container start(短縮形 docker start)コマンド
停止したコンテナの実行を再開します。
docker container rm(短縮形 docker rm)コマンド
コンテナを削除します。
docker container commit(短縮形 docker commit)コマンド
コンテナを変更した場合に、新たなコンテナイメージとして残します。
docker-composeコマンド
yaml形式の設定ファイル(複数のコンテナを定義できるファイル)を使うことで、一括してコンテナを管理できます。
docker runコマンド
端末
docker run -d -p 9000:8080 example/echo:latest
をyaml形式で記述すると、以下のようになります。
docker-compose.yml
version "3"
services:
echo:
image: example/echo:latest
ports:
- 9000:8080
記述 | 説明 |
---|---|
version "3" | docker-compose.ymlファイルのフォーマットバージョン。Version3 はファイルの記述定義のうち安定して利用できます。 |
echo | コンテナの名前の定義 |
image | Dockerのイメージの定義 |
ports | この例では、ホスト側の9000ポートからコンテナ側の8080ポートへポートフォワーディングする。 |
docker-compose.ymlの任意のディレクトリに格納し、docker-compose.ymlに定義されたコンテナを起動するため、以下のコマンドを実行します。
端末
docker-compose up -d
docker-compose up コマンドで起動したコンテナは、以下のコマンドで停止・削除できます。
端末
docker-compose down
docker image コマンド一覧
docker image --help
Usage: docker image COMMAND
Manage images
Commands:
build Build an image from a Dockerfile
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Display detailed information on one or more images
load Load an image from a tar archive or STDIN
ls List images
prune Remove unused images
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rm Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
docker container コマンド一覧
docker container --help
Usage: docker container COMMAND
Manage containers
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
おわりに
Dockerのコマンドオプションとmarkdown記述方法の備忘録として記載しました。
Discussion