帮助命令
docker version #显示docker的版本信息
docker info #显示dcoker的系统信息,包括镜像和容器的数量
docker [命令] --help #帮助命令
docker官方文档地址:https://docs.docker.com/compose/reference/
镜像命令
docker images 查看所有本地主机上的镜像
[root@vm22 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
waibi/centos 1.0 161ad1397191 7 hours ago 231MB
tomcat 2.0 8663b1d60f71 8 hours ago 684MB
nginx latest 605c77e624dd 9 months ago 141MB
#解释
REPOSITORY 镜像的仓库源
TAG 镜像的标签
IMAGE ID 镜像的ID
CREATED 镜像的创建时间
SIZE 镜像的大小
#可选项
-a --all 显示所有镜像
-q --quiet 只显示景象ID
docker search 搜索镜像
[root@vm22 ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 13234 [OK]
mariadb MariaDB Server is a high performing open sou… 5062 [OK]
#可选项
--filter=starts=3000 #搜索出来镜像starts大于3000的镜像
docker pull 下载镜像
下载镜像 $ docker pull 镜像名[:tag]
[root@vm22 ~]# docker pull mysql
Using default tag: latest #如果指定tag,将默认下载latest
latest: Pulling from library/mysql
72a69066d2fe: Already exists #分层下载
93619dbc5b36: Already exists
99da31dd6142: Already exists
626033c43d70: Already exists
37d5d7efb64e: Already exists
ac563158d721: Already exists
d2ba16033dad: Already exists
688ba7d5c01a: Pull complete
00e060b6d11d: Pull complete
1c04857f594f: Pull complete
4d7cfa90e6ea: Pull complete
e0431212d27d: Pull complete
Digest: sha256:e9027fe4d91c0153429607251656806cc784e914937271037f7738bd5b8e7709 #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
docker rmi 删除镜像
[root@vm22 ~]# docker rmi -f 3218b38490ce #删除指定镜像
[root@vm22 ~]# docker rmi -f IMAGE ID IMAGE ID #删除多个镜像
[root@vm22 ~]# docker rmi -f $(docker images -aq) #删除全部镜像
容器命令
启动容器
$ docker run IMAGE [COMMAND] [ARG...]
# 参数说明
--name="name" 给容器命名
-d 后台运行
-it 使用交互方式运行,进入容器查看内容
-p 指定容器端口 -p 8080:8080
-p 主机端口:容器端口
-p IP:主机端口:容器端口
-P 随机指定端口
#启动并进入容器
[root@vm22 ~]# docker run -it centos /bin/bash
[root@bdcf79e05d39 /]# ls # 查看容器内的centos
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
[root@bdcf79e05d39 /]# exit # 退出容器
exit
列出所有正在运行的容器
# docker ps 命令
-a # 列出当前正在运行的容器+历史运行过的容器
-n=? # 显示最近创建的容器
-q # 只显示容器ID
[root@vm22 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@vm22 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bdcf79e05d39 centos "/bin/bash" 3 minutes ago Exited (0) About a minute ago brave_jepsen
退出容器
exit # 直接停止并退出容器
Ctrl + P + Q # 容器不停止退出
删除容器
docker rm 容器id #删除指定容器
docker rm -f $(docker ps -aq) # 删除所有容器
启动和停止容器操作
docker start 容器ID # 启动容器
docker stop 容器ID # 停止当前正在运行的容器
docker restart 容器ID # 重启容器
docker kill 容器ID # 强制停止容器
其他常用命令
后台启动容器
# 命令 doceker run -d 镜像
[root@vm22 ~]# docker run -d centos
查看日志
docker logs -f -t --tail 容器
# 测试
[root@vm22 ~]# docker run -d centos /bin/bash -c "while true;do echo print logs;sleep 1;done "
[root@vm22 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
82bb167358c7 centos "/bin/bash -c 'while…" 34 seconds ago Up 33 seconds hungry_mclean
# 显示日志
-tf
--tail number
[root@vm22 ~]# docker logs -t -f --tail 4 82bb167358c7
2022-09-28T17:41:05.146127148Z print logs
2022-09-28T17:41:06.151409147Z print logs
2022-09-28T17:41:07.157108266Z print logs
2022-09-28T17:41:08.161395599Z print logs
查看容器中进程信息
# 命令 docker top 容器ID
[root@vm22 ~]# docker top 82bb167358c7
UID PID PPID C STIME TTY TIME CMD
root 4773 4753 0 01:39 ? 00:00:00 /bin/bash -c while true;do echo print logs;sleep 1;done
root 5172 4773 0 01:44 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的元数据
# 命令 docker inspect 容器ID
[root@vm22 ~]# docker inspect 82bb167358c7
[
{
"Id": "82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b",
"Created": "2022-09-28T17:39:20.314052558Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo print logs;sleep 1;done "
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 4773,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-09-28T17:39:20.57549319Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
"ResolvConfPath": "/var/lib/docker/containers/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b/hostname",
"HostsPath": "/var/lib/docker/containers/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b/hosts",
"LogPath": "/var/lib/docker/containers/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b/82bb167358c7835cae235ad9d3ba2097e5d63ecd7286de50fb26061467d9ef4b-json.log",
"Name": "/hungry_mclean",
"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/fcd02afc26d5c1aea385a6b77399b4ec4af0f356359e91041cde84cb9b1237c7-init/diff:/var/lib/docker/overlay2/d95e550d4f62886c70a537524378ebfba5e96932146481454237df6005a0cb8c/diff",
"MergedDir": "/var/lib/docker/overlay2/fcd02afc26d5c1aea385a6b77399b4ec4af0f356359e91041cde84cb9b1237c7/merged",
"UpperDir": "/var/lib/docker/overlay2/fcd02afc26d5c1aea385a6b77399b4ec4af0f356359e91041cde84cb9b1237c7/diff",
"WorkDir": "/var/lib/docker/overlay2/fcd02afc26d5c1aea385a6b77399b4ec4af0f356359e91041cde84cb9b1237c7/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "82bb167358c7",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
],
"Cmd": [
"/bin/bash",
"-c",
"while true;do echo print logs;sleep 1;done "
],
"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": "0cd61c994ce552f0c40d92076a80223aa1ca509814ced1ba2e05ca3006f800a1",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/0cd61c994ce5",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "4791507c4b3d9221dcca506844705803e4d6faafeacfa206225650490224ba02",
"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": "2893fe225a852d859fb8e831ff668623b9f220c3f47a8b1b83bb31691a8961b5",
"EndpointID": "4791507c4b3d9221dcca506844705803e4d6faafeacfa206225650490224ba02",
"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 -it 容器ID /bin/bahs # 进入容器后开启一个新的终端
docker attch 容器ID # 进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机上
docker cp 容器ID:容器内路径 目标路径
[root@vm22 ~]# docker cp cfb377b1b56f:/home/docker-cp /home
[root@vm22 ~]# ls /home
docker-cp docker-test-volume mysql test
学习视频:https://www.bilibili.com/video/BV1og4y1q7M4/?spm_id_from=333.999.0.0&vd_source=4297c14c1c7438e292ea9310e1abeb31