Dockerコンテナの作成〜起動〜利用〜停止〜削除

:::note info
コンテナの一覧表示(lsもpsも同じ結果が得られる)
*$ docker container ls -a*
*$ docker container ps -a*
:::
```
$ sudo docker container ls     ＃稼働中コンテナを表示
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

$ sudo docker container ls -a  #すべてのコンテナを表示
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
1917c7f76a5f   centos    "/bin/bash"   6 minutes ago    Exited (0) 5 minutes ago              test2

$ sudo docker container ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED        STATUS                    PORTS     NAMES
1917c7f76a5f   centos    "/bin/bash"   10 hours ago   Exited (0) 10 hours ago             test2
```

## 1.DockerイメージからDockerコンテナの新規作成とコンテナの起動その１
### 1.1 新規作成（起動名NAMES自動付与）
*$ docker container create*
```
$ sudo docker container create centos
99671ed077ff23ab05ba73e39a6659dd7fd83af65b0284d0156e0d81a7acf0f0

$ sudo docker container ls -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS    PORTS     NAMES
99671ed077ff   centos    "/bin/bash"   30 seconds ago   Created             sad_wescoff
```
### 1.2 コンテナの起動（バックグラウンド）
*$ docker container start*
```
$ sudo docker container start 99671ed077ff
99671ed077ff

$ sudo docker container ls -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                      PORTS     NAMES
99671ed077ff   centos    "/bin/bash"   4 minutes ago   Exited (0) 17 seconds ago             sad_wescoff
```

## 2.Dockerイメージからコンテナの新規作成とコンテナの起動その２
*$ docker container run*

### 2.1 Dockerコンテナの起動とコマンドの実行結果表示
```
$ sudo docker container run -it --name "test1" centos /bin/cal
      July 2022     
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31                  
$ sudo docker container ls -a
CONTAINER ID   IMAGE     COMMAND       CREATED          STATUS                      PORTS     NAMES
35cb1215e744   centos    "/bin/cal"    29 seconds ago   Exited (0) 29 seconds ago             test1
```
### 2.2 対話的実行（/bin/bash）
```
$ sudo docker container run -it --name "test2" centos /bin/bash

[root@1917c7f76a5f /]#
[root@1917c7f76a5f /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr
[root@1917c7f76a5f /]# date
Sat Jul 16 12:59:37 UTC 2022

　→途中で抜けたい場合には、Ctrl+Pの後にCtrl+Qを入力する

[root@1917c7f76a5f /]# exit
exit

$ sudo docker container ls -a
CONTAINER ID   IMAGE     COMMAND       CREATED              STATUS                      PORTS     NAMES
1917c7f76a5f   centos    "/bin/bash"   About a minute ago   Exited (0) 22 seconds ago             test2
```

### 2.3 コンテナのバックグラウンドコマンド実行
```
$ sudo docker container run -d centos /bin/ping localhost
1118172ceadde54ff8e2dea8bed3cb4067e02ecc4f3d849cec705d8e4bd59cc8

$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND                 CREATED         STATUS         PORTS     NAMES
1118172ceadd   centos    "/bin/ping localhost"   9 seconds ago   Up 8 seconds             hopeful_thompson

$ sudo docker container logs -t 1118172ceadd
2022-07-16T13:29:38.741802937Z PING localhost (127.0.0.1) 56(84) bytes of data.
2022-07-16T13:29:38.741826496Z 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.019 ms
2022-07-16T13:29:39.773095817Z 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.048 ms
2022-07-16T13:29:40.797139241Z 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.048 ms
```

## 3.既存コンテナへの接続 
*docker container start -i XXX　　#コンテナ環境（コンテナの標準入力）へ接続*
*docker container start -a XXX　　#コンテナ環境（コンテナの標準出力）へ接続*
```
#コンテナ環境（コンテナの標準入力）へ接続する場合には -i を入力
$ sudo docker container start -i 1917c7f76a5f
[root@1917c7f76a5f /]# exit
exit

#コンテナ環境（コンテナの標準出力）へ接続する場合には -a を入力
$ sudo docker container ps
CONTAINER ID   IMAGE     COMMAND                 CREATED          STATUS          PORTS     NAMES
11bbc82b2fca   centos    "/bin/ping localhost"   15 seconds ago   Up 14 seconds             sleepy_beaver

$ sudo docker container start -a 11bbc82b2fca
64 bytes from localhost (127.0.0.1): icmp_seq=48 ttl=64 time=0.046 ms
64 bytes from localhost (127.0.0.1): icmp_seq=49 ttl=64 time=0.432 ms
64 bytes from localhost (127.0.0.1): icmp_seq=50 ttl=64 time=0.047 ms
64 bytes from localhost (127.0.0.1): icmp_seq=51 ttl=64 time=0.047 ms
64 bytes from localhost (127.0.0.1): icmp_seq=52 ttl=64 time=0.053 ms
64 bytes from localhost (127.0.0.1): icmp_seq=53 ttl=64 time=0.048 ms
^C
--- localhost ping statistics ---
53 packets transmitted, 53 received, 0% packet loss, time 53231ms
rtt min/avg/max/mdev = 0.019/0.053/0.432/0.053 ms
```
### 3.1 稼働中のコンテナで別のプロセス（コマンド）を実行する
*$ docker container exec XXX command*
```
$ sudo docker container start 11bbc82b2fca
11bbc82b2fca

$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND                 CREATED        STATUS         PORTS     NAMES
11bbc82b2fca   centos    "/bin/ping localhost"   11 hours ago   Up 2 seconds             sleepy_beaver

$ sudo docker container exec -it 11bbc82b2fca /bin/bash
[root@11bbc82b2fca /]# 
[root@11bbc82b2fca /]# exit
exit

$ sudo docker container exec -it 11bbc82b2fca /bin/echo "Hello"
Hello
```

### 3.2 コンテナ接続時に実行中のコマンドを止めた場合
```
$ sudo docker container start -a 11bbc82b2fca
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.044 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.052 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.051 ms
^C
--- localhost ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3055ms
rtt min/avg/max/mdev = 0.018/0.041/0.052/0.014 ms

$ sudo docker container ps    ← ctrl+C で標準出力を止めるとプロセスも止まる
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

#再開させる場合にはstartコマンドを実行すれば良い
$ sudo docker container start 11bbc82b2fca
11bbc82b2fca

$ sudo docker container ps
CONTAINER ID   IMAGE     COMMAND                 CREATED         STATUS         PORTS     NAMES
11bbc82b2fca   centos    "/bin/ping localhost"   6 minutes ago   Up 3 seconds             sleepy_beaver
```

## 4.コンテナの中断／再開
*$ docker container pause XXX*
*$ docker container unpause XXX*
```
#起動中のコンテナの中断
$ sudo docker container pause 11bbc82b2fca
11bbc82b2fca

$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND                 CREATED       STATUS                   PORTS     NAMES
11bbc82b2fca   centos    "/bin/ping localhost"   3 hours ago   Up 21 minutes (Paused)             sleepy_beaver

#停止中のコンテナの再開
$ sudo docker container unpause 11bbc82b2fca
11bbc82b2fca

$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND                 CREATED       STATUS          PORTS     NAMES
11bbc82b2fca   centos    "/bin/ping localhost"   3 hours ago   Up 22 minutes             sleepy_beaver
```

## 5.コンテナの停止
*$ docker container stop*
```
$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND                 CREATED         STATUS         PORTS     NAMES
1118172ceadd   centos    "/bin/ping localhost"   2 minutes ago   Up 2 minutes             hopeful_thompson

$ sudo docker container stop 1118172ceadd
1118172ceadd

$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
```

## 6 コンテナの稼働確認

### 6.1 コンテナ実行リソース確認
*$ docker container stats XXX*
```
$ sudo docker container stats 11bbc82b2fca
CONTAINER ID   NAME            CPU %     MEM USAGE / LIMIT     MEM %     NET I/O       BLOCK I/O     PIDS
11bbc82b2fca   sleepy_beaver   0.00%     1.172MiB / 15.55GiB   0.01%     20.4kB / 0B   6.02MB / 0B   1
^C　← ctrl＋Cで終了
```

### 6.2 コンテナ実行プロセス確認
*docker container top XXX*
```
$ sudo docker container top 11bbc82b2fca
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                3710                3690                0                   10:44               ?                   00:00:00            /bin/ping localhost
```

### 6.3 コンテナ実行ログ確認
*$ docker container logs*
```
$ sudo docker container logs -t 35cb1215e744
2022-07-16T12:51:04.670600911Z       July 2022     
2022-07-16T12:51:04.670629823Z Su Mo Tu We Th Fr Sa
2022-07-16T12:51:04.670635057Z                 1  2
2022-07-16T12:51:04.670638343Z  3  4  5  6  7  8  9
2022-07-16T12:51:04.670641539Z 10 11 12 13 14 15 16
2022-07-16T12:51:04.670645276Z 17 18 19 20 21 22 23
2022-07-16T12:51:04.670648828Z 24 25 26 27 28 29 30
2022-07-16T12:51:04.670652259Z 31     

$ sudo docker container logs -t 1118172ceadd | head -10
2022-07-16T13:29:38.741802937Z PING localhost (127.0.0.1) 56(84) bytes of data.
2022-07-16T13:29:38.741826496Z 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.019 ms
2022-07-16T13:29:39.773095817Z 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.048 ms
2022-07-16T13:29:40.797139241Z 64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.048 ms
```

## 7.コンテナの再起動
*$ docker container restart*
```
$ sudo docker container restart 1118172ceadd
1118172ceadd
 
$ sudo docker container ls
CONTAINER ID   IMAGE     COMMAND                 CREATED       STATUS          PORTS     NAMES
1118172ceadd   centos    "/bin/ping localhost"   9 hours ago   Up 10 seconds             hopeful_thompson

$ sudo docker container ps
CONTAINER ID   IMAGE     COMMAND                 CREATED       STATUS          PORTS     NAMES
1118172ceadd   centos    "/bin/ping localhost"   9 hours ago   Up 14 seconds             hopeful_thompson
```

## 8.コンテナの削除
*$ docker container rm*
*$ docker container prune  　#停止中のコンテナをすべて削除*
```
$ sudo docker container stop 1118172ceadd
1118172ceadd

$ sudo docker container rm 1118172ceadd
1118172ceadd
 
$ sudo docker container logs -t 1118172ceadd | head -10
Error: No such container: 1118172ceadd　← コンテナを削除するとログも消えるので必要な場合は退避しておく
```