Skip to content

Instantly share code, notes, and snippets.

@hilios
Last active August 25, 2017 02:59
Show Gist options
  • Save hilios/b12b690a627f1f9f6d2b35a395437fe1 to your computer and use it in GitHub Desktop.
Save hilios/b12b690a627f1f9f6d2b35a395437fe1 to your computer and use it in GitHub Desktop.
"""
Get the docker --name from inside a container.
$ pip install docker
"""
import docker
import subprocess
client = docker.from_env()
cgroup = subprocess.check_output(["head", "/proc/1/cgroup"]) or subprocess.check_output(["head", "/proc/1/cpuset"])
container_id = subprocess.check_output(["basename", cgroup]).decode()[:-2]
container = client.containers.get(container_id)
username = container.name
"""
Get the username from the start-singleuser.sh
https://github.com/jupyter/docker-stacks/blob/master/base-notebook/start-singleuser.sh
"""
import os
os.environ.get('JPY_USER')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment