Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

docker

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

like image 261
Human Khoo Avatar asked Oct 27 '25 09:10

Human Khoo


1 Answers

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
like image 196
D. Vinson Avatar answered Oct 29 '25 22:10

D. Vinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!