說明:把Docker常用命令歸納到linux基礎,是因為我本地docker安裝在centos上,平時運行實驗鏡像的時候都是直接在centos上操作運行。
1、docker版本、狀態查看
[root@k8smaster test]# docker -v
Docker version 20.10.6, build 370c289
[root@k8smaster test]# docker version
Client: Docker Engine - Community
Version: 20.10.6
API version: 1.41
Go version: go1.13.15
Git commit: 370c289
Built: Fri Apr 9 22:45:33 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
......
......
2、鏡像(image)操作
從倉庫拉取鏡像:
// Docker的鏡像存儲在鏡像注冊中心(image registry)中,默認是Docker Hub(https://hub.docker.com/),在拉取鏡像的時候也可以指定其它注冊中心。
// 拉取鏡像,使用docker image pull命令
[root@k8smaster test]# docker image pull --help
Usage: docker image pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--platform string Set platform if server is
multi-platform capable
-q, --quiet Suppress verbose output
查詢已有鏡像:
[root@k8smaster test]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 2 months ago 13.3kB
刪除鏡像:
[root@k8smaster test]# docker rmi --help
Usage: docker rmi [OPTIONS] IMAGE [IMAGE...]
Remove one or more images
Options:
-f, --force Force removal of the image
--no-prune Do not delete untagged parents
[root@k8smaster test]# docker rmi -f hello-world
Untagged: hello-world:latest
Untagged: hello-world@sha256:5122f6204b6a3596e048758cabba3c46b1c937a46b5be6225b835d091b90e46c
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
[root@k8smaster test]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
刪除所有的鏡像:
[root@k8smaster test]# docker rmi $(docker images -q)
3、容器(container)操作
運行容器:
// -d 后臺運行 -it交互模式 -p端口映射 --name命名
[root@k8smaster io-cached]# docker run --privileged --name=App -itd feisky/app:io-cached
742cd266966a35af9088b842c4f607e767a8578b7feb2d29ccf0154986e56f05
容器運行日志查看(用于驗證容器是否已經運行):
[root@k8smaster io-cached]# docker logs app
Reading data from disk /dev/sda2 with buffer size 33554432
Time used: 0.410266 s to read 33554432 bytes
Time used: 0.011844 s to read 33554432 bytes
Time used: 0.011292 s to read 33554432 bytes
Time used: 0.011079 s to read 33554432 bytes
Time used: 0.011068 s to read 33554432 bytes
Time used: 0.010529 s to read 33554432 bytes
Time used: 0.011277 s to read 33554432 bytes
查看運行中的容器:
[root@k8smaster io-cached]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5017ff0bc9a6 feisky/app:io-direct "/app" 5 minutes ago Up 5 minutes app
暫停、重啟、刪除容器:
[root@k8smaster io-cached]# docker stop app
app
[root@k8smaster io-cached]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@k8smaster io-cached]# docker start app
app
[root@k8smaster io-cached]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
742cd266966a feisky/app:io-cached "/app" 3 minutes ago Up 4 seconds app
[root@k8smaster io-cached]# docker rm -f app
app
[root@k8smaster io-cached]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
停止或刪除所有容器:
[root@k8smaster io-cached]# docker stop $(docker ps -qa)
[root@k8smaster io-cached]# docker rm -f $(docker ps -aq)
[root@k8smaster io-cached]# docker rm -f $(docker stop $(docker ps -q))
4、進入交互模式
[root@k8smaster io-cached]# docker exec -it app /bin/bash
[root@k8smaster io-cached]# docker exec -it app /bin/sh