Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker gets the container. How does id know the container name?

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Docker gets the container id how to know the container name. In view of this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Sometimes users want to get the docker container name from the container, such as the cluster that is using-scale with docker-compose to create images, they can get the hostname of the container, that is, container ID, so how to get the container name from inside the container.

Docker remote apis should help you to use the appropriate version of remote api depending on your version of Docker.

Use container-inspect API to retrieve container name

1. Determine the port on which the docker daemon listens. This is the default port 2375.

$netstat-anp | grep dockerdtcp 0: 2375:: * LISTEN 7/dockerdtcp 0 0:: ffff:172.18.0.110:2375:: ffff:172.18.0.1:29445 ESTABLISHED 7/dockerdunix 2 [ACC] STREAM LISTENING 1242188041 7/dockerd / var/run/docker/metrics.sockunix 2 [ACC] STREAM LISTENING 1242187193 7/dockerd / var/run/docker.sockunix 2 [ACC] STREAM LISTENING 1242186375 7/dockerd / var/run/docker/libnetwork/79ba9bb412445c757dd4cc2fc30c1cbc7efde8482bdc4c1cf15c4ce0e4190752.sockunix 3 [] STREAM CONNECTED 1242187211 7 / dockerdunix 3 [] STREAM CONNECTED 1242183625 7/dockerdunix 3 [] STREAM CONNECTED 1242184185 7/dockerd

2. Run the 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: 192.168.0.37 is the IP of this machine. In addition, in addition to the container name, you can use these docker remote api to retrieve a lot of information

After that, the container checks the complete output of the example of API

In short, you need to know which port your machine's ip and docker daemon is listening on to retrieve any type of information from the container.

This is the answer to the question about how to know the container name of the container id obtained by docker. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report