说明:把Docker常用命令归纳到linux基础,是因为我本地docker安装在centos上,平时运行实验镜像的时候都是直接在centos上操作运行。
[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
......
......
从仓库拉取镜像:
// 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)
运行容器:
// -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))
[root@k8smaster io-cached]# docker exec -it app /bin/bash
[root@k8smaster io-cached]# docker exec -it app /bin/sh