docker version #显示docker的版本信息
docker info #显示docker的系统信息,包括镜像和容器的数量
docker 命令 --help #帮助命令
帮助文档地址:https://docs.docker.com/engine/reference/commandline/
docker images 查看所有本地的主机上的镜像
[root@izbp1191gh7vw5p1u5ru50z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 11 days ago 13.3kB
# 解释
#REPOSITORY :镜像的名
#TAG :镜像的标签
#IMAGE ID :镜像的ID
#CREATED :镜像的创建时间
#SIZE :镜像的大小
#常用可选项:
-a, --all #列出所有的镜像 (default hides intermediate images)
-q, --quiet #只显示ID
docker search 搜索镜像
[root@izbp1191gh7vw5p1u5ru50z ~]# docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10624 [OK]
mariadb MariaDB Server is a high performing open sou… 3980 [OK]
#可选项,通过收藏来过滤
# --filter=STARS=3000 搜索出来的镜像都是STARS大于3000的
[root@izbp1191gh7vw5p1u5ru50z ~]# docker search mysql --filter=STARS=3000
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relation… 10624 [OK]
mariadb MariaDB Server is a high performing open sou… 3980 [OK]
docker pull下载镜像
#下载镜像 docker pull 镜像名[:tag]
[root@izbp1191gh7vw5p1u5ru50z ~]# docker pull mysql
Using default tag: latest #如果不写tag,默认就是latest
latest: Pulling from library/mysql
a076a628af6f: Pull complete #分层下载,docker image的核心,联合文佳系统(属于docker高级部分,后面再说)
f6c208f3f991: Pull complete
88a9455a9165: Pull complete
406c9b8427c6: Pull complete
7c88599c0b25: Pull complete
25b5c6debdaf: Pull complete
43a5816f1617: Pull complete
1a8c919e89bf: Pull complete
9f3cf4bd1a07: Pull complete
80539cea118d: Pull complete
201b3cad54ce: Pull complete
944ba37e1c06: Pull complete
Digest: sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c #签名
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest #真实地址
#以下两个命令是等价的
#docker pull mysql
#docker pull docker.io/library/mysql:latest
#指定版本下载
[root@izbp1191gh7vw5p1u5ru50z ~]# docker pull mysql:5.7
5.7: Pulling from library/mysql
a076a628af6f: Already exists #分层是个非常高级的东西,这部分之前已经下载,直接共用,极大的减少了内存浪费
f6c208f3f991: Already exists
88a9455a9165: Already exists
406c9b8427c6: Already exists
7c88599c0b25: Already exists
25b5c6debdaf: Already exists
43a5816f1617: Already exists
1831ac1245f4: Pull complete
37677b8c1f79: Pull complete
27e4ac3b0f6e: Pull complete
7227baa8c445: Pull complete
Digest: sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
#查询一下
[root@izbp1191gh7vw5p1u5ru50z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 11 days ago 13.3kB
mysql 5.7 a70d36bc331a 8 weeks ago 449MB
mysql latest c8562eaf9d81 8 weeks ago 546MB
docker rmi 删除镜像,rm代表删除,i代表镜像
#-f代表全部删除 a70d36bc331a代表镜像ID,是mysql5.7
[root@izbp1191gh7vw5p1u5ru50z ~]# docker rmi -f a70d36bc331a
Untagged: mysql:5.7
Untagged: mysql@sha256:b3d1eff023f698cd433695c9506171f0d08a8f92a0c8063c1a4d9db9a55808df
Deleted: sha256:a70d36bc331a13d297f882d3d63137d24b804f29fa67158c40ad91d5050c39c5
Deleted: sha256:50c77bf7bcddd1f1d97789d80ac2404eec22c860c104e858620d2a2e321f0ef7
Deleted: sha256:14244329b83dfc8982398ee4104a548385652d2bffb957798ff86a419013efd6
Deleted: sha256:6d990477f90af28473eb601a9bca22253f6381e053c5a8edda0a4f027e124a3c
Deleted: sha256:ee0449796df204071589162fc16f8d65586312a40c68d1ba156c93c56f5e5ce8
#此时mysql5.7已经删除
[root@izbp1191gh7vw5p1u5ru50z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 11 days ago 13.3kB
mysql latest c8562eaf9d81 8 weeks ago 546MB
#可以全部删除,注意$()是centos的内部命令,可以输出括号内的值。
[root@izbp1191gh7vw5p1u5ru50z ~]# docker rmi -f $(docker images -aq)
Untagged: hello-world:latest
Untagged: hello-world@sha256:308866a43596e83578c7dfa15e27a73011bdd402185a84c5cd7f32a88b501a24
Deleted: sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726
Untagged: mysql:latest
Untagged: mysql@sha256:feada149cb8ff54eade1336da7c1d080c4a1c7ed82b5e320efb5beebed85ae8c
Deleted: sha256:c8562eaf9d81c779cbfc318d6e01b8e6f86907f1d41233268a2ed83b2f34e748
Deleted: sha256:1b649b85960473808c6b812fc30c3f6a3ff1c0ffdcba5c9435daf01cf7d5373a
Deleted: sha256:19cc889447050c16c797fd209fa114ee219de23facb37c00d4137a4ed4aad922
Deleted: sha256:3c793c06a026d276cf56a6a6a75527026ed9eafa7a7d21a438f7d5ed2314148e
Deleted: sha256:1e1cd89a2bc183a7fea3dab0b543e9924278321ad0921c22cc088adbf3c2e77b
Deleted: sha256:83b2015dfd000588c7c947b2d89b3be7a8e5a3abc6ab562668c358033aa779ec
Deleted: sha256:d08533f1e2acc40ad561a46fc6a76b54c739e6b24f077c183c5709e0a6885312
Deleted: sha256:4f9d91a4728e833d1062fb65a792f06e22e425f63824f260c8b5a64b776ddc38
Deleted: sha256:20bf4c759d1b0d0e6286d2145453af4e0e1b7ba3d4efa3b8bce46817ad4109de
Deleted: sha256:a9371bbdf16ac95cc72555c6ad42f79b9f03a82d964fe89d52bdc5f335a5f42a
Deleted: sha256:5b02130e449d94f51e8ff6e5f7d24802246198749ed9eb064631e63833cd8f1d
Deleted: sha256:ab74465b38bc1acb16c23091df32c5b7033ed55783386cb57acae8efff9f4b37
Deleted: sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864
#docker rmi -f 镜像id 删除指定的镜像
#docker rmi -f 镜像id 镜像id 镜像id 删除多个镜像
#docker rmi -f $(docker images -aq) 删除全部的容器
说明:我们有了镜像才可以创建容器,linux,下载一个centos镜像来测试学习
[root@izbp1191gh7vw5p1u5ru50z ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
docker.io/library/centos:latest
新建容器并启动
docker run [可选参数] image
#参数说明
--name="Name" 容器名字 例如tomcat1 tomcat2 用来区分容器
-d 后台方式运行
-it 使用交互方式运行,进入容器查看内容
-p (小写)指定容器的端口
格式:
-p ip:主机端口:容器端口
-p 主机端口:容器端口(常用),如-p 8888:8080
-p 容器端口
-P (大写)随机指定端口(容器内部端口随机映射到主机的端口)
#启动并进入容器
[root@izbp1191gh7vw5p1u5ru50z ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 3 months ago 209MB
[root@izbp1191gh7vw5p1u5ru50z ~]# docker run -it centos /bin/bash
[root@29816dba2381 /]# ls #查看容器内的centos,这个centos属于基础版本,很多命令都是不完善的!
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
#从容器退会主机
[root@29816dba2381 /]# exit
exit
[root@izbp1191gh7vw5p1u5ru50z ~]#
退出容器
exit #直接容器停止并退出
Ctrl + P + Q #容器不停止退出
列出所有的运行的容器
#docker ps 命令
#列出当前正在运行的容器
-a #列出当前正在运行的容器以及历史运行过的容器
-n=? #显示最近创建的容器
-q #静默模式,只显示容器的编号
[root@izbp1191gh7vw5p1u5ru50z /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@izbp1191gh7vw5p1u5ru50z /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
29816dba2381 centos "/bin/bash" 6 minutes ago Exited (130) 3 minutes ago priceless_hertz
1f45eb6b7f44 d1165f221234 "/hello" 19 hours ago Exited (0) 19 hours ago musing_perlman
删除容器
docker rm 容器id #删除指定的容器,不能删除正在运行的容器,
#如果要强制删除 rm -f
docker rm -f $(docker ps -aq) #删除所有的容器
docker ps -a -q|xargs docker rm #删除所有的容器
启动和停止容器的操作
docker start 容器id #启动容器,docker run只有在第一次运行时使用,
#将镜像放到容器中,以后再次启动这个容器的时候,
#只需要使用命令docker start就可以。
docker restart 容器id #重启容器
docker stop 容器id #停止当前正在运行的容器
doccker kill 容器id #强制停止当前容器
[root@izbp1191gh7vw5p1u5ru50z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e5fb14ea9c7 centos "/bin/bash" 3 minutes ago Up About a minute wizardly_shockley
[root@izbp1191gh7vw5p1u5ru50z ~]# 8e5fb14ea9c7
[root@izbp1191gh7vw5p1u5ru50z ~]# docker stop 8e5fb14ea9c7
8e5fb14ea9c7
[root@izbp1191gh7vw5p1u5ru50z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@izbp1191gh7vw5p1u5ru50z ~]# docker start 8e5fb14ea9c7
8e5fb14ea9c7
[root@izbp1191gh7vw5p1u5ru50z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8e5fb14ea9c7 centos "/bin/bash" 3 minutes ago Up 2 seconds wizardly_shockley
后台启动容器
# 命令 docker run -d 镜像名
[root@izbp1191gh7vw5p1u5ru50z ~]# docker run -d centos
09704bfd7bebacb1bc5974b15f2814ca1607f0b4b1687ecd6b0ea46d90abb9b3
[root@izbp1191gh7vw5p1u5ru50z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#问题 docker ps ,发现centos停止了
#常见的坑:docker 容器使用后台运行,就必须要有一个前台进程,docker发现没有应用了,就会自动停止。
#nginx,容器启动后,发现自己额米有提供服务,就会立刻停止,就是没有程序了。
查看日志
# docker logs -t -f --tail 数字
-t #显示时间戳
-f #持续显示日志
--tail #显示末尾的记录
[root@izbp1191gh7vw5p1u5ru50z ~]# docker logs -tf --tail 10 8e5fb14ea9c7
查看容器中的进程信息
# docker top 容器id
[root@izbp1191gh7vw5p1u5ru50z ~]# docker top 8e5fb14ea9c7
UID PID PPID C STIME TTY TIME CMD
root 8974 8941 0 13:28 pts/0 00:00:00 /bin/bash
查看镜像的元数据
#命令
docker inspect 容器id
#测试
[root@izbp1191gh7vw5p1u5ru50z ~]# docker inspect 8e5fb14ea9c7
[
{
"Id": "8e5fb14ea9c706bf18b68cee2f591ee4bb81d4880c417cadbb2ce10fec0e686c",
"Created": "2021-03-17T05:13:08.525134158Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 8974,
"ExitCode": 0,
"Error": "",
"StartedAt": "2021-03-17T05:29:00.015412527Z",
"FinishedAt": "2021-03-17T05:18:49.821629259Z"
},
"Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
"ResolvConfPath": "/var/lib/docker/containers/8e5fb14ea9c706bf18b68cee2f591ee4bb81d4880c417cadbb2ce10fec0e686c/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/8e5fb14ea9c706bf18b68cee2f591ee4bb81d4880c417cadbb2ce10fec0e686c/hostname",
"HostsPath": "/var/lib/docker/containers/8e5fb14ea9c706bf18b68cee2f591ee4bb81d4880c417cadbb2ce10fec0e686c/hosts",
"LogPath": "/var/lib/docker/containers/8e5fb14ea9c706bf18b68cee2f591ee4bb81d4880c417cadbb2ce10fec0e686c/8e5fb14ea9c706bf18b68cee2f591ee4bb81d4880c417cadbb2ce10fec0e686c-json.log",
"Name": "/wizardly_shockley",
"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/91f427077545eb5af9267e22b9d33318a879637e73b05326fd28ebee687a1e8d-init/diff:/var/lib/docker/overlay2/2613cdd0e800bf5c3db5b500048c868206aac74fe88d5fa881c91ee9d928d1a4/diff",
"MergedDir": "/var/lib/docker/overlay2/91f427077545eb5af9267e22b9d33318a879637e73b05326fd28ebee687a1e8d/merged",
"UpperDir": "/var/lib/docker/overlay2/91f427077545eb5af9267e22b9d33318a879637e73b05326fd28ebee687a1e8d/diff",
"WorkDir": "/var/lib/docker/overlay2/91f427077545eb5af9267e22b9d33318a879637e73b05326fd28ebee687a1e8d/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "8e5fb14ea9c7",
"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": "20201204",
"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": "e2f461e33b70e031a6d76274e4bd91884c363545f8f75536f05e36e954744ac7",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
},
"SandboxKey": "/var/run/docker/netns/e2f461e33b70",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "37acd82c6b2099de8a926ca737793a0e2ec8e1da66af34e8a7751ca86ca41cdf",
"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": "6f227d7d636d643fd98f4f999a572cc187eaeff2363bcbffc47f89cd264d62c4",
"EndpointID": "37acd82c6b2099de8a926ca737793a0e2ec8e1da66af34e8a7751ca86ca41cdf",
"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 bashShell
#测试
[root@izbp1191gh7vw5p1u5ru50z ~]# docker exec -it 8e5fb14ea9c7 /bin/bash
[root@8e5fb14ea9c7 /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@8e5fb14ea9c7 /]# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 05:28 pts/0 00:00:00 /bin/bash
root 15 0 0 05:44 pts/1 00:00:00 /bin/bash
root 30 15 0 05:44 pts/1 00:00:00 ps -ef
#方式二
docker attach 容器id
[root@izbp1191gh7vw5p1u5ru50z ~]# docker attach 8e5fb14ea9c7
[root@8e5fb14ea9c7 /]#
#docker exec 进入容器后开启一个新的终端,可以在里面操作(常用)
#docker attach 进入容器正在执行的终端,不会启动新的进程!
从容器内拷贝文件到主机上
docker cp 容器id:容器内路径 目的的主机路径
#进入docker容器内部
[root@izbp1191gh7vw5p1u5ru50z ~]# docker attach 8e5fb14ea9c7
[root@8e5fb14ea9c7 /]# cd /home
[root@8e5fb14ea9c7 home]# ls
#在容器内新建一个文件
[root@8e5fb14ea9c7 home]# touch test.java
[root@8e5fb14ea9c7 home]# ls
test.java
[root@8e5fb14ea9c7 home]# exit
exit
[root@izbp1191gh7vw5p1u5ru50z ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@izbp1191gh7vw5p1u5ru50z ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
09704bfd7beb centos "/bin/bash" 39 minutes ago Exited (0) 39 minutes ago affectionate_wu
8e5fb14ea9c7 centos "/bin/bash" 45 minutes ago Exited (0) 11 seconds ago wizardly_shockley
#将这个文件拷贝到主机上
[root@izbp1191gh7vw5p1u5ru50z ~]# docker cp 8e5fb14ea9c7:/home/test.java /home
[root@izbp1191gh7vw5p1u5ru50z ~]# cd /home
[root@izbp1191gh7vw5p1u5ru50z home]# ls
admin test.java
#注意,拷贝是一个手动过程,未来我们使用-v卷的技术,可以实现自动同步。
attach #当前shell下attach连接指定运行镜像
build #通过Dockerfile定制镜像
commit #提交当前容器为新的镜像
cp #从容器中拷贝指定文件或目录到宿主机中
create #创建一个新的容器,同run,但不启动容器
diff #查看docker容器变化
events #从docker服务获取容器实时事件
exec #在已存在的容器上运行命令
export #导出容器的内容流作为一个tar归档文件[对应import]
history #展示一个镜像形成历史
images #列出系统当前镜像
import #从tar包中的内容创建一个新的文件系统映像[对应export]
info #显示系统相关信息
inspect #查看容器详细信息
kill #kill指定docker容器
load #从一个tar包中加载一个镜像[对应save]
login #注册或登入一个docker源服务器
logout #从当前docker registry退出
logs #输出当前容器日志信息
port #查看映射端口对应的容器内部源端口
pause #暂停容器
ps #列出容器列表
pull #从docker镜像源服务器拉取指定镜像或者库镜像
push #推送指定镜像或者库镜像至docker源服务器
restart #重启运行的容器
rm #移除一个或者多个容器
rmi #移除一个或者多个镜像[无容器使用该镜像才可删除,否则需删除相关容器才可继续或 -f 强制删除]
run #创建一个新的容器并运行一个命令
save #保存一个镜像为一个tar包
search #在docker hub中搜索镜像
start #启动容器
stop #停止容器
tag #给源中镜像打标签
top #查看容器中运行的进程信息
unpause #取消暂停容器
version #查看docker版本号
wait #截取容器停止时的退出状态值