帮助命令

docker version		 # 显示docker的版本
docker info # 显示docker的系统信息,也包括镜像和容器的数量
docker [命令] --help # 帮助命令

帮助文档地址:https://docs.docker.com/engine/reference/commandline

镜像命令

docker images

docker images | Docker Documentation

查看所有本地的主机上的镜像

[root@swcode ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest feb5d9fea6a5 16 months ago 13.3kB
标签 解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的id
CREATED 镜像的创建时间
SIZE 镜像的大小

可选参数

-a, --all         # 列出所有的镜像
-q, --quiet # 只显示镜像的id

docker search | Docker Documentation

搜索镜像

Docker Hub的地址:https://hub.docker.com/

[root@swcode ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used… 13737 [OK]
mariadb MariaDB Server is a … 5242 [OK]

可选参数

--filter=STARS=3000		# 搜索STARS大于3000的

docker pull

docker pull | Docker Documentation

下载镜像

docker pull mysql[:tag]
[root@swcode ~]# docker pull mysql
Using default tag: latest # 不写 tag,默认就是 latest
latest: Pulling from library/mysql
39fbafb6c7ef: Pull complete # 分层下载,docker image的核心 联合文件系统
76e9f8ca4fd7: Pull complete
1fcdd923e548: Pull complete
15716fe15a39: Pull complete
95c8bb47c8b9: Pull complete
b050c46cac02: Pull complete
7e19c22a3773: Pull complete
a2abd6d528f1: Pull complete
6aca593545a5: Pull complete
8a262a30fc7f: Pull complete
a1170ff1b1e9: Pull complete
Digest: sha256:19b05df6eb4b7ed6f274c0552f053ff0c00842a40dcf05941225c429a716683d #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest # 真实地址

真实地址

docker pull mysql
docker pull docker.io/library/mysql:latest

指定版本下载

docker pull mysql:5.7

docker rmi

docker rmi | Docker Documentation

删除一个镜像

[root@swcode ~]# docker rmi -f 镜像id

删除多个镜像

[root@swcode ~]# docker rmi -f 镜像id 镜像id 镜像id

删除全部镜像

[root@swcode ~]# docker rmi -f $(docker images -aq)

容器命令

注意:有了镜像才可以创建容器

下载一个CentOS镜像测试学习

docker pull centos

docker run

docker run | Docker Documentation

新建容器并启动

docker run [可选参数] image
参数 说明
–name=”Name” 容器名字,区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器端口 -p 8080:8080
-P 随机指定端口

启动并进入容器

[root@swcode ~]# docker run -it centos /bin/bash
[root@4196286a2ebf /]#

退出容器

[root@4196286a2ebf /]# exit

docker ps

docker ps | Docker Documentation

列出容器

[root@swcode ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
参数 描述
-a 列出当前正在运行的容器+历史运行过
-n=? 列出最近创建的n个容器
-q 只显示容器ID

退出容器

先进入容器

[root@swcode ~]# docker run -it centos /bin/bash
[root@4196286a2ebf /]#

使用下面方式退出

exit	# 停止并退出容器
Ctrl + P + Q # 不停止退出

删除容器

docker rm | Docker Documentation

删除容器

docker rm 容器id	# 无法删除正在运行的容器

强制删除所有容器

docker rm -f $(docker ps -aq)
docker ps -a -q | xargs docker rm -f

启动和停止容器

docker start | Docker Documentation

docker restart | Docker Documentation

docker stop | Docker Documentation

docker kill | Docker Documentation

启动已经停止的容器

docker start 容器id		# 启动容器
docker restart 容器id # 重启容器
docker stop 容器id # 停止运行中的容器
docker kill 容器id # 强制停止

常用其他命令

后台启动容器

[root@swcode ~]# docker run -d centos
9dc577939aee390cda66c44d32656b6e369ad3685ce43083094b5da0a88dbf2a
[root@swcode ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
9dc577939aee centos "/bin/bash" 7 seconds ago Exited (0) 6 seconds ago

使用docker ps查看,发现centos已经停止。

docker容器使用后台运行,就必须要有一个前台进程,docker发现没有相应前台进程,就会自动停止

查看日志

docker logs | Docker Documentation

docker logs -f -t --tail n 打印最后n行日志
参数 描述
-f 一直监听打印
-t 显示时间戳
–tail n 要显示的日志条数

查看容器中进程信息

docker top | Docker Documentation

[root@swcode ~]# docker top 3e275c8c6acd
UID PID PPID C STIME TTY TIME CMD
root 2718 2698 0 16:48 pts/0 00:00:00 /bin/bash

查看镜像的元数据

docker inspect | Docker Documentation

docker inspect 3e275c8c6acd
点击查看元数据
[
{
"Id": "3e275c8c6acdb662a9641ed98a6dd57911e39da2933608a327905be2f99968f9",
"Created": "2023-01-29T08:48:35.874003235Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 2718,
"ExitCode": 0,
"Error": "",
"StartedAt": "2023-01-29T08:48:36.190774581Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/3e275c8c6acdb662a9641ed98a6dd57911e39da2933608a327905be2f99968f9/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/3e275c8c6acdb662a9641ed98a6dd57911e39da2933608a327905be2f99968f9/hostname",
"HostsPath": "/var/lib/docker/containers/3e275c8c6acdb662a9641ed98a6dd57911e39da2933608a327905be2f99968f9/hosts",
"LogPath": "/var/lib/docker/containers/3e275c8c6acdb662a9641ed98a6dd57911e39da2933608a327905be2f99968f9/3e275c8c6acdb662a9641ed98a6dd57911e39da2933608a327905be2f99968f9-json.log",
"Name": "/competent_johnson",
"RestartCount": 0,
"Driver": "overlay2",
"Platform": "linux",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": {
"Binds": null,
"ContainerIDFile": "",
"LogConfig": {
"Type": "json-file",
"Config": {}
},
"NetworkMode": "default",
"PortBindings": {},
"RestartPolicy": {
"Name": "no",
"MaximumRetryCount": 0
},
"AutoRemove": false,
"VolumeDriver": "",
"VolumesFrom": null,
"CapAdd": null,
"CapDrop": null,
"CgroupnsMode": "host",
"Dns": [],
"DnsOptions": [],
"DnsSearch": [],
"ExtraHosts": null,
"GroupAdd": null,
"IpcMode": "private",
"Cgroup": "",
"Links": null,
"OomScoreAdj": 0,
"PidMode": "",
"Privileged": false,
"PublishAllPorts": false,
"ReadonlyRootfs": false,
"SecurityOpt": null,
"UTSMode": "",
"UsernsMode": "",
"ShmSize": 67108864,
"Runtime": "runc",
"ConsoleSize": [
0,
0
],
"Isolation": "",
"CpuShares": 0,
"Memory": 0,
"NanoCpus": 0,
"CgroupParent": "",
"BlkioWeight": 0,
"BlkioWeightDevice": [],
"BlkioDeviceReadBps": null,
"BlkioDeviceWriteBps": null,
"BlkioDeviceReadIOps": null,
"BlkioDeviceWriteIOps": null,
"CpuPeriod": 0,
"CpuQuota": 0,
"CpuRealtimePeriod": 0,
"CpuRealtimeRuntime": 0,
"CpusetCpus": "",
"CpusetMems": "",
"Devices": [],
"DeviceCgroupRules": null,
"DeviceRequests": null,
"KernelMemory": 0,
"KernelMemoryTCP": 0,
"MemoryReservation": 0,
"MemorySwap": 0,
"MemorySwappiness": null,
"OomKillDisable": false,
"PidsLimit": null,
"Ulimits": null,
"CpuCount": 0,
"CpuPercent": 0,
"IOMaximumIOps": 0,
"IOMaximumBandwidth": 0,
"MaskedPaths": [
"/proc/asound",
"/proc/acpi",
"/proc/kcore",
"/proc/keys",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"ReadonlyPaths": [
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
},
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/62d28c2661f3e2b0752edf8bd773c3257b99673815519b5eb9bbf084c5a34fb2-init/diff:/var/lib/docker/overlay2/5cc00ea5ad884604b11721369c550c113ea31ef73b1f919dc335777da7bad8c1/diff",
"MergedDir": "/var/lib/docker/overlay2/62d28c2661f3e2b0752edf8bd773c3257b99673815519b5eb9bbf084c5a34fb2/merged",
"UpperDir": "/var/lib/docker/overlay2/62d28c2661f3e2b0752edf8bd773c3257b99673815519b5eb9bbf084c5a34fb2/diff",
"WorkDir": "/var/lib/docker/overlay2/62d28c2661f3e2b0752edf8bd773c3257b99673815519b5eb9bbf084c5a34fb2/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "3e275c8c6acd",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash"
],
"Image": "centos",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"org.label-schema.build-date": "20210915",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "2896da3059dd45610c6f1944336b18ee71d836d22b83487ab4febdd5a217a789",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/2896da3059dd",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "7216c18e3946b0ee4b69eab6752b189289318ce460310d0324d03e97c620b43f",
"Gateway": "172.17.0.1",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"MacAddress": "02:42:ac:11:00:02",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "dc21a4da01fb418f9dc845b545b0355f01ea0e7fb81ade2d4d2ac3dd4ec2e842",
"EndpointID": "7216c18e3946b0ee4b69eab6752b189289318ce460310d0324d03e97c620b43f",
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02",
"DriverOpts": null
}
}
}
}
]

进入当前正在运行的容器

方式一

docker exec | Docker Documentation

docker exec -it 容器id bashShell

方式二

docker attach | Docker Documentation

docker attach 容器id bashShell
  • docker exec:进入容器后开启一个新的终端
  • docker attach:进入容器当前正在执行的终端

拷贝文件到主机

docker cp | Docker Documentation

docker cp 容器id:容器路径 主机路径

案例

[root@swcode ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e275c8c6acd centos "/bin/bash" 23 minutes ago Up 23 minutes competent_johnson
# 进入docker容器
[root@swcode ~]# docker attach 3e275c8c6acd
# 在容器内创建文件
[root@3e275c8c6acd home]# touch test.java
[root@3e275c8c6acd home]# exit
exit
# 将文件拷贝到主机上
[root@swcode ~]# docker cp 3e275c8c6acd:/home/test.java /home
[root@swcode ~]# ll /home/
total 0
-rw-r--r-- 1 root root 0 Jan 29 17:12 test.java

拷贝是一个手动过程,未来我们使用 -v 卷的技术,可以实现自动同步

总结