注意:
docker使用后台运行,必须有一个前台进程,docker发现没有应用,就会自动停止;
nginx容器启动后,发现自己没有提供服务,就会立即停止,就是没有程序了。
1.查看日志
docker log -f -t --tail 10 容器
#shell 脚本 docker run -d centos /bin/sh -c "while true;do echo rizhi;sleep 1;done"
查看日志
-tf : #显示日志 t:时间戳 f:动态显示
–tail number # 要显示的日志条数
[root@VM-0-9-centos ~]# docker logs -tf --tail 10 3f818d81c23e 2021-08-24T14:22:30.123258831Z rizhi 2021-08-24T14:22:31.124990296Z rizhi 2021-08-24T14:22:32.126686655Z rizhi 2021-08-24T14:22:33.128395685Z rizhi 2021-08-24T14:22:34.130117944Z rizhi 2021-08-24T14:22:35.131881590Z rizhi 2021-08-24T14:22:36.133743533Z rizhi 2021-08-24T14:22:37.136511874Z rizhi 2021-08-24T14:22:38.137442775Z rizhi 2021-08-24T14:22:39.139236017Z rizhi 2021-08-24T14:22:40.141035656Z rizhi 2021-08-24T14:22:41.142850480Z rizhi 2021-08-24T14:22:42.144932998Z rizhi 2021-08-24T14:22:43.146684473Z rizhi
查看容器中的进程信息 ps
命令 docker top 容器id
[root@VM-0-9-centos ~]# docker top 3f818d81c23e UID PID PPID C STIME TTY TIME CMD root 1496763 1496745 0 22:20 ? 00:00:00 /bin/sh -c while true;do echo rizhi;sleep 1;done root 1498196 1496763 0 22:26 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
查看镜像的元数据
docker inspect 容器id
[root@VM-0-9-centos ~]# docker inspect 3f818d81c23e [ { "Id": "3f818d81c23e68dbf46d9022e9693d72460db40f68a8bd3b07b57fcac1e016a4", "Created": "2021-08-24T14:20:14.532415456Z", "Path": "/bin/sh", "Args": [ "-c", "while true;do echo rizhi;sleep 1;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 1496763, "ExitCode": 0, "Error": "", "StartedAt": "2021-08-24T14:20:14.856594947Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55", "ResolvConfPath": "/var/lib/docker/containers/3f818d81c23e68dbf46d9022e9693d72460db40f68a8bd3b07b57fcac1e016a4/resolv.conf", "HostnamePath": "/var/lib/docker/containers/3f818d81c23e68dbf46d9022e9693d72460db40f68a8bd3b07b57fcac1e016a4/hostname", "HostsPath": "/var/lib/docker/containers/3f818d81c23e68dbf46d9022e9693d72460db40f68a8bd3b07b57fcac1e016a4/hosts", "LogPath": "/var/lib/docker/containers/3f818d81c23e68dbf46d9022e9693d72460db40f68a8bd3b07b57fcac1e016a4/3f818d81c23e68dbf46d9022e9693d72460db40f68a8bd3b07b57fcac1e016a4-json.log", "Name": "/unruffled_elgamal", "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/389f807d7230a4c81797f454300f912737f696aa4b5cf29753a75a510ead643f-init/diff:/var/lib/docker/overlay2/f1fa23a7c80519d2310bc03823a8b7f49b1995417c262e426aee026cb9e052b3/diff", "MergedDir": "/var/lib/docker/overlay2/389f807d7230a4c81797f454300f912737f696aa4b5cf29753a75a510ead643f/merged", "UpperDir": "/var/lib/docker/overlay2/389f807d7230a4c81797f454300f912737f696aa4b5cf29753a75a510ead643f/diff", "WorkDir": "/var/lib/docker/overlay2/389f807d7230a4c81797f454300f912737f696aa4b5cf29753a75a510ead643f/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "3f818d81c23e", "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/sh", "-c", "while true;do echo rizhi;sleep 1;done" ], "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": "2bbff1d7c403e316862afc4682c0ddbaae5eb510b8297515fd9b9d4fbcbc945d", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/2bbff1d7c403", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "d0a4a5a7dcfa20c18b2892bbe2e6df23e1f7857e5d0b0a0c677b43b90fd784c4", "Gateway": "172.18.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.18.0.4", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:12:00:04", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "61e72818ec60a0e3a31c5ba57e82e033446e52aac5837926e0e2c8216572826b", "EndpointID": "d0a4a5a7dcfa20c18b2892bbe2e6df23e1f7857e5d0b0a0c677b43b90fd784c4", "Gateway": "172.18.0.1", "IPAddress": "172.18.0.4", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:12:00:04", "DriverOpts": null } } } } ]
进入当前正在运行的容器
我们通常容器都是使用后台方式运行,需要进入容器修改配置
命令
docker exec -it 容器di bashShell
# 方式1 [root@VM-0-9-centos ~]# docker exec -it 045a9b51b395 /bin/bash root@045a9b51b395:/usr/local/tomcat# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 02:27 pts/0 00:00:39 /usr/local/openjdk-11/bin/java -Djava.util.logging.config.file=/usr/loc root 27 0 0 14:39 pts/1 00:00:00 /bin/bash root 34 27 0 14:39 pts/1 00:00:00 ps -ef # 方式2 # docker attach 容器id docker attach e160eb260ebd
docker exec :进入容器后开启一个新的终端,可以在里面操作(常用)
docker attach:进入容器正在执行的终端,不会启动新的进程
从容器内拷贝文件到主机
[root@3f818d81c23e /]# cd /home/ #在容器内新建文件test.java [root@3f818d81c23e home]# touch test.java [root@3f818d81c23e home]# ls test.java [root@3f818d81c23e home]# exit exit [root@VM-0-9-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3f818d81c23e centos "/bin/sh -c 'while t…" 32 minutes ago Up 32 minutes unruffled_elgamal 045a9b51b395 tomcat "catalina.sh run" 12 hours ago Up 12 hours 8080/tcp tomcat #容器内的 test.java 拷贝到主机 [root@VM-0-9-centos ~]# docker cp 3f818d81c23e:/home/test.java /home
拷贝是一个手动过程,未来使用-V卷的技术