1. 程式人生 > 其它 >Docker入門第五章

Docker入門第五章

常用其他命令


後臺啟動容器

#命令 docker run -d 映象名!
[root@lvshihao /]# docker run -d centos
757173133e8e73985f024dc7e0506f4ac773ce6ba1fbbce18584eda80dbc7603

[root@lvshihao /]# docker ps -a
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS                     PORTS     NAMES
757173133e8e   centos    "/bin/bash"   9 seconds ago   Exited (0) 8 seconds ago             optimistic_banach

#問題docker ps,返現centos已經停止了

#常見的坑,docker容器使用後臺執行,就必須要有一個前臺程序,docker發現沒有應用,就會停止

檢視容器日誌

docker logs -f -t --tail 顯示的條數 容器ID      #發現centos沒有日誌

#自己編寫一段shell指令碼,讓centos在後臺一直每一秒執行著輸出lvshihao的操作,這樣centos就不會立刻掛掉
docker run -d centos /bin/bash -c "while true;do echo lvshihao;sleep 1;done"

[root@lvshihao /]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS     NAMES
3ab34a21e1cc   centos    "/bin/bash -c 'while…"   About a minute ago   Up About a minute             sharp_yonath

#顯示日誌
-tf	#顯示日誌
--tail number #要顯示日誌條數

[root@lvshihao /]# docker logs -tf --tail 3 3ab34a21e1cc
2021-08-14T01:52:57.239363667Z lvshihao
2021-08-14T01:52:58.243463010Z lvshihao
2021-08-14T01:52:59.249260645Z lvshihao

檢視容器中程序資訊

#命令 docker top 容器id
[root@lvshihao /]# docker top 3ab34a21e1cc
UID                 PID                 PPID                C                   STIME               TTY                 TIME                CMD
root                70857               70839               0                   09:49               ?                   00:00:00            /bin/bash -c while true;do echo lvshihao;sleep 1;done
root                76932               70857               0                   09:56               ?                   00:00:00            /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1

檢視映象的元素據

#命令
docker inspect 容器id

#測試
[root@lvshihao /]# docker inspect 3ab34a21e1cc
[
    {
        "Id": "3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32",
        "Created": "2021-08-14T01:49:57.957598671Z",
        "Path": "/bin/bash",
        "Args": [
            "-c",
            "while true;do echo lvshihao;sleep 1;done"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 70857,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2021-08-14T01:49:58.36755625Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55",
        "ResolvConfPath": "/var/lib/docker/containers/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32/hostname",
        "HostsPath": "/var/lib/docker/containers/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32/hosts",
        "LogPath": "/var/lib/docker/containers/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32/3ab34a21e1cce570adf734bb9cea5b1df197bbafee806dd137cdf839c9f5af32-json.log",
        "Name": "/sharp_yonath",
        "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/fd530145a93bd0d4db9acd20db467c650621cc30e5f605bdeb7e282ee380cc14-init/diff:/var/lib/docker/overlay2/3bdf7ab69d622c811093a3a63304f2742982927912dc4299e5178f5318e5f5e8/diff",
                "MergedDir": "/var/lib/docker/overlay2/fd530145a93bd0d4db9acd20db467c650621cc30e5f605bdeb7e282ee380cc14/merged",
                "UpperDir": "/var/lib/docker/overlay2/fd530145a93bd0d4db9acd20db467c650621cc30e5f605bdeb7e282ee380cc14/diff",
                "WorkDir": "/var/lib/docker/overlay2/fd530145a93bd0d4db9acd20db467c650621cc30e5f605bdeb7e282ee380cc14/work"
            },
            "Name": "overlay2"
        },
        "Mounts": [],
        "Config": {
            "Hostname": "3ab34a21e1cc",
            "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 lvshihao;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": "7af8de99d2031e7131411d3fee641b0c0d0b2ecc240603f83a786bedf877894d",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {},
            "SandboxKey": "/var/run/docker/netns/7af8de99d203",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "aa1b52291a367be48b38a3ab5b7615f7a415e88f757e4f82b8bfa096d0a28019",
            "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": "04da5e0f80d775440a2afa1f93fd7a2ddd829ee3c504c9d9e75305865509a305",
                    "EndpointID": "aa1b52291a367be48b38a3ab5b7615f7a415e88f757e4f82b8bfa096d0a28019",
                    "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
                }
            }
        }
    }
]

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

作者:呂世昊 出處:https://www.cnblogs.com/lvshihao/p/15140204.html

個性簽名:學習如逆水行舟,不進則退!

如果覺得這篇文章對你有小小的幫助的話,記得在右下角點個“推薦”哦,博主在此感謝!