Docker 是怎么工作的?
Docker 是一个 Client – Server 构造的零碎,Docker 的守护过程运行在宿主机上,通过 Socket 从客户端拜访。Docker-Server 收到 Docker-Client 的指令,就会执行命令。
Docker 为什么比 VM 快?
1.Docker 有更少的形象层
2.Docker 利用的是宿主机的内核,VM 须要 Guest OS(操作系统内核)
帮忙命令
docker version
docker info #零碎信息,包含镜像和容器的数量
docker xxx --help
官网文档:https://docs.docker.com/refer…
镜像命令
docker images --help
docker images 查看所有本地镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest d1165f221234 4 months ago 13.3kB
REPOSITORY #镜像仓库源
TAG #镜像标签
IMAGE ID #镜像 ID
CREATED #镜像创立工夫
SIZE #镜像大小
#可选项
-a --all #列出所有镜像
-q --quiet #只显示镜像 ID
docker search 搜寻镜像
docker search mysql
#可选项
-f --filter #过滤条件
--fitler=STARS=3000 #珍藏 3000 以上的镜像
docker pull 下载镜像
Dockerhub 地址:https://hub.docker.com
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
OPTIONS #可选项
NAME #镜像名
TAG #镜像标签
#指定版本下载
docker pull mysql:5.7
docker rmi 删除镜像
docker rmi -f 容器 ID1 ... 容器 IDN
#删除全副镜像
docker rmi -f $(docker images -aq)
容器命令
docker run 新建并启动容器
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
#参数阐明
--name #容器名
-d #后盾形式运行
-it #交互方式运行,进入容器查看
-p #指定容器端口
-P #随机指定端口
#测试:启动并进入容器
docker pull centos
docker run -it centos /bin/bash
#退出容器
exit #容器进行并退出
CTRL + P + Q #不进行退出
docker ps 列出容器
docker ps #列出以后正在运行的容器
-a #列出以后正在运行的容器 + 历史运行过的容器
-n=? #最近创立的容器
-q #只显示容器编号
docker rm 删除容器
docker rm 容器 ID
docker rm $(docker ps -aq) #删除所有容器
-f #强制删除(包含运行中的容器)
docker start 启动容器
docker start 容器 ID
docker restart 容器 ID
docker stop 容器 ID
docker kill 容器 ID
docker logs 查看日志
docker logs -tf --tail 10 容器 ID #显示指定行数
docker logs -t -f 容器 ID #显示日志
docker top 查看过程
docker top 容器 ID
docker inspect 查看元数据
docker inspect 容器 ID
[
{
"Id": "d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63",
"Created": "2021-07-29T23:22:16.737750806Z",
"Path": "/bin/bash",
"Args": [],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 137,
"Error": "","StartedAt":"2021-07-29T23:31:58.686730736Z","FinishedAt":"2021-07-29T23:32:10.840176294Z"},"Image":"sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55","ResolvConfPath":"/var/lib/docker/containers/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63/resolv.conf","HostnamePath":"/var/lib/docker/containers/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63/hostname","HostsPath":"/var/lib/docker/containers/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63/hosts","LogPath":"/var/lib/docker/containers/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63/d3b9799417b69391ba9bdd644b7a5604780cecc371a6511a62b5503d063c3e63-json.log","Name":"/optimistic_bartik","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/fd5fe8080f2c13c00311711f4e51865588f39c784047fcdce26d4611f6128587-init/diff:/var/lib/docker/overlay2/27c6cfd367d905528b550279efab95fa7f52c97abb12625131c408b6dea72fe7/diff",
"MergedDir": "/var/lib/docker/overlay2/fd5fe8080f2c13c00311711f4e51865588f39c784047fcdce26d4611f6128587/merged",
"UpperDir": "/var/lib/docker/overlay2/fd5fe8080f2c13c00311711f4e51865588f39c784047fcdce26d4611f6128587/diff",
"WorkDir": "/var/lib/docker/overlay2/fd5fe8080f2c13c00311711f4e51865588f39c784047fcdce26d4611f6128587/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "d3b9799417b6",
"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":"0ac7744565902b645737fcc3eabd47c9c27a7babaa0856ec55e466a7237e6ba8","HairpinMode": false,"LinkLocalIPv6Address":"",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/0ac774456590",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "","Gateway":"",
"GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"IPAddress":"",
"IPPrefixLen": 0,
"IPv6Gateway": "","MacAddress":"",
"Networks": {
"bridge": {
"IPAMConfig": null,
"Links": null,
"Aliases": null,
"NetworkID": "2d10dc4aa739ceb9a9aad3c64688a40431ff71e5dfaed298ec83265434d7863c",
"EndpointID": "","Gateway":"",
"IPAddress": "","IPPrefixLen": 0,"IPv6Gateway":"",
"GlobalIPv6Address": "","GlobalIPv6PrefixLen": 0,"MacAddress":"",
"DriverOpts": null
}
}
}
}
]
docker exec\attach 进入以后正在运行的容器
docker exec -it 容器 ID /bin/bash
#进入容器后开启新的终端,能够操作
docker attach 容器 ID
#进入容器正在执行的终端,不会启动新的过程
docker cp 拷贝文件
docker cp 容器 ID: 容器内门路 宿主机门路
其余
docker run -d 容器 ID
docker ps 发现容器进行了
起因:docker 容器在后盾运行,必须有一个前台过程,如果 docker 发现没有利用,就会主动进行
小结