Skip to content

Instantly share code, notes, and snippets.

@blakev
Last active September 26, 2017 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blakev/015c7c4833d21bd3af1a1f28fd84fed7 to your computer and use it in GitHub Desktop.
Save blakev/015c7c4833d21bd3af1a1f28fd84fed7 to your computer and use it in GitHub Desktop.
Are we running in a docker container? (Python)
import os
import subprocess
def in_container():
# type: () -> bool
""" Determines if we're running in an lxc/docker container. """
out = subprocess.check_output('cat /proc/1/sched', shell=True)
out = out.decode('utf-8').lower()
checks = [
'docker' in out,
'/lxc/' in out,
out.split()[0] not in ('systemd', 'init',),
os.path.exists('/.dockerenv'),
os.path.exists('/.dockerinit'),
os.getenv('container', None) is not None
]
return any(checks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment