Docker學習筆記(5)容器的其他常用命令
阿新 • • 發佈:2020-10-28
1.後臺啟動
docker run -d 映象名
2.檢視日誌
docker logs -tf --tail
後臺啟動容器,會發現一個問題,啟動後檢視發現並沒有啟動的程序
說明:
這裡會有一個坑:容器使用後臺執行,就必須要有一個前臺程序,docker發現沒有前臺就會自動停止
會發現沒有日誌
自己寫一段shell指令碼就可以實現
docker run -d centos /bin/sh -c "while true;do echo dm;sleep 1;done"
然後檢視日誌
docker logs -tf --tail 10 容器id
3. 檢視容器中程序資訊
docker top 容器id
4.檢視映象的元資料
docker inspect 容器id
[root@dm dumeng]# docker inspect a22e31544f58 [ { "Id": "a22e31544f5806998af8552e09bf1b3d4f02a17866f2af97b4bfbcf0f97faf42", "Created": "2020-10-28T03:11:52.874465598Z", "Path": "/bin/sh", "Args": [ "-c", "while true;do echo dm;sleep 1;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 4043, "ExitCode": 0, "Error": "","StartedAt": "2020-10-28T03:11:53.164089245Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566", "ResolvConfPath": "/var/lib/docker/containers/a22e31544f5806998af8552e09bf1b3d4f02a17866f2af97b4bfbcf0f97faf42/resolv.conf", "HostnamePath": "/var/lib/docker/containers/a22e31544f5806998af8552e09bf1b3d4f02a17866f2af97b4bfbcf0f97faf42/hostname", "HostsPath": "/var/lib/docker/containers/a22e31544f5806998af8552e09bf1b3d4f02a17866f2af97b4bfbcf0f97faf42/hosts", "LogPath": "/var/lib/docker/containers/a22e31544f5806998af8552e09bf1b3d4f02a17866f2af97b4bfbcf0f97faf42/a22e31544f5806998af8552e09bf1b3d4f02a17866f2af97b4bfbcf0f97faf42-json.log", "Name": "/elegant_ritchie", "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, "Capabilities": null, "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/04fbd86b30c20d0322139e4347ee4d8a572cdfb7d75b8472059c09c9e932da9f-init/diff:/var/lib/docker/overlay2/ba7c3c731542b86dbd3054837980d0d0b97d5935cea10f4207686a359ccb3ba1/diff", "MergedDir": "/var/lib/docker/overlay2/04fbd86b30c20d0322139e4347ee4d8a572cdfb7d75b8472059c09c9e932da9f/merged", "UpperDir": "/var/lib/docker/overlay2/04fbd86b30c20d0322139e4347ee4d8a572cdfb7d75b8472059c09c9e932da9f/diff", "WorkDir": "/var/lib/docker/overlay2/04fbd86b30c20d0322139e4347ee4d8a572cdfb7d75b8472059c09c9e932da9f/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "a22e31544f58", "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 dm;sleep 1;done" ], "Image": "centos", "Volumes": null, "WorkingDir": "", "Entrypoint": null, "OnBuild": null, "Labels": { "org.label-schema.build-date": "20200809", "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": "6a0a80eef7fc4013dbffa08b8fe767930d8187258a5922ae22a4a430b4ae8671", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": {}, "SandboxKey": "/var/run/docker/netns/6a0a80eef7fc", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "06024b25d63dda01da0fab2835fa28dab5a2a3a2a3cbee09008cdd7fd35fecaa", "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": "6a3eea87c9a42f171f41908b962c4127c912358b541c893752041ec8ca3394ef", "EndpointID": "06024b25d63dda01da0fab2835fa28dab5a2a3a2a3cbee09008cdd7fd35fecaa", "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 } } } } ]
5.進入當前執行的容器
docker exec -it 容器id /bin/bash
docker attach 容器id
兩者區別:
- docker exec :進入容器開啟一個新的終端,可以在裡面操作(常用)
- docker attach :進入容器正在執行的終端,不會啟動新的執行緒
6. 從容器內拷貝檔案到主機上
docker cp 容器id:容器內路徑 目的主機路徑
測試:
1.以互動式開啟一個容器
docker run -it centos
2.建立一個檔案 a.java
touch a.java
3. Ctrl + P + Q
4. 複製檔案
docker cp 1945ba2c2b0f:/home/a.java /home