从容器内部获取容器的名称 [英] Getting the name of the container from within the container
问题描述
我想从容器中获取docker容器名称。我需要此信息,因为我正在运行Dockerfile中的脚本,该脚本以 ENTRYPOINT
的形式运行,需要知道节点号。我正在将-scale
与 docker-compose
一起使用来创建图像集群。
我能够获得容器的主机名
,即容器ID。但是有没有办法从容器内部获取容器名称?
Docker 远程api 应该适合您。
取决于您的Docker版本使用了适当版本的远程api。检查此以了解更多信息。
我使用了 container-inspect api检索容器名称
。
请按照以下步骤操作:
- 找出Docker守护程序在监听哪个
端口
。在我的情况下,这是默认端口2375
。
$ netstat -anp | grep dockerd
tcp 0 0 ::: 2375 ::: *侦听7 / dockerd
tcp 0 0 :: ffff:172.18.0.110:2375 :: ffff:172.18.0.1:29445已建立7 / dockerd
unix 2 [ACC]流侦听1242188041 7 / dockerd /var/run/docker/metrics.sock
unix 2 [ACC]流侦听1242187193 7 / dockerd /var/run/docker.sock
unix 2 [ACC]流侦听1242186375 7 / dockerd /var/run/docker/libnetwork/79ba9bb412445c757dd4cc2fc30c1cbc7efde8482bdc4c1cf15c4ce0e4190752.sock
unix 3 []流连接12225b $ b Unix 12942b 7 / dockerd
unix 3 []流已连接1242184185 7 / dockerd
- 运行容器并检索容器名称。
$ docker run -it alpine sh
/#apk更新
/#apk添加curl
/#apk add jq
/#curl -s 192.168.0.37:2375/containers/$HOSTNAME/json | jq -r .Name
/ zen_wescoff
注意: 192.168.0.37是我机器的IP。除了容器名称
外,您还可以使用这些docker远程api检索很多信息。
对容器的全部输出进行采样-检查api
/#curl -s 192.168.0.37:2375/containers/$HOSTNAME/json?pretty=true
{
Id: 3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9,
Created: 2019-06-20T09:09:51.258807808Z,
Path: sh,
Args:[],
州:{
状态:正在运行,
正在运行:true,
已暂停:false,
正在重新启动:false,
OOMKilled:false,
Dead:false,
Pid:3824,
ExitCode:0,
错误:,
StartedAt: 2019-06-20T09:09:53.341188978Z,
FinishedAt: 0001-01-01T00:00:00Z
},
Image: sha256:4d90542f0623c71f1f9c11be3da23167174ac9d93731cf91912922e916bab02c,
ResolvConfPath: / var / lib / do cker / containers / 3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9 / resolv.conf,
HostnamePath: / var / lib / docker / containers / 3409974f45bedacd3adbe663679e4eb7a74ccb主机集装箱/ 3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9 /主机,
LOGPATH: /var/lib/docker/containers/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9-json.log,
名称: / jovial_moser,
RestartCount:0,
Driver: overlay2,
Platform: linux,
MountLabel:,
ProcessLabel: ,
AppArmorProfile: docker-default,
ExecIDs:null,
HostConfig:{
Binds:null,
ContainerIDFile:,
LogConfig:{
Type: json-file,
Config:{}
},
NetworkMode:默认,
PortBindings:{},
RestartPolicy:{
Name: no,
MaximumRetryCount:0
},
AutoRemove:否,
VolumeDriver:,
VolumesFrom:空,
CapAdd:空,
CapDrop:空,
Capabilities:空,
Dns:[],
DnsOptions:[],
DnsSearch:[],
ExtraHosts:空,
GroupAdd:空,
IpcMode:私有,
Cgroup:,
链接: null,
OomScoreAdj:0,
PidMode:,
Privileged:false,
PublishAllPorts:false,
ReadonlyRootfs :false,
SecurityOpt:null,
UTSMode:,
UsernsMode:,
ShmSize:67108864,
运行时: runc,
控制台大小:[0,0],
隔离度n:,
CpuShares:0,
内存:0,
NanoCpus:0,
CgroupParent:,
BlkioWeight:0,
BlkioWeightDevice:[],
BlkioDeviceReadBps:空,
BlkioDeviceWriteBps:空,
BlkioDeviceReadIOps:空,
BlkioDeviceWriteIOps:空,
CpuPeriod:0,
CpuQuota:0,
CpuRealtimePeriod:0,
CpuRealtimeRuntime:0,
CpusetCpus:,
CpusetMems:,
Devices:[],
DeviceCgroupRules:null,
DeviceRequests:null ,
DiskQuota:0,
KernelMemory:0,
KernelMemoryTCP:0,
MemoryReservation:0,
MemorySwap:0 ,
MemorySwappiness:空,
OomKillDisable:false,
PidsLimit:空,
Ulimits:空,
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 / a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39-init / diff: / var / lib / docker / overlay2 / edb63c15d70f0a29c60f3725ed952dd76439bef4da8915e66ffe6ed1ecf02107 / diff,
MergedDir: / var / lib / docker / overlay2 / a5f16849169cb7c773f4d204089f8f53daf5D1e4e4e4e1e4e3d3e4e3e3e0e3d0e4e4e3e0e0e0e0e0e0e4e3e0e0e0c0e8c0e0c0e0美国市场/ docker / overlay2 / a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39 / diff,
WorkDir: / var / lib / docker / overlay2 / a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39 / work
},
Name: overlay2
},
Mounts:[],
Config:{
主机名: 3409974f45be,
域名:,
用户:,
AttachStdin:true,
AttachStdout:true ,
AttachStderr:正确,
Tty:正确,
OpenStdin:正确,
StdinOnce:正确,
Env:[ PATH = / usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin],
Cmd:[ sh],
图片:高山,
卷:空,
WorkingDir:,
Entrypoint:空,
OnBuild: null,
标签:{}
},
NetworkSettings:{
Bridge:,
SandboxID: 7f41a5043e99bb110e215ac8e13a8a2c37838b25c6a9fa985584ff458c1e52cc
HairpinMode:false,
LinkLocalIPv6Address :,
LinkLocalIPv6PrefixLen:0,
端口:{},
SandboxKey: / var / run / docker / netns / 7f41a5043e99,
SecondaryIPAddresses:空,
SecondaryIPv6Addresses:空,
EndpointID: 7aa9f269772edff8a9b7f895d51d6dd472a5820ff56a643134465465ab8bff1914e,
Gateway: 172.17.0.1, ,
GlobalIPv6PrefixLen:0,
IPAddress: 172.17.0.2,
IPPrefixLen:16,
IPv6Gateway:,
MacAddress: 02:42:ac:11:00:02,
Networks:{
bridge:{
IPAMConfig:null,
链接:null,
别名:null,
NetworkID: 55987078c24843c830172de5c352b00c96cb3f177a349437dd7bee9bd1814e86,
EndpointID: 7aa9f269772edff8a9b7b6b19bff4e1414ff4a4e4f14d1014e4e4f9e54e4a8e9b6b5b0b0e5e0b0b0b0b0e0b0e6e0b0e0e8e0b0eb8e8b6b6e7b0b0bf8a8bb7b6b4e7b0b0e0b0b0e6e4a8e8bb6e8b6e8b6e8b6b4b5b0b0bb3e7b0b0b0b使用者 17 2.17.0.1,
IPAddress: 172.17.0.2,
IPPrefixLen:16,
IPv6Gateway:,
GlobalIPv6Address: ,
GlobalIPv6PrefixLen:0,
MacAddress: 02:42:ac:11:00:02,
DriverOpts:空
}
}
}
}
总之,您需要了解 ip
以及您的docker守护进程正在侦听的端口
端口,以从容器内检索任何类型的信息。 / p>
希望这会有所帮助,让我知道。
I would like to get the docker container name from within a container. I need this information because I am running a script from the Dockerfile as an ENTRYPOINT
that needs to know the node number. I am using the --scale
with docker-compose
to create a cluster of my image.
I am able to get the hostname
of the container which is the container ID. But is there a way to get the container name from inside the container?
Docker remote apis should work for you.
Depending on the version of your docker used appropriate version of remote api. Check this for more info.
I made use of container-inspect api to retrieve the container name
.
Follow these steps:
- Figure out on which
port
docker daemon is listening. In my case it was2375
which is the default port.
$ netstat -anp | grep dockerd
tcp 0 0 :::2375 :::* LISTEN 7/dockerd
tcp 0 0 ::ffff:172.18.0.110:2375 ::ffff:172.18.0.1:29445 ESTABLISHED 7/dockerd
unix 2 [ ACC ] STREAM LISTENING 1242188041 7/dockerd /var/run/docker/metrics.sock
unix 2 [ ACC ] STREAM LISTENING 1242187193 7/dockerd /var/run/docker.sock
unix 2 [ ACC ] STREAM LISTENING 1242186375 7/dockerd /var/run/docker/libnetwork/79ba9bb412445c757dd4cc2fc30c1cbc7efde8482bdc4c1cf15c4ce0e4190752.sock
unix 3 [ ] STREAM CONNECTED 1242187211 7/dockerd
unix 3 [ ] STREAM CONNECTED 1242183625 7/dockerd
unix 3 [ ] STREAM CONNECTED 1242184185 7/dockerd
- Run a container and retrieve the container name.
$ docker run -it alpine sh
/ # apk update
/ # apk add curl
/ # apk add jq
/ # curl -s 192.168.0.37:2375/containers/$HOSTNAME/json | jq -r ".Name"
/zen_wescoff
NOTE: Here 192.168.0.37 is the ip of my machine. Also apart from container name
you can retrieve lot of information using these docker remote apis.
Sample full output of container-inspect api
/ # curl -s 192.168.0.37:2375/containers/$HOSTNAME/json?pretty=true
{
"Id": "3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9",
"Created": "2019-06-20T09:09:51.258807808Z",
"Path": "sh",
"Args": [],
"State": {
"Status": "running",
"Running": true,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 3824,
"ExitCode": 0,
"Error": "",
"StartedAt": "2019-06-20T09:09:53.341188978Z",
"FinishedAt": "0001-01-01T00:00:00Z"
},
"Image": "sha256:4d90542f0623c71f1f9c11be3da23167174ac9d93731cf91912922e916bab02c",
"ResolvConfPath": "/var/lib/docker/containers/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9/resolv.conf",
"HostnamePath": "/var/lib/docker/containers/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9/hostname",
"HostsPath": "/var/lib/docker/containers/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9/hosts",
"LogPath": "/var/lib/docker/containers/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9/3409974f45bedacd3adbe663679e4eb7a74006483e3a3b04985e5a7cc65229b9-json.log",
"Name": "/jovial_moser",
"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": {},
"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,
"DiskQuota": 0,
"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/a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39-init/diff:/var/lib/docker/overlay2/edb63c15d70f0a29c60f3725ed952dd76439bef4da8915e66ffe6ed1ecf02107/diff",
"MergedDir": "/var/lib/docker/overlay2/a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39/merged",
"UpperDir": "/var/lib/docker/overlay2/a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39/diff",
"WorkDir": "/var/lib/docker/overlay2/a5f16849169cb7c773f4d204089f8e53daf5b648864846ee1d1f076cef9f8a39/work"
},
"Name": "overlay2"
},
"Mounts": [],
"Config": {
"Hostname": "3409974f45be",
"Domainname": "",
"User": "",
"AttachStdin": true,
"AttachStdout": true,
"AttachStderr": true,
"Tty": true,
"OpenStdin": true,
"StdinOnce": true,
"Env": ["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],
"Cmd": ["sh"],
"Image": "alpine",
"Volumes": null,
"WorkingDir": "",
"Entrypoint": null,
"OnBuild": null,
"Labels": {}
},
"NetworkSettings": {
"Bridge": "",
"SandboxID": "7f41a5043e99bb110e215ac8e13a8a2c37838b25c6a9fa985584ff458c1e52cc",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {},
"SandboxKey": "/var/run/docker/netns/7f41a5043e99",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "7aa9f269772edff8a9b7f895d51d6dd472a5820ff56a643134465ab8bff1914e",
"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": "55987078c24843c830172de5c352b00c96cb3f177a349437dd7bee9bd1814e86",
"EndpointID": "7aa9f269772edff8a9b7f895d51d6dd472a5820ff56a643134465ab8bff1914e",
"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
}
}
}
}
So in-short you need to know ip
for your machine and the port
on which docker daemon is listening to retrieve any sort of information from within the container.
Hope this helps, let me know.
这篇关于从容器内部获取容器的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!