Dockerコマンド


## Dokcerを起動する
```java:ターミナル

docker-compose up

docker-compose up --build

Ctrl + C
docker-compose up -d
```

## Dockerを停止
```java:ターミナル
docker-compose stop

docker-compose down

docker stop ${container_name}
```

## Docker内に入る
```java:ターミナル
docker-compose exec ${container_name} bash

docker container exec -it コンテナ名orコンテナID bash

exit
```


## Dockerコンテナを確認・削除する
```java:ターミナル
docker-compose ps

docker container ps

docker rm
```

## Dockerイメージを確認・削除する
```java:ターミナル
docker images

docker rmi
```

## Dockerの容量に空きがなくなった時
```java:ターミナル
:::note info
docker-compose buildでエラーが出た
:::
docker-compose build
[2/7] RUN apt-get update:
Get:1 http://security.debian.org/debian-security bullseye-security InRelease
Err:1 http://security.debian.org/debian-security bullseye-security InRelease
docker executor failed running [/bin/sh -c apt-get update]: exit code: 100

:::note info
dockerのシステムを確認
:::

docker system df
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          36        21        27.08GB   14.26GB (52%)
Containers      27        0         97.9MB    97.9MB (100%)
Local Volumes   124       12        4.927GB   3.814GB (77%)
Build Cache     649       0         12.03GB   12.03GB

docker info
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Build with BuildKit (Docker Inc., v0.6.1-docker)
  compose: Docker Compose (Docker Inc., v2.0.0-rc.1)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 27
  Running: 0
  Paused: 0
  Stopped: 27
 Images: 48
 Server Version: 20.10.8
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runtime.v1.linux runc io.containerd.runc.v2
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: e25210fe30a0a703442421b0f60afac609f950a3
 runc version: v1.0.1-0-g4144b63
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 5.10.47-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 4
 Total Memory: 1.942GiB
 Name: docker-desktop
 ID: C5ZK:Q565:DXEC:56YL:PFP3:MMCB:ELI7:3PGM:JIDS:TN4W:FRXK:DDJI
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

docker images
REPOSITORY                 TAG       IMAGE ID       CREATED         SIZE
pythondajngo_web           latest    dda742fc38be   7 weeks ago     1.93GB
<none>                     <none>    386b96a62e94   2 months ago    961MB
python_django_web          latest    33e8642a6047   2 months ago    1.09GB
<none>                     <none>    99e422b51d40   2 months ago    898MB
<none>                     <none>    6fc2e0d19319   2 months ago    898MB
<none>                     <none>    1d30cbb374ae   2 months ago    898MB

:::note info
docker image pruneでもスペースが空かない
:::

docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

:::note info
TAGが<none>を削除しようとしてもエラーになる
:::

docker images -f 'dangling=true'
REPOSITORY   TAG       IMAGE ID       CREATED        SIZE
<none>       <none>    386b96a62e94   2 months ago   961MB
<none>       <none>    99e422b51d40   2 months ago   898MB
<none>       <none>    6fc2e0d19319   2 months ago   898MB
<none>       <none>    1d30cbb374ae   2 months ago   898MB

docker images -f 'dangling=true' -q
386b96a62e94
99e422b51d40
6fc2e0d19319
1d30cbb374ae

docker rmi $(docker images -f'dangling=true' -q)
Error response from daemon: conflict: unable to delete 386b96a62e94 (must be forced) - image is being used by stopped container 5a3abc0fc891
Error response from daemon: conflict: unable to delete 99e422b51d40 (must be forced) - image is being used by stopped container 31a53bf47c22
Error response from daemon: conflict: unable to delete 6fc2e0d19319 (must be forced) - image is being used by stopped container 116e79670155
Error response from daemon: conflict: unable to delete 1d30cbb374ae (must be forced) - image is being used by stopped container eab07485ece2

docker ps -a
CONTAINER ID   IMAGE                   COMMAND                  CREATED         STATUS                      PORTS     NAMES
2eca7d642041   pythondajngo_web        "python ./PythonDjan…"   7 weeks ago     Exited (0) 7 minutes ago              pythondjango
5a3abc0fc891   386b96a62e94            "django-admin.py sta…"   2 months ago    Exited (0) 2 months ago               pythondajngo_web_run_6685b3c8d7e4
31a53bf47c22   99e422b51d40            "/bin/sh -c 'pip ins…"   2 months ago    Exited (1) 2 months ago               elated_knuth
116e79670155   6fc2e0d19319            "/bin/sh -c 'pip ins…"   2 months ago    Exited (1) 2 months ago               compassionate_lewin
eab07485ece2   1d30cbb374ae            "/bin/sh -c 'pip ins…"   2 months ago    Exited (1) 2 months ago               festive_engelbart

:::note info
不要なコンテナを削除してイメージを削除
:::

docker rmi $(docker images -f'dangling=true' -q)
Deleted: sha256:386b96a62e942f9293b534115fe1df4ed120aa1eeeeda20021bf1fcb0e81d53b
Deleted: sha256:0b9a16af303e18d9dcbaf95ac3eef4c88e0bb41def6792f67950342e6dae6dde
Deleted: sha256:5660a3b7d5aae510449e70db4468cd41046b7402e5f80fcd6731908ec6e8e763
Deleted: sha256:ab3f96b17e450d961ef8f842a0cfcdb5c433ff27827871eca1b6ac757821f4cd
Deleted: sha256:f6e4dbe46b611cfc8d77018b1333013485386bd0f97a3ea53556a17bdee3ac55
Deleted: sha256:a3c5810c6aca09fbcb8370c338d3564bf575f8f5470129e8a638bc5cabf6bb41

:::note info
システムを確認
:::

docker info                                     
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Build with BuildKit (Docker Inc., v0.6.1-docker)
  compose: Docker Compose (Docker Inc., v2.0.0-rc.1)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 24
  Running: 0
  Paused: 0
  Stopped: 24
 Images: 44
 Server Version: 20.10.8
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: e25210fe30a0a703442421b0f60afac609f950a3
 runc version: v1.0.1-0-g4144b63
 init version: de40ad0
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 5.10.47-linuxkit
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 4
 Total Memory: 1.942GiB
 Name: docker-desktop
 ID: C5ZK:Q565:DXEC:56YL:PFP3:MMCB:ELI7:3PGM:JIDS:TN4W:FRXK:DDJI
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

docker system df                                
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          34        19        25.94GB   14.26GB (54%)
Containers      24        0         89.54MB   89.54MB (100%)
Local Volumes   124       12        4.927GB   3.814GB (77%)
Build Cache     649       0         12.1GB    12.1GB

:::note info
Build Cacheを削除
:::
docker builder prune
WARNING! This will remove all dangling build cache. Are you sure you want to continue? [y/N] y
Deleted build cache objects:

Total reclaimed space: 12.1GB

docker system df    
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          34        19        25.94GB   14.26GB (54%)
Containers      24        0         89.54MB   89.54MB (100%)
Local Volumes   124       12        4.927GB   3.814GB (77%)
Build Cache     235       0         0B        0B

:::note info
build成功
:::

docker-compose build
Building web
[+] Building 99.3s (12/12) FINISHED                    
```

## インストールされたpipを確認
```java:ターミナル
docker-compose exec web pip list | grep panas
```

## 参考資料
[Dockerのコンテナ内に入る！でもなんで入れるの？](https://qiita.com/Asaiii12/items/4ac73445ff8d9b75e02e)
[dockerのコンテナの中に入る](https://qiita.com/Keitaroooo/items/a30bf4eb9310d7b3f7cd)
[Dockerコマンド(確認編)](https://qiita.com/negi524/items/bb7a2f0331b3d74d863b)
[dockerの起動、停止、再起動](https://qiita.com/tanakin_prog/items/6e6219a62e7a05eb22c2)

[docker　容量が足りず発生するエラーを解消した](https://zenn.dev/masatofromr6s/articles/998cdc7e5c4402)
[Dockerでapt-get updateができないとき確認すること](https://note.yu9824.com/error/docker-debian-gpg-error/)
[Dockerのtips的な自分のメモ](https://qiita.com/k-waragai/items/046473b56c8ecca18634)
[【Docker】<none>タグのイメージを一括削除する & Rails .gitignoreの編集](https://blog.cloud-acct.com/posts/u-docker-noneimage-deleteall/)
[Docker がシステムの容量を圧迫している時](https://qiita.com/kino-ma/items/caac47bb22d717e8f2e8)
[使用していない Docker オブジェクトの削除（prune）](https://docs.docker.jp/config/pruning.html)
[Dockerでビルドした際に作られるBuild Cacheを削除する](https://aduce.jp/docker/remove-docker-build-cache)