简体   繁体   中英

Python getting Docker Container Name from the inside of a docker container

I need to get the containers name, from within the running container in python

i could easily get the container id from inside the container in python with

bashCommand = """head -1 /proc/self/cgroup|cut -d/ -f3"""
output = subprocess.check_output(['bash','-c', bashCommand])
print output

now i need the containername

Just set the Name at runtime like:

docker run --name MYCOOLCONTAINER alpine:latest

Then:

bashCommandName = `echo $NAME`

output = subprocess.check_output(['bash','-c', bashCommandName]) 

print output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM