Skip to content

Instantly share code, notes, and snippets.

@anantkamath
Created October 30, 2016 17:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anantkamath/623ce7f5432680749e087cf8cfba9b69 to your computer and use it in GitHub Desktop.
Save anantkamath/623ce7f5432680749e087cf8cfba9b69 to your computer and use it in GitHub Desktop.
Check if you're running inside a Docker container in Python
def runningInDocker():
with open('/proc/self/cgroup', 'r') as procfile:
for line in procfile:
fields = line.strip().split('/')
if fields[1] == 'docker':
return True
return False
@spacether
Copy link

spacether commented Jan 10, 2018

Sometimes fields[1] is reported as 'docker-ce' and fields=['13:name=systemd:', 'docker-ce', 'docker', 'blah']
Should we instead use:

            if 'docker' in fields:
                return True

@scorpiodawg
Copy link

Does this work on Windows? Don't have a Windows machine handy to test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment