---
# System prepended metadata

title: '`docker`指令總覽及系統指令'
tags: [docker]

---

`docker`指令總覽及系統指令
===
(last update:2021/10/31)

## `docker`指令分類一覽

* 系統相關：`docker system` **列出docker相關資訊**
* 映像檔相關：`docker image`  **管理映像檔**
* 容器相關：`docker container` **container通常省略**
* 網路相關：`docker network` **管理網路**
* 儲存相關：`docker volume` **管理docker儲存設備**
* ~~服務相關：`docker service`~~ **管理系統服務，有了k8s就沒落了**
* ~~堆疊相關：`docker stack`~~ **管理多個容器堆疊，有了k8s就沒落了**
* ~~多主機相關：`docker swarm`~~ **管理多台主機的docker叢集，有了k8s就沒落了**

### 系統圖解
![](https://i.imgur.com/5xciVnC.png)

### 所有指令一覽
* `docker attach` **連接到一個執行中容器的輸出(顯示容器)**
* `docker build` **利用自建的`Dockerfile`建立映像檔**
* `docker commit` **從目前執行的container建立一個映像檔**
* `docker cp` **容器間或實體機容器間複製檔案**
* `docker exec` **連接至容器內執行，如`docker exec -it name bash`**
* `docker export` **把容器的檔案系統存成tar壓縮檔**
* <span style="color:red"> `docker images` **列出所有映像檔** </span>
* `docker import` **從tar檔案建立映像檔**
* `docker info` **列出目前docker的系統資訊**
* `docker inspect` **列出docker的`json`系統完整資訊** </span>
* `docker kill` **刪除執行中的容器**
* `docker load` **從tar中載入映像檔**
* `docker login` **登入docker的映像檔hub**
* `docker logout` **登出docker的映像檔hub**
* `docker logs` **查看容器的輸出資訊**
* `docker pause` **暫停容器的執行**
* `docker port` **列出容器對外開放的通訊埠**
* <span style="color:red">`docker ps` **列出執行的容器`docker ps -a`**</span>
* <span style="color:red">`docker pull` **下載映像檔**</span>
* <span style="color:red">`docker push` **上傳映像檔**</span>
* `docker rename` **更名映像檔**
* `docker restart` **重啟映像檔**
* <span style="color:red">`docker rm` **刪除容器**</span>
* `docker rmi` **刪除映像檔**
* <span style="color:red">`docker run` **啟動一個新的容器執行**</span>
* `docker save` **將容器存入tar中**
* <span style="color:red">`docker search` **搜索docker hub**</span>
* `docker start` **啟動停止的容器**
* `docker stats` **列出容器的執行狀態**
* `docker stop` **停止容器**
* `docker tag` **幫映像檔取標籤名稱**
* `docker top` **列出執行中容器目前的處理程序**
* `docker unpause` **讓暫停中的容器繼續執行**

## 系統相關指令範例
**範例1：`docker version`**
列出docker版本，注意不同版本會有不同功能。有些功能在新版本均移除，請注意 https://docs.docker.com/engine/deprecated/。
```
(joshhuAI)joshhu:~/ $ docker version                                                                            [0:06:34]
Client: Docker Engine - Community
 Version:           20.10.7
 API version:       1.41
 Go version:        go1.13.15
 Git commit:        f0df350
 Built:             Wed Jun  2 11:56:47 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.7
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       b0f5bc3
  Built:            Wed Jun  2 11:54:58 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.6
  GitCommit:        d71fcd7d8303cbf684402823e425e9dd2e99285d
 nvidia:
  Version:          1.0.0-rc95
  GitCommit:        b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
(joshhuAI)joshhu:~/ $      
```
**範例2：`docker info`**
顯示系統資訊。
```
(joshhuAI)joshhu:~/ $ docker info                                                                               [0:06:43]
Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  scan: Docker Scan (Docker Inc., v0.8.0)

Server:
 Containers: 1
  Running: 1
  Paused: 0
  Stopped: 0
 Images: 7
 Server Version: 20.10.7
 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 nvidia runc
 Default Runtime: nvidia
 Init Binary: docker-init
 containerd version: d71fcd7d8303cbf684402823e425e9dd2e99285d
 runc version: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
 Kernel Version: 4.15.0-142-generic
 Operating System: Ubuntu 16.04.7 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 24
 Total Memory: 62.83GiB
 Name: joshhuai
 ID: YSFJ:UUP7:B7EV:OZLF:IX7Q:SVIP:4SM6:OVMR:SC7D:FVYV:D2N2:SAXM
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Username: joshhu
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

WARNING: No swap limit support
```
**範例3：`docker system prune`**
清除系統所有不要的東西。
```
(joshhuAI)joshhu:~/ $ docker system prune                                                                       [0:09:24]
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
(joshhuAI)joshhu:~/ $  
```
**範例4：`docker system events`**
檢視主機目前執行docker有關的訊息。
```
docker run -d newimage
```
```
(joshhuAI)joshhu:~/ $ docker system events                                                                      [0:12:03]
2021-10-31T00:12:56.299461853+08:00 container create 3d0571ca7fad49b69e48debc97880478acc5d4b74792cb78e3b07c29ee1b88fc (image=newimage, name=intelligent_snyder)
2021-10-31T00:12:56.320222453+08:00 network connect 5b4c28676aae128b3b825b86714510df45a86cd62e35bc9ec2b1ef3cbca66950 (container=3d0571ca7fad49b69e48debc97880478acc5d4b74792cb78e3b07c29ee1b88fc, name=bridge, type=bridge)
2021-10-31T00:12:56.881895643+08:00 container start 3d0571ca7fad49b69e48debc97880478acc5d4b74792cb78e3b07c29ee1b88fc (image=newimage, name=intelligent_snyder)
```
```
docker rm -f 3
```
```
(joshhuAI)joshhu:~/ $ docker system events                                                                      [0:11:22]
2021-10-31T00:11:45.735274280+08:00 container kill 781d1370a70ee9b2cd0b54ae0ae1a90acea648897e05307ad9232f4730bca6ca (image=newimage, name=romantic_volhard, signal=9)
2021-10-31T00:11:45.799964394+08:00 container die 781d1370a70ee9b2cd0b54ae0ae1a90acea648897e05307ad9232f4730bca6ca (exitCode=137, image=newimage, name=romantic_volhard)
2021-10-31T00:11:45.944675253+08:00 network disconnect 5b4c28676aae128b3b825b86714510df45a86cd62e35bc9ec2b1ef3cbca66950 (container=781d1370a70ee9b2cd0b54ae0ae1a90acea648897e05307ad9232f4730bca6ca, name=bridge, type=bridge)
2021-10-31T00:11:46.005736426+08:00 container destroy 781d1370a70ee9b2cd0b54ae0ae1a90acea648897e05307ad9232f4730bca6ca (image=newimage, name=romantic_volhard)
```
**範例5：`docker system df`**
列出目前磁碟使用狀況，主要為映像檔。
```
(joshhuAI)joshhu:~/ $ docker system df                                                                                              [0:14:15]
TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          4         0         544.4MB   544.4MB (100%)
Containers      0         0         0B        0B
Local Volumes   4         0         7.184GB   7.184GB (100%)
Build Cache     0         0         0B        0B
(joshhuAI)joshhu:~/ $  
```
###### tags: `docker`