溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何查看Docker容器的信息

發(fā)布時間:2022-03-21 09:08:57 來源:億速云 閱讀:704 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細介紹“如何查看Docker容器的信息”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當(dāng),希望這篇“如何查看Docker容器的信息”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

創(chuàng)建tomcat容器

在docker環(huán)境下執(zhí)行如下命令,即可創(chuàng)建一個名為tomcat001的tomcat容器:

docker run --name tomcat001 -idt tomcat

執(zhí)行docker ps檢查一下,容器創(chuàng)建成功:

root@maven:~# docker ps
CONTAINER ID	IMAGE	COMMAND				CREATED			STATUS			PORTS		NAMES
98d119ed4c73	tomcat	"catalina.sh run"	10 minutes ago	Up 10 minutes	8080/tcp	tomcat001

獲取信息常用的方式有如下三種:

  • 進入容器內(nèi)部獲取信息;

  • 執(zhí)行docker exec命令;

  • 執(zhí)行docker inspect -f命令(推薦方式);

第一種:進入容器內(nèi)部獲取信息

執(zhí)行以下命令可進入容器內(nèi)部:

docker exec -it tomcat001 /bin/bash

此時已進入容器內(nèi)部,所以分別執(zhí)行hostname、ip addr、env這三個命令可以獲取相關(guān)信息,如下:

root@98d119ed4c73:/usr/local/tomcat# hostname
98d119ed4c73
root@98d119ed4c73:/usr/local/tomcat# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
17: eth0@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.5/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:5/64 scope link 
       valid_lft forever preferred_lft forever
root@98d119ed4c73:/usr/local/tomcat# env
LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib
CATALINA_HOME=/usr/local/tomcat
LANG=C.UTF-8
HOSTNAME=98d119ed4c73
OPENSSL_VERSION=1.1.0f-3+deb9u1
TOMCAT_VERSION=8.5.24
GPG_KEYS=05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23
JAVA_HOME=/docker-java-home/jre
TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib
JAVA_VERSION=8u151
PWD=/usr/local/tomcat
HOME=/root
CA_CERTIFICATES_JAVA_VERSION=20170531+nmu1
TOMCAT_MAJOR=8
JAVA_DEBIAN_VERSION=8u151-b12-1~deb9u1
TOMCAT_SHA1=9e26ba61c5665aafc849073edeb769be555283cd
TERM=xterm
SHLVL=1
TOMCAT_ASC_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc      https://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc    https://www.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc       https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc
PATH=/usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TOMCAT_TGZ_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz  https://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz        https://www.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz   https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz
_=/usr/bin/env

第二種:執(zhí)行docker exec命令

不用進入容器,直接在docker環(huán)境執(zhí)行docker exec 容器名 容器內(nèi)命令的方式也可以獲取相關(guān)信息,舉例如下:

  • 獲取容器的hostname:docker exec tomcat001 hostname

  • 獲取容器ip地址:docker exec tomcat001 ip addr

  • 獲取容器環(huán)境變量:docker exec tomcat001 env

執(zhí)行結(jié)果如下所示:

root@maven:~# docker exec tomcat001 hostname
98d119ed4c73
root@maven:~# docker exec tomcat001 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
17: eth0@if18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:05 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.5/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:5/64 scope link 
       valid_lft forever preferred_lft forever
root@maven:~# docker exec tomcat001 env
PATH=/usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=98d119ed4c73
LANG=C.UTF-8
JAVA_HOME=/docker-java-home/jre
JAVA_VERSION=8u151
JAVA_DEBIAN_VERSION=8u151-b12-1~deb9u1
CA_CERTIFICATES_JAVA_VERSION=20170531+nmu1
CATALINA_HOME=/usr/local/tomcat
TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib
LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib
OPENSSL_VERSION=1.1.0f-3+deb9u1
GPG_KEYS=05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23
TOMCAT_MAJOR=8
TOMCAT_VERSION=8.5.24
TOMCAT_SHA1=9e26ba61c5665aafc849073edeb769be555283cd
TOMCAT_TGZ_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz  https://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz        https://www.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz   https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz
TOMCAT_ASC_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc      https://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc    https://www.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc       https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc
HOME=/root

第三種:使用docker inspect命令

推薦使用docker inspect來獲取信息,這個命令會返回一個json字符串,里面以key-value的格式準(zhǔn)備了該容器相關(guān)的信息,內(nèi)容十分豐富,可以一次性取得全部內(nèi)容,也可以返回指定key對應(yīng)的信息;

我們先來看看所有內(nèi)容,執(zhí)行命令docker inspect tomcat001,可以看到如下信息:

root@maven:~# docker inspect tomcat001
[
    {
        "Id": "98d119ed4c73c750e53353e77f7c6942803809a9d5dedc28d8b0f41ce58b8b17",
        "Created": "2018-05-06T07:39:45.464492435Z",
        "Path": "catalina.sh",
        "Args": [
            "run"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            "Dead": false,
            "Pid": 22468,
            "ExitCode": 0,
            "Error": "",
            "StartedAt": "2018-05-06T07:39:46.143092505Z",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "sha256:3dcfe809147daf54fbdbe1f6886b4c49df63f28e2b3c8ac119fef6a7f7490d3c",
        "ResolvConfPath": "/var/lib/docker/containers/98d119ed4c73c750e53353e77f7c6942803809a9d5dedc28d8b0f41ce58b8b17/resolv.conf",
        "HostnamePath": "/var/lib/docker/containers/98d119ed4c73c750e53353e77f7c6942803809a9d5dedc28d8b0f41ce58b8b17/hostname",
        "HostsPath": "/var/lib/docker/containers/98d119ed4c73c750e53353e77f7c6942803809a9d5dedc28d8b0f41ce58b8b17/hosts",
        "LogPath": "/var/lib/docker/containers/98d119ed4c73c750e53353e77f7c6942803809a9d5dedc28d8b0f41ce58b8b17/98d119ed4c73c750e53353e77f7c6942803809a9d5dedc28d8b0f41ce58b8b17-json.log",
        "Name": "/tomcat001",
        "RestartCount": 0,
        "Driver": "aufs",
        "MountLabel": "",
        "ProcessLabel": "",
        "AppArmorProfile": "",
        "ExecIDs": [
            "cffec99f38514024f6a6e31e0e160d37d295041e4813a2245466be2dbc9c4037"
        ],
        "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,
            "Dns": [],
            "DnsOptions": [],
            "DnsSearch": [],
            "ExtraHosts": null,
            "GroupAdd": null,
            "IpcMode": "",
            "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": null,
            "BlkioDeviceReadBps": null,
            "BlkioDeviceWriteBps": null,
            "BlkioDeviceReadIOps": null,
            "BlkioDeviceWriteIOps": null,
            "CpuPeriod": 0,
            "CpuQuota": 0,
            "CpuRealtimePeriod": 0,
            "CpuRealtimeRuntime": 0,
            "CpusetCpus": "",
            "CpusetMems": "",
            "Devices": [],
            "DiskQuota": 0,
            "KernelMemory": 0,
            "MemoryReservation": 0,
            "MemorySwap": 0,
            "MemorySwappiness": -1,
            "OomKillDisable": false,
            "PidsLimit": 0,
            "Ulimits": null,
            "CpuCount": 0,
            "CpuPercent": 0,
            "IOMaximumIOps": 0,
            "IOMaximumBandwidth": 0
        },
        "GraphDriver": {
            "Name": "aufs",
            "Data": null
        },
        "Mounts": [],
        "Config": {
            "Hostname": "98d119ed4c73",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "8080/tcp": {}
            },
            "Tty": true,
            "OpenStdin": true,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "LANG=C.UTF-8",
                "JAVA_HOME=/docker-java-home/jre",
                "JAVA_VERSION=8u151",
                "JAVA_DEBIAN_VERSION=8u151-b12-1~deb9u1",
                "CA_CERTIFICATES_JAVA_VERSION=20170531+nmu1",
                "CATALINA_HOME=/usr/local/tomcat",
                "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib",
                "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib",
                "OPENSSL_VERSION=1.1.0f-3+deb9u1",
                "GPG_KEYS=05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23",
                "TOMCAT_MAJOR=8",
                "TOMCAT_VERSION=8.5.24",
                "TOMCAT_SHA1=9e26ba61c5665aafc849073edeb769be555283cd",
                "TOMCAT_TGZ_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz \thttps://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz \thttps://www.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz \thttps://archive.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz",
                "TOMCAT_ASC_URLS=https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc \thttps://www-us.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc \thttps://www.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc \thttps://archive.apache.org/dist/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz.asc"
            ],
            "Cmd": [
                "catalina.sh",
                "run"
            ],
            "ArgsEscaped": true,
            "Image": "tomcat",
            "Volumes": null,
            "WorkingDir": "/usr/local/tomcat",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {}
        },
        "NetworkSettings": {
            "Bridge": "",
            "SandboxID": "50244517221e6faff4217ff5453919f0c297b10434812ecfbf3decb3ce9860f6",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "8080/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/50244517221e",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "e9336feece03b1c87e6724253575ee50963e9bdc1f9618609fe641a8a939b87a",
            "Gateway": "172.17.0.1",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "172.17.0.5",
            "IPPrefixLen": 16,
            "IPv6Gateway": "",
            "MacAddress": "02:42:ac:11:00:05",
            "Networks": {
                "bridge": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": null,
                    "NetworkID": "87eb4d08d408c08489db4460915aabed59210d04f8193364420d3719cd308d83",
                    "EndpointID": "e9336feece03b1c87e6724253575ee50963e9bdc1f9618609fe641a8a939b87a",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.5",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:11:00:05"
                }
            }
        }
    }
]

可見內(nèi)容十分豐富;

如果我們只對其中的一些感興趣,例如hostname、ip地址、環(huán)境變量,該如何查看呢?

hostname:在返回值中,hostname的位置如下圖紅框所示,

如何查看Docker容器的信息

由上圖可見,hostname位于Config.Hostname這個節(jié)點位置,所以執(zhí)行命令docker inspect -f {{.Config.Hostname}} tomcat001即可獲取到hostname,如下所示:

root@maven:~# docker inspect -f {{.Config.Hostname}} tomcat001
98d119ed4c73

ip地址:執(zhí)行命令docker inspect -f {{.NetworkSettings.IPAddress}} tomcat001即可獲取到ip地址:

root@maven:~# docker inspect -f {{.NetworkSettings.IPAddress}} tomcat001
172.17.0.5

環(huán)境變量:從完整的inspect信息中可以看到環(huán)境變量Config.Env的內(nèi)容是個json數(shù)組,所以除了用docker inspect -f {{.Config.Env}} tomcat001獲取所有環(huán)境變量信息,還可以用docker inspect -f &lsquo;{{index .Config.Env 1}}&rsquo; tomcat001命令獲取數(shù)組中的某一條記錄,如下:

root@maven:~# docker inspect -f '{{index .Config.Env 1}}' tomcat001
LANG=C.UTF-8
root@maven:~# docker inspect -f '{{index .Config.Env 2}}' tomcat001 
JAVA_HOME=/docker-java-home/jre

讀到這里,這篇“如何查看Docker容器的信息”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI