Are we running in a docker container? (Python)
This file contains 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