Human Khoo
Human Khoo

Reputation: 135

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

Upvotes: 4

Views: 11433

Answers (1)

D. Vinson
D. Vinson

Reputation: 1158

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

Upvotes: 2

Related Questions