Docker 使用详解看了挺开悟的

使用docker ps命令可以查看所有正在运行中的容器列表,

使用docker inspect命令我们可以查看更详细的关于某一个容器的信息。

$ docker inspect  容器id/image
[{"Id": "b57ee6bbf1f4f62a5aba6a73acd53b0f9b8ec542e1f9fa9213159ffd3828c7b4","Created": "2023-12-14T12:27:44.062757726Z","Path": "python","Args": ["app.py"],"State": {"Status": "running","Running": true,"Paused": false,"Restarting": false,"OOMKilled": false,"Dead": false,"Pid": 3019869,"ExitCode": 0,"Error": "","StartedAt": "2023-12-14T12:27:44.285873292Z","FinishedAt": "0001-01-01T00:00:00Z"},"Image": "sha256:68835f4eea5fe4cb40ea826970c1eff0b2aae33bfd4452aef7678325151665ce","ResolvConfPath": "/var/lib/docker/containers/b57ee6bbf1f4f62a5aba6a73acd53b0f9b8ec542e1f9fa9213159ffd3828c7b4/resolv.conf","HostnamePath": "/var/lib/docker/containers/b57ee6bbf1f4f62a5aba6a73acd53b0f9b8ec542e1f9fa9213159ffd3828c7b4/hostname","HostsPath": "/var/lib/docker/containers/b57ee6bbf1f4f62a5aba6a73acd53b0f9b8ec542e1f9fa9213159ffd3828c7b4/hosts","LogPath": "/var/lib/docker/containers/b57ee6bbf1f4f62a5aba6a73acd53b0f9b8ec542e1f9fa9213159ffd3828c7b4/b57ee6bbf1f4f62a5aba6a73acd53b0f9b8ec542e1f9fa9213159ffd3828c7b4-json.log","Name": "/chandao","RestartCount": 0,"Driver": "overlay2","Platform": "linux","MountLabel": "","ProcessLabel": "","AppArmorProfile": "docker-default","ExecIDs": null,"HostConfig": {"Binds": null,"ContainerIDFile": "","LogConfig": {"Type": "json-file","Config": {}},"NetworkMode": "default","PortBindings": {"5000/tcp": [{"HostIp": "","HostPort": "5000"}]},"RestartPolicy": {"Name": "no","MaximumRetryCount": 0},"AutoRemove": false,"VolumeDriver": "","VolumesFrom": null,"CapAdd": null,"CapDrop": null,"CgroupnsMode": "private","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": null,"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/992b402f831d5dfd9c32ce0f03578e216af97457a027fde5777195efd7e23ca3-init/diff:/var/lib/docker/overlay2/6b02b27b1984eef979d37b1e8ab160944de9d5df92c6ca68ebec437e51a9a7ed/diff:/var/lib/docker/overlay2/70f28063357fd702541aa0ce1fcd38eb3ad76545a4faafbc866396b429462f6d/diff:/var/lib/docker/overlay2/bcc3578b8f52fe217de9db1f0153c97cc9bd1791612cb7c3d558fafc473390bb/diff:/var/lib/docker/overlay2/ae1dd88de31045abf0691ad9c14c8af301c66df3ddf93db1ff67b29fc65432af/diff:/var/lib/docker/overlay2/15f57c56eb051a7342f9a0cfafcf7ba60f7727938d83ba1f07794fb0e87e8183/diff:/var/lib/docker/overlay2/87865dd872c3989c33f13dd8cb67d08056b66618c0b75db92b4587d93cdee97b/diff:/var/lib/docker/overlay2/ac12d1cdaa9764b08f53441ce1d6633f0eaa308ffcce98dacfefeaf2d38576a8/diff:/var/lib/docker/overlay2/a26453e5107d214eb51b55603b80db575972402419d7489377278e2329c65ae6/diff:/var/lib/docker/overlay2/9a6c984800d13b9756ec782229e56c3fb3f563d212b7298bd07e2ee0b1775e9d/diff:/var/lib/docker/overlay2/acc4dc4a229e16e16f6231201f5f69cd7478f4d934b0fe07ec94b2201cb33210/diff:/var/lib/docker/overlay2/871b52f81ee7add1d576851994444dadfdb1ac98a9214b15401020a500849490/diff","MergedDir": "/var/lib/docker/overlay2/992b402f831d5dfd9c32ce0f03578e216af97457a027fde5777195efd7e23ca3/merged","UpperDir": "/var/lib/docker/overlay2/992b402f831d5dfd9c32ce0f03578e216af97457a027fde5777195efd7e23ca3/diff","WorkDir": "/var/lib/docker/overlay2/992b402f831d5dfd9c32ce0f03578e216af97457a027fde5777195efd7e23ca3/work"},"Name": "overlay2"},"Mounts": [],"Config": {"Hostname": "b57ee6bbf1f4","Domainname": "","User": "","AttachStdin": false,"AttachStdout": false,"AttachStderr": false,"ExposedPorts": {"5000/tcp": {}},"Tty": false,"OpenStdin": false,"StdinOnce": false,"Env": ["PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin","LANG=C.UTF-8","GPG_KEY=E3FF2839C048B25C084DEBE9B26995E310250568","PYTHON_VERSION=3.9.17","PYTHON_PIP_VERSION=23.0.1","PYTHON_SETUPTOOLS_VERSION=58.1.0","PYTHON_GET_PIP_URL=https://github.com/pypa/get-pip/raw/9af82b715db434abb94a0a6f3569f43e72157346/public/get-pip.py","PYTHON_GET_PIP_SHA256=45a2bb8bf2bb5eff16fdd00faef6f29731831c7c59bd9fc2bf1f3bed511ff1fe"],"Cmd": ["python","app.py"],"Image": "chandao:1.0","Volumes": null,"WorkingDir": "/app","Entrypoint": null,"OnBuild": null,"Labels": {}},"NetworkSettings": {"Bridge": "","SandboxID": "cd049cf355c7be06729b89d4a1ccd239bb0c5ef58e33310a8fa37e1a577381f1","HairpinMode": false,"LinkLocalIPv6Address": "","LinkLocalIPv6PrefixLen": 0,"Ports": {"5000/tcp": [{"HostIp": "0.0.0.0","HostPort": "5000"},{"HostIp": "::","HostPort": "5000"}]},"SandboxKey": "/var/run/docker/netns/cd049cf355c7","SecondaryIPAddresses": null,"SecondaryIPv6Addresses": null,"EndpointID": "f2aaf1698c71664de4ea184efe526911851caea6e9d65471f98f6fe0a4a41cc3","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": "e5d160a327c5d8820421c5b0e5543075406a3327b1c29c36be14294a8c7424d0","EndpointID": "f2aaf1698c71664de4ea184efe526911851caea6e9d65471f98f6fe0a4a41cc3","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}}}}
]

查看Docker容器的信息

对于运行在Docker环境的容器,有时我们想查询它们的一些基本信息,例如环境变量、hostname、ip地址等,接下来我们以一个tomcat容器为例,看看有哪些方式来取得这些信息;

创建tomcat容器

docker环境下执行如下命令,即可创建一个名为tomcat001tomcat容器:

docker run --name tomcat001 -idt tomcat

执行docker ps检查一下,容器创建成功:

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

获取信息常用的方式有如下三种:

  1. 进入容器内部获取信息;
  2. 执行docker exec命令;
  3. 执行docker inspect -f命令(推荐方式);

第一种:进入容器内部获取信息

执行以下命令可进入容器内部:

docker exec -it tomcat001 /bin/bash

此时已进入容器内部,所以分别执行hostname、ip addr、env这三个命令可以获取相关信息,如下:

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 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::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 0inet 172.17.0.5/16 scope global eth0valid_lft forever preferred_lft foreverinet6 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

第二种:执行docker exec命令

不用进入容器,直接在docker环境执行docker exec 容器名 容器内命令的方式也可以获取相关信息,举例如下:

  1. 获取容器的hostname:docker exec tomcat001 hostname
  2. 获取容器ip地址:docker exec tomcat001 ip addr
  3. 获取容器环境变量:docker exec tomcat001 env

执行结果如下所示:

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 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::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 0inet 172.17.0.5/16 scope global eth0valid_lft forever preferred_lft foreverinet6 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的格式准备了该容器相关的信息,内容十分丰富,可以一次性取得全部内容,也可以返回指定key对应的信息;

我们先来看看所有内容,执行命令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"}}}}
]

可见内容十分丰富;

如果我们只对其中的一些感兴趣,例如hostname、ip地址、环境变量,该如何查看呢?

  1. hostname:在返回值中,hostname的位置如下图红框所示,

在这里插入图片描述

由上图可见,hostname位于Config.Hostname这个节点位置,所以执行命令docker inspect -f {{.Config.Hostname}} tomcat001即可获取到hostname,如下所示:

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

ip地址:执行命令docker inspect -f {{.NetworkSettings.IPAddress}} tomcat001即可获取到ip地址:

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

环境变量:从完整的inspect信息中可以看到环境变量Config.Env的内容是个json数组,所以除了用docker inspect -f {{.Config.Env}} tomcat001获取所有环境变量信息,还可以用docker inspect -f ‘{{index .Config.Env 1}}’ tomcat001命令获取数组中的某一条记录,如下:

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

至此,三种获取容器信息的方式已经全部列出,之所以在本文中推荐第三种,是因为有时我们会在shell脚本中获取容器信息,这时候用这种方式最为简单;


Docker 容器启动 查看容器状态 - 四

1、容器两种方式进行启动

一种是基于创建一个容器并启动

docker create 
docker start 

另一种 使用 run 创建自动启动;
是状态下的停止 启动

docker start nginx1
docker stop NAMES     # 停止
docker restart NAMES  # 启动
# service docker start# docker run - 运行(创建)一个容器 
docker run -d -p 0.0.0.0:18080:8080 --name nginx1 nginx
-d: 让容器在后台运行
-P: 将容器内部使用的网络端口映射到我们使用的主机上
-p: 指定端口
--name :设置容器命名
nginx 是容器镜像
docker pull nginx  拉取镜像;

2、docker 进入 容器系统;

进入一个 nginx1 容器

docker exec -it nginx1 /bin/bash
-d : 分离模式: 在后台运行
-i : 交互模式 即使没有附加也保持STDIN 打开
-t : 分配一个伪终端
/bin/bash : 运行命令 bash shell

另一种进入方式:

docker attach centos6.3

3、 批量操作容器

docker 启动所有的容器
docker start $(docker ps -a | awk '{ print $1}' | tail -n +2)docker 关闭所有的容器
docker stop $(docker ps -a | awk '{ print $1}' | tail -n +2)docker 删除所有的容器
docker rm $(docker ps -a | awk '{ print $1}' | tail -n +2)docker 删除所有的镜像
docker rmi $(docker images | awk '{print $3}' |tail -n +2)

如何查看docker容器运行状态

如何查看docker容器运行状态?有时候,我们需要进入运行的docker容器,查看容器运行情况,查看 docker 容器的状态信息。

查看容器状态:

docker stats nginx1

查看进程信息:

docker top nginx1

使用 docker inspect 来查看Docker的底层信息。它会返回一个 JSON 文件记录着 Docker 容器的配置和状态信息

docker 容器默认的目录:

/var/lib/docker/containers

目录下 12 位开头的 就是 容器ID
inspect 内容就是 config.v2.json 文件

cat/var/lib/docker/containers/23defb07e362b81fa9d282382dfb5101e7a269f97b3d167493a5b1e031d15120/config.v2.json

inspect 查看容器的配置信息,包含容器名、环境变量、运行命令、主机配置、网络配置和数据卷配置

docker inspect nginx1 查看容器所有状态;

root@rbtnode1: ~# docker inspect --format='{{.NetworkSettings.IPAddress}}' nginx1
172.16.57.5root@rbtnode1: ~# docker inspect --format '{{.Name}} {{.State.Running}}' nginx1
/nginx1 true

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/226525.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[Angular] 笔记 8:list/detail 页面以及@Input

1. list 页面 list/detail 是重要的 UI 设计模式。 vscode terminal 运行如下命令生成 detail 组件&#xff1a; PS D:\Angular\my-app> ng generate component pokemon-base/pokemon-detail --modulepokemon-base/pokemon-base.module.ts CREATE src/app/pokemon-base/p…

Mybatis行为配置之Ⅲ—其他行为配置项说明

专栏精选 引入Mybatis Mybatis的快速入门 Mybatis的增删改查扩展功能说明 mapper映射的参数和结果 Mybatis复杂类型的结果映射 Mybatis基于注解的结果映射 Mybatis枚举类型处理和类型处理器 再谈动态SQL Mybatis配置入门 Mybatis行为配置之Ⅰ—缓存 Mybatis行为配置…

ChatGPT在地学、GIS、气象、农业、生态、环境等领域中的高级应用

以ChatGPT、LLaMA、Gemini、DALLE、Midjourney、Stable Diffusion、星火大模型、文心一言、千问为代表AI大语言模型带来了新一波人工智能浪潮&#xff0c;可以面向科研选题、思维导图、数据清洗、统计分析、高级编程、代码调试、算法学习、论文检索、写作、翻译、润色、文献辅助…

华为云服务器yum无更新问题解决

买了一个华为云服务器&#xff0c;系统是银河麒麟v10&#xff0c;cpu选择的事华为的坤鹏。 买了一段时间后&#xff0c;使用yum更新&#xff0c;发现没有任何更新包。 又过了一段时间&#xff0c;还是没有更新包。 通过漏扫设备&#xff0c;发现系统内存在较多存在漏洞的软件…

tsconfig.app.json文件报红:Option ‘importsNotUsedAsValues‘ is deprecated...

在创建vue3 vite ts项目时的 tsconfig.json&#xff08;或者tsconfig.app.json&#xff09; 配置文件经常会报一个这样的错误&#xff1a; 爆红&#xff1a; Option ‘importsNotUsedAsValues’ is deprecated and will stop functioning in TypeScript 5.5. Specify compi…

gin框架使用系列之四——json和protobuf的渲染

系列目录 《gin框架使用系列之一——快速启动和url分组》《gin框架使用系列之二——uri占位符和占位符变量的获取》《gin框架使用系列之三——获取表单数据》 上篇我们介绍了如何获取数据&#xff0c;本篇我们介绍一下如何返回固定格式的数据。 一、返回JSON数据 在web开发中…

大创项目推荐 深度学习OCR中文识别 - opencv python

文章目录 0 前言1 课题背景2 实现效果3 文本区域检测网络-CTPN4 文本识别网络-CRNN5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 &#x1f6a9; **基于深度学习OCR中文识别系统 ** 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff0c;…

红队打靶练习:DIGITALWORLD.LOCAL: FALL

目录 信息收集 1、arp 2、netdiscover 3、nmap 4、nikto 5、whatweb 6、小结 目录探测 1、gobuster 2、dirsearch WEB 80端口 /test.php 文件包含漏洞 SSH登录 提权 get root and flag 信息收集 1、arp ┌──(root㉿ru)-[~/kali] └─# arp-scan -l Interfa…

Qt/C++音视频开发61-多屏渲染/一个解码渲染到多个窗口/画面实时同步

一、前言 多屏渲染就是一个解码线程对应多个渲染界面&#xff0c;通过addrender这种方式添加多个绘制窗体&#xff0c;我们经常可以在展会或者卖电视机的地方可以看到很多电视播放的同一个画面&#xff0c;原理应该类似&#xff0c;一个地方负责打开解码播放&#xff0c;将画面…

Android下载gradle失败解决方法

1、在gradle-wrapper.properties文件中查看自己需要下载gradle什么版本的包和zip路径&#xff08;wrapper/dists&#xff09;。 2、在setting中查看Gradle的保存路径&#xff0c;如下图&#xff1a;C:/Users/Administrator/.gradle&#xff0c;加上第一步的zip路径得到下载grad…

SQL Server 存储过程 触发器 事务处理

CSDN 成就一亿技术人&#xff01; 难度指数&#xff1a;* * CSDN 成就一亿技术人&#xff01; 目录 1. 存储过程的作用 创建存储过程 2. 触发器 触发器的种类 insert触发器 update触发器 delete触发器 测试 3. 事务 开始事务 提交事务 回滚事务 举个实例 在 SQ…

分享好用的chatgpt

1.在vscode中&#xff0c;点击这个&#xff1a; 2.搜索&#xff1a;ChatGPT - 中文版&#xff0c;个人觉得这个更好用&#xff1a; 3.下载完成之后&#xff0c;左侧会多出来这个&#xff1a; 点击这个图标就能进入chatgpt界面了 4.如果想使用tizi访问国外的chatgpt&#xf…

.Net FrameWork总结

.Net FrameWork总结 介绍.Net公共语言运行库CLI的组成.NET Framework的主要组成.NET Framework的优点CLR在运行期管理程序的执行&#xff0c;包括以下内容CLR提供的服务FCL的组成 或 服务&#xff08;这个其实就是我们编码时常用到的类库&#xff09;&#xff1a;&#xff08;下…

使用vue3实现echarts漏斗图表以及实现echarts全屏放大效果

1.首先安装echarts 安装命令&#xff1a;npm install echarts --save 2.页面引入 echarts import * as echarts from echarts; 3.代码 <template> <div id"main" :style"{ width: 400px, height: 500px }"></div> </template> …

认识数据的规范化

关系模型满足的确定约束条件称为范式&#xff0c;根据满足约束条件的级别不同&#xff0c;范式由低到高分为 1NF&#xff08;第一范式&#xff09;、2NF&#xff08;第二范式&#xff09;、3NF&#xff08;第三范式&#xff09;、BCNF&#xff08;BC 范式&#xff09;、4NF&…

Vue使用Element table表格格式化GMT时间为Shanghai时间

Vue使用Element表格格式化GMT时间为Shanghai时间 说明 阿里巴巴java开发规范规定&#xff0c;数据库必备gmt_create、gmt_modified字段&#xff0c;使用的是GMT时间&#xff0c;在中国使用必然要转换我中国时间。 在阿里巴巴的Java开发规范中&#xff0c;要求每个表都必备三…

T-Dongle-S3开发笔记——创建工程

创建Hello world工程 打开命令面板 方法1&#xff1a;查看->命令面板 方法2&#xff1a;按F1 选择ESP-IDF:展示示例项目 创建helloworld 选择串口 选择芯片 至此可以编译下载运行了 运行后打印的信息显示flash只有2M。但是板子上电flash是W25Q32 4MB的吗 16M-bit

B3842 起动电流小,工作频率 可达500kHz的Dc-Dc开关电源芯片

B3842/43/44是专为脱线和Dc-Dc开关电源应用设计的恒频电流型Pwd控制器内部包含温度补偿精密基准、供精密占空比调节用的可调振荡器、高增益混放大器、电流传感比较器和适合作功率MOST驱动用的大电流推挽输出颇以及单周期徊滞式限流欠压锁定、死区可调、单脉冲计数拴锁等保护电路…

BDD - Python Behave 配置文件 behave.ini

BDD - Python Behave 配置文件 behave.ini 引言behave.ini配置参数的类型配置项 behave.ini 应用feature 文件step 文件创建 behave.ini执行 Behave查看配置默认值 behave -v 引言 前面文章 《BDD - Python Behave Runner Script》就是为了每次执行 Behave 时不用手动敲一长串…

磁盘管理 :逻辑卷、磁盘配额

一 LVM可操作的对象&#xff1a;①完成的磁盘 ②完整的分区 PV 物理卷 VG 卷组 LV 逻辑卷 二 LVM逻辑卷管理的命令 三 建立LVM逻辑卷管理 虚拟设置-->一致下一步就行-->确认 echo "- - -" > /sys/class/scsi_host/host0/scan;echo "- -…