管理人のSE経験値(Exp)を見える化するブログ

中小企業(独立系Sier)でインフラエンジニアをしている管理人が日々の業務、自己啓発によって得た知識、経験を雑記するブログです。

Docker:Docker管理コマンド

目的

Dockerの管理に利用するコマンドについて個人的な備忘の為に記事を書く。

Docker管理コマンド

#Dockerバージョン確認
[root@localhost ~]# docker -v
Docker version 1.12.3, build 6b644ec
#Docker状態の確認
[root@localhost ~]# docker info
Containers: 8
 Running: 0
 Paused: 0
 Stopped: 8
Images: 6
Server Version: 1.12.3
Storage Driver: devicemapper
 Pool Name: docker-253:3-1074561216-pool
 Pool Blocksize: 65.54 kB
 Base Device Size: 10.74 GB
 Backing Filesystem: xfs
 Data file: /dev/loop0
 Metadata file: /dev/loop1
 Data Space Used: 1.137 GB
 Data Space Total: 107.4 GB
 Data Space Available: 106.2 GB
 Metadata Space Used: 2.134 MB
 Metadata Space Total: 2.147 GB
 Metadata Space Available: 2.145 GB
 Thin Pool Minimum Free Space: 10.74 GB
 Udev Sync Supported: true
 Deferred Removal Enabled: false
 Deferred Deletion Enabled: false
 Deferred Deleted Device Count: 0
 Data loop file: /var/lib/docker/devicemapper/devicemapper/data
 WARNING: Usage of loopback devices is strongly discouraged for production use. Use `--storage-opt dm.thinpooldev` to specify a custom block storage device.
 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2016-06-09)
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
 Volume: local
 Network: bridge host null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Security Options: seccomp
Kernel Version: 3.10.0-327.36.3.el7.x86_64
Operating System: CentOS Linux 7    (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 3.695 GiB
Name: localhost.localdomain
ID: FTSO:BZS4:7A7J:RGKW:YAMP:I22I:M4UJ:535T:3IDC:FQU4:G4OY:6ZQC
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Insecure Registries:
 127.0.0.0/8
#Dockerクライアント、サーバー、APIバージョンの確認
[root@localhost ~]# docker version
Client:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:
 OS/Arch:      linux/amd64

Server:
 Version:      1.12.3
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   6b644ec
 Built:
 OS/Arch:      linux/amd64
#Dockerイメージ入手
[root@localhost ~]# docker pull centos:centos7.1.1503
centos7.1.1503: Pulling from library/centos
77fe1992330f: Already exists
Digest: sha256:cb462399353506bfdfa311ed08274e9d3f595ada38ace7309aec2bc86b2885b5
Status: Downloaded newer image for centos:centos7.1.1503
#Dockerイメージの確認
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              centos7.1.1503      285396d0a019        12 weeks ago        212.1 MB
#Dockerコンテナの起動
#--name:コンテナ名設定、-h:ホスト名設定-i:標準入力(STDIN)を受付ける、-t:仮想端末(pseudo-TTY)をコンテナに割り当てる
[root@localhost ~]# docker run --name test01 -h test01 -i -t centos:centos7.1.1503 /bin/bash
[root@test01 /]#
#Dockerコンテナ一覧確認
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS                        PORTS               NAMES
e8ffc3e52332        centos:centos7.1.1503   "/bin/bash"         25 minutes ago      Exited (127) 22 minutes ago                       test01
#Dockerコンテナのコミット
[root@localhost ~]# docker commit e8ffc3e52332 centos:testcon01
sha256:46c2fc5590d011d562ddad66bb0d652ac8af371ffda0064c6ba85406308e3fe7


#Dockerイメージの確認
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
centos              testcon01           46c2fc5590d0        About a minute ago   212.1 MB
centos              centos7.1.1503      285396d0a019        12 weeks ago         212.1 MB
#Dockerコンテナのバックグラウンド起動
#--name:コンテナ名設定、-h:ホスト名設定-i:標準入力(STDIN)を受付ける、-t:仮想端末(pseudo-TTY)をコンテナに割り当てる
[root@localhost ~]# docker run -d --name test02 -h test02 -i -t centos:centos7.1.1503 /bin/bash
13675cc930cd730767304cf082454832f39dd03e3d229d2f4db511e22f5e126d


#Dockerコンテナ一覧確認
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED              STATUS                      PORTS               NAMES
13675cc930cd        centos:centos7.1.1503   "/bin/bash"         About a minute ago   Up About a minute                               test02
ab80b16565e6        centos:centos7.1.1503   "/bin/bash"         34 minutes ago       Exited (0) 34 minutes ago                       test01
#バックグラウンド稼働中のDockerコンテナへの接続
[root@localhost ~]# docker attach 13675cc930cd
[root@test02 /]#
#Dockerコンテナを停止せずに抜ける
#[Ctrl]+[P] → [Ctrl]+[Q]のキーボードを入力
[root@test02 /]#
[root@localhost ~]#


#Dockerコンテナ一覧確認
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS                      PORTS               NAMES
13675cc930cd        centos:centos7.1.1503   "/bin/bash"         21 minutes ago      Up 21 minutes                                   test02
ab80b16565e6        centos:centos7.1.1503   "/bin/bash"         55 minutes ago      Exited (0) 54 minutes ago                       test01
#Dockerコンテナの終了
[root@test01 /]# exit
exit
#停止しているDockerコンテナの起動
[root@localhost ~]# docker start ab80b16565e6
ab80b16565e6
#起動しているDockerコンテナの停止
[root@localhost ~]# docker stop ab80b16565e6
ab80b16565e6
#Dockerコンテナの削除
[root@localhost ~]# docker rm ab80b16565e6
ab80b16565e6
#稼働中のDockerコンテナの強制削除
[root@localhost ~]# docker rm -f 13675cc930cd
13675cc930cd
#Dockerイメージの削除
[root@localhost ~]# docker rmi centos:testcon01
Untagged: centos:testcon01
Deleted: sha256:46c2fc5590d011d562ddad66bb0d652ac8af371ffda0064c6ba85406308e3fe7
Deleted: sha256:7f409f5fd222fa5b0e0beb2c2754f92d54137d37af02da804d48497ee8b5eb09
#Dockerイメージの強制削除
[root@localhost ~]# docker rmi -f centos:cent7.1.1503
Untagged: centos:cent7.1.1503
Deleted: sha256:b81c5494b34e8f608225e8d91e22ccfd48bb46d43a3668543e5244fbc05f2eb3
Deleted: sha256:786ac458fb47ae6a80e5602fa67920fc67bf98df79713222a018abd67cff5c32

利用環境

CentOS 7.2.1511
Docker 1.12.3