Skip to content

Instantly share code, notes, and snippets.

@blakev
Last active Sep 26, 2017
Embed
What would you like to do?
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