Last active
September 26, 2017 22:57
-
-
Save blakev/015c7c4833d21bd3af1a1f28fd84fed7 to your computer and use it in GitHub Desktop.
Are we running in a docker container? (Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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