Docker安装: https://docs.docker.com/engine/install/centos/
创建交互式容器
# 运行docker容器, 退出则容器停止运行
docker run --name ubuntu-test -it ubuntu /bin/bash
--name 指定名称
-it 为新建的容器提供一个交互式shell
# 启动容器
docker start ubuntu-test
# 附着到运行中的容器上, 退出则容器停止运行
docker attach ubuntu-test
创建守护式容器
# 后台运行容器
docker run --name ubuntu-test2 -d -p 81:80 ubuntu /bin/bash -c "while true;do echo hello world; sleep 1; done"
-d 后台运行容器
-p 指定端口映射(宿主机:容器)
# 获取容器日志
docker logs -ft ubuntu-test2
-f 跟踪最新日志, 也可以写成"docker logs --tail 100f ubuntu-test2"
-t 为日志加上时间戳
# 查看容器内的进程
docker top ubuntu-test2
# 查看一个或多个容器的统计信息(CPU, 内存, 网络IO, 存储IO)
docker stats ubuntu-test2 ubuntu-test3
# 在容器内部运行进程
# 以后台任务模式在容器内创建文件
docker exec -d ubuntu-test2 touch /etc/a.txt
#以交互式任务模式在容器中启动一个shell
docker exec -it ubuntu-test2 /bin/bash
# 停止容器, 后接名称或ID
docker stop ubuntu-test2
# 查看最近x个容器, 不论运行还是停止
docker ps -n x
# 删除容器
docker rm ubuntu-test2
# 删除所有容器, -aq 返回所有容器的ID列表
docker rm `docker ps -aq`
docker日志驱动
docker run --log-driver="syslog" --name ubuntu-test3 -d ubuntu /bin/bash -c "while true;do echo hello world; sleep 1; done"
--log-driver 可以指定日志驱动选项
1.默认"json-file", 为docker logs提供基础
2."syslog"选项, 禁用docker logs, 可以在启动Docker守护进程时指定该选项, 将所有容器日志输出都重定向到Syslog, 或者通过docker run 对个别容器进行日志重定向输出
3."none"选项, 禁用所有日志
深入容器
# 获取详细的容器信息
docker inspect ubuntu-test2
[
{
"Id": "bf0e685e1a8860f71c0ba1a97dc8e2d7dabc000c9c6437a655cd659fd4e33075",
"Created": "2022-05-16T05:29:29.9735706Z",
"Path": "/bin/bash",
"Args": [
"-c",
"while true;do echo hello world; sleep 1; done"
],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 1403,
"ExitCode": 0,
"Error": "",
"StartedAt": "2022-05-16T05:29:30.6428712Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:ba6acccedd2923aee4c2acc6a23780b14ed4b8a5fa4e14e252a23b846df9b6c1",
"ResolvConfPath": "/var/lib/docker/containers/bf0e685e1a8860f71c0ba1a97dc8e2d7dabc000c9c6437a655cd659fd4e33075/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/bf0e685e1a8860f71c0ba1a97dc8e2d7dabc000c9c6437a655cd659fd4e33075/hostname",
"HostsPath": "/var/lib/docker/containers/bf0e685e1a8860f71c0ba1a97dc8e2d7dabc000c9c6437a655cd659fd4e33075/hosts",
"LogPath": "/var/lib/docker/containers/bf0e685e1a8860f71c0ba1a97dc8e2d7dabc000c9c6437a655cd659fd4e33075/bf0e685e1a8860f71c0ba1a97dc8e2d7dabc000c9c6437a655cd659fd4e33075-json.log",
"Name": "/ubuntu-test2",
"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/7c3f69f58e8da043b9a6c98e00ad0c89ce8d57be772f02a2c532a8b5f0de238e-init/diff:/var/lib/docker/overlay2/b140d38824482627e53ae88d80f69d2420d112c97303fe5af92280a2af73c74a/diff",
"MergedDir": "/var/lib/docker/overlay2/7c3f69f58e8da043b9a6c98e00ad0c89ce8d57be772f02a2c532a8b5f0de238e/merged",
"UpperDir": "/var/lib/docker/overlay2/7c3f69f58e8da043b9a6c98e00ad0c89ce8d57be772f02a2c532a8b5f0de238e/diff",
"WorkDir": "/var/lib/docker/overlay2/7c3f69f58e8da043b9a6c98e00ad0c89ce8d57be772f02a2c532a8b5f0de238e/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "bf0e685e1a88",
"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 hello world; sleep 1; done"
],
"Image": "ubuntu",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {
"desktop.docker.io/wsl-distro": "Ubuntu-20.04"
}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "36a33e3f4803a5f612acfe85d0d8fad878a9399fd49a3097e0c379bc65e726e0",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/36a33e3f4803",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "4353c8520732d62dceb9f88f806f3a0a587db576d7dff37415297514ce63f414",
"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": "b36033f925f386ce30ea7e0d90ff57d51dc07a3fa0890b865312b9e65ae3395a",
"EndpointID": "4353c8520732d62dceb9f88f806f3a0a587db576d7dff37415297514ce63f414",
"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
}
}
}
}
]
# 通过-f来选定查看结果
# 查看容器IP
docker inspect -f '{{.NetworkSettings.IPAddress}}' ubuntu-test2
# 查看多个容器状态
docker inspect -f '{{.Name}}{{.State.Running}}' ubuntu-test2 ubuntu-test3