Docker:【コマンド集】最初に役に立ったもの


はじめに

  • Dockerの基本コマンド集
    • Dockerを勉強し始めた頃に使用したもの
    • 作成や実行、確認などの操作方法
  • ここに記載したこと
    • イメージ(Dockerイメージ)関連
    • コンテナ実行関連

Dockerイメージ関連

Dockerイメージ取得(リポジトリからダウンロード)

最新バージョンを取得する

sudo docker pull <REPOSITORY>
sudo docker pull <REPOSITORY>:latest
$ sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB

$ sudo docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
59bf1c3509f3: Pull complete
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest

$ sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
  • docker pull
    • リポジトリ(Docker HUB)からダウンロード
  • docker images
    • 今あるイメージの一覧を表示
    • 「|sort」で表示をLinuxコマンドのsortを使って並び替え
  • alpline:latest
    • Alpine Linux最新版
    • デフォルト(省略した場合は:latestになる)

バージョンを指定してイメージ取得

sudo docker pull <REPOSITORY>:<TAG>
$ sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB

$ sudo docker pull alpine:3.5
3.5: Pulling from library/alpine
8cae0e1ac61c: Pull complete
Digest: sha256:66952b313e51c3bd1987d7c4ddf5dba9bc0fb6e524eed2448fa660246b3e76ec
Status: Downloaded newer image for alpine:3.5
docker.io/library/alpine:3.5

$ sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      3.5       f80194ae2e0c   3 years ago    4MB
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
  • docker pull
    • リポジトリ(Docker HUB)からダウンロード
  • docker images
    • 今あるイメージの一覧を表示
    • 「|sort」で表示をLinuxコマンドのsortを使って並び替え
  • apline:3.5
    • バージョン指定(3.5)

Dockerイメージ表示

Dockerイメージを一覧表示

sudo docker images
$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED              SIZE
<none>                      <none>    092864290ad5   About a minute ago   5.59MB
alpine                      latest    c059bfaa849c   2 months ago         5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago         13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago          104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago          191MB
  • docker images
    • 今あるイメージの一覧取得

Dockerイメージをソートして表示

sudo docker images|sort
$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
alpine                      3.5       f80194ae2e0c   3 years ago    4MB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB

$ sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      3.5       f80194ae2e0c   3 years ago    4MB
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
  • ソートなしとソートありを並べた
  • REPOSITORY列の順に並んでいることが分かる
  • |sort
    • 「|」 …… パイプ。処理をつなぐLinuxコマンドの記号
    • DockerコマンドとLinuxコマンドをつなげてソートしている

一覧表示でイメージIDのみ表示

sudo docker images -q
$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
<none>                      <none>    bc4bb1d191cf   9 seconds ago   5.59MB
<none>                      <none>    092864290ad5   2 minutes ago   5.59MB
alpine                      latest    c059bfaa849c   2 months ago    5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago    13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago     104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago     191MB

$ sudo docker images -q
bc4bb1d191cf
092864290ad5
c059bfaa849c
feb5d9fea6a5
5b117edd0b76
f589ccde7957
  • 「-q」なしと「-q」ありを並べた
  • -q
    • IMAGE ID列のみを表示する

Dockerイメージ作成

タグ名なし

sudo docker build .
$ sudo docker build .
Sending build context to Docker daemon   5.12kB
Step 1/2 : FROM alpine
 ---> c059bfaa849c
Step 2/2 : CMD ["/bin/sh", "-c", "echo AI Self Studying by $AUTHOR."]
 ---> Running in 1e6cc53e03a1
Removing intermediate container 1e6cc53e03a1
 ---> 8494f9e83001
Successfully built 8494f9e83001

$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED          SIZE
<none>                      <none>    8494f9e83001   14 seconds ago   5.59MB
alpine                      latest    c059bfaa849c   2 months ago     5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago     13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago      104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago      191MB
  • docker build
    • Dockerイメージを作成する
    • イメージ作成にはDockerファイルが必要になる
  • 「.」
    • カレントディレクトリにあるDockerファイルを読み込む
    • Dockerファイル名は「Dockerfile」
  • その他のオプションなし
    • REPOSITORY列、TAG列は<none>になる
    • このイメージを一意に特定する情報はイメージID(IMAGE ID列)

タグ名指定

sudo docker build -t futa/myapp .
$ sudo docker build -t futa/myapp .
Sending build context to Docker daemon   5.12kB
Step 1/2 : FROM alpine
 ---> c059bfaa849c
Step 2/2 : CMD ["/bin/sh", "-c", "echo AI Self Studying by $AUTHOR."]
 ---> Running in 8b184c78ef3f
Removing intermediate container 8b184c78ef3f
 ---> f713b07373f4
Successfully built f713b07373f4
Successfully tagged futa/myapp:latest

$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED          SIZE
futa/myapp                latest    f713b07373f4   14 seconds ago   5.59MB
alpine                      latest    c059bfaa849c   2 months ago     5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago     13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago      104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago      191MB
  • docker build
    • Dockerイメージを作成する
    • イメージ作成にはDockerファイルが必要になる
  • -t
    • タグ名を指定
    • 「futa/myapp」にした

タグ名を複数指定

sudo docker build -t futa/myapp:1.0.2 -t futa/myapp:latest .
$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB

$ sudo docker build -t futa/myapp:1.0.2 -t futa/myapp:latest .
Sending build context to Docker daemon   5.12kB
Step 1/2 : FROM alpine
 ---> c059bfaa849c
Step 2/2 : CMD ["/bin/sh", "-c", "echo AI Self Studying by $AUTHOR."]
 ---> Running in 9c905d9c55f6
Removing intermediate container 9c905d9c55f6
 ---> 6c5662e10c3f
Successfully built 6c5662e10c3f
Successfully tagged futa/myapp:1.0.2
Successfully tagged futa/myapp:latest

$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED         SIZE
futa/myapp                1.0.2     6c5662e10c3f   6 seconds ago   5.59MB
futa/myapp                latest    6c5662e10c3f   6 seconds ago   5.59MB
alpine                      latest    c059bfaa849c   2 months ago    5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago    13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago     104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago     191MB
  • 「-t」指定2つ
    • 複数のイメージができる

Dockerイメージ削除

イメージIDを指定して削除

docker rmi <IMAGE_ID>
$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED              SIZE
<none>                      <none>    aa12fc327385   About a minute ago   5.59MB
alpine                      latest    c059bfaa849c   2 months ago         5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago         13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago          104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago          191MB

$ sudo docker rmi aa12fc327385
Deleted: sha256:aa12fc3273859d13a0a1f105c1edb6d98057c75bbedc2dbe4cd59afaf808dec5

$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
  • docker images
    • イメージ一覧を表示する
    • IMAGE ID列で「aa12fc327385」を確認する
  • docker rmi
    • イメージ削除
    • イメージID「aa12fc327385」で削除

リポジトリ名で削除

docker rmi <REPOSITORY>:<TAG>
$ sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      3.5       f80194ae2e0c   3 years ago    4MB
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB

$ sudo docker rmi alpine
Untagged: alpine:latest
Untagged: alpine@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Deleted: sha256:c059bfaa849c4d8e4aecaeb3a10c2d9b3d85f5165c66ad3a4d937758128c4d18
Deleted: sha256:8d3ac3489996423f53d6087c81180006263b79f206d3fdec9e66f0e27ceb8759

$ sudo docker images|sort
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      3.5       f80194ae2e0c   3 years ago    4MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
  • docker rmi
    • イメージ削除
    • REPOSITORY名「alpine」で削除
  • alpine
    • これは省略形
    • 「alpine:latest」のこと(latestの場合は記載を省略できる)
  • 結果考察
    • REPOSITORY名がaplineのものは2つある
    • TAG列が異なっていて「3.5」と「latest」
    • 「latest」のイメージだけ削除されている

Dockerコンテナ関連

コンテナ実行

シンプルにコンテナ実行

sudo docker run <REPOSITORY>
#リポジトリ確認
$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
alpine                      3.5       f80194ae2e0c   3 years ago    4MB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB

#ローカルのリポジトリを使ってコンテナ実行
$ sudo docker run alpine

#起動確認
$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

$ sudo docker ps -a
CONTAINER ID   IMAGE         COMMAND     CREATED          STATUS                      PORTS     NAMES
fae2aa45380a   alpine        "/bin/sh"   23 seconds ago   Exited (0) 22 seconds ago             hungry_ptolemy
be19cc34d51b   hello-world   "/hello"    2 days ago       Exited (0) 2 days ago                 condescending_almeida
13e8f94aab43   hello-world   "/hello"    2 days ago       Exited (0) 2 days ago                 gallant_lumiere
e62a017fdc2d   hello-world   "/hello"    2 days ago       Exited (0) 2 days ago                 magical_euclid
  • docker run
    • コンテナ実行する
    • リポジトリ名を指定して実行する
  • docker ps
    • コンテナを確認する
    • 「-a」…… コンテナ実行履歴を確認する
    • 今回は実行が終わっているので「-a」オプションで確認する
  • 結果考察

名前を付けてコンテナ実行

sudo docker run <REPOSITORY> --name xxx
#名前を付けて実行
$ sudo docker run --name futa-test alpine

#コンテナ実行履歴確認
$ sudo docker ps -a
CONTAINER ID   IMAGE         COMMAND     CREATED         STATUS                     PORTS     NAMES
96454bed4c4a   alpine        "/bin/sh"   4 seconds ago   Exited (0) 3 seconds ago             futa-test
be19cc34d51b   hello-world   "/hello"    5 days ago      Exited (0) 5 days ago                condescending_almeida
13e8f94aab43   hello-world   "/hello"    5 days ago      Exited (0) 5 days ago                gallant_lumiere
e62a017fdc2d   hello-world   "/hello"    5 days ago      Exited (0) 5 days ago                magical_euclid

#名前を指定してコンテナ実行履歴削除
$ sudo docker rm futa-test
futa-test

#削除確認
$ sudo docker ps -a
CONTAINER ID   IMAGE         COMMAND    CREATED      STATUS                  PORTS     NAMES
be19cc34d51b   hello-world   "/hello"   5 days ago   Exited (0) 5 days ago             condescending_almeida
13e8f94aab43   hello-world   "/hello"   5 days ago   Exited (0) 5 days ago             gallant_lumiere
e62a017fdc2d   hello-world   "/hello"   5 days ago   Exited (0) 5 days ago             magical_euclid
  • docker run
    • コンテナ実行する
    • リポジトリ名を指定して実行する
  • –name(nameの左のハイフンは2つ)
    • コンテナ名を指定する
    • 「futa-test」にした
  • docker ps -a
    • コンテナ実行履歴を確認する
    • NAME列が「futa-test」になっている

コマンド履歴を残さずにコンテナ実行

sudo docker run --rm xxx
  • docker run
    • コンテナ実行する
  • –rm(rmの左のハイフンは2つ)
    • コンテナ実行時にコンテナ実行履歴を残さない
    • 実行中の場合はdocker psには出力される
  • 参考

特殊な実行

Dockerコンテナに入ってコマンド実行

sudo docker run -it alpine
$ sudo docker images
REPOSITORY                  TAG       IMAGE ID       CREATED        SIZE
alpine                      latest    c059bfaa849c   2 months ago   5.59MB
hello-world                 latest    feb5d9fea6a5   4 months ago   13.3kB
ubuntu                      12.04     5b117edd0b76   4 years ago    104MB
dockersamples/static-site   latest    f589ccde7957   5 years ago    191MB

$ sudo docker run -it alpine
/ # ls bin dev etc home lib media mnt opt proc root run sbin srv sys tmp usr var / # exit $ sudo docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7f6cde70a87b alpine "/bin/sh" 47 seconds ago Exited (0) 10 seconds ago zealous_leakey be19cc34d51b hello-world "/hello" 5 days ago Exited (0) 5 days ago condescending_almeida 13e8f94aab43 hello-world "/hello" 5 days ago Exited (0) 5 days ago gallant_lumiere e62a017fdc2d hello-world "/hello" 5 days ago Exited (0) 5 days ago magical_euclid
  • docker images
    • 利用するイメージのリポジトリ名を確認する
    • REPOSITORY列が「alpine」
  • docker run
    • イメージ「alpine」を使ってコンテナ実行
  • -it
    • コンテナ内に入る
    • プロンプトが変わりコンテナ内のOS環境でコマンド実行できる
    • exitでコンテナから出る(コンテナは終了する)
  • docker ps -a
    • コンテナ実行履歴を確認する

Dockerコンテナのバックグラウンド実行

sudo docker run -d xxx
  • ここにはコマンドのみ紹介
  • -d
    • detachの略
    • バックグラウンド実行する
    • フォアグラウンド実行は「attach」
  • 参考
    • 下記の記事にバックグラウンド実行の例

リファレンス


Posted by futa