Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one detect if one is running within a docker container within Python?

Tags:

python

docker

I was trying to find out if my script was running inside a docker container or not within the python script.

Something like:

if inside_docker():     do_stuff() else:     do_some_other_stuff() 

to do this the only hacky way I found out how to do it is that I can inspect the hostname (with platform.node()) and if the hostname is not the same as my computer then its not in docker (since hostnames docker are some weird hash or something).

Instead I was thinking something a bit more programatic as follows:

  1. first detect docker with cat /proc/1/cgroup
  2. then compare the name those hierarchies with the docker id/hash.

Something like:

from subprocess import call import platform hash = call('cat /proc/1/cgroup') hostname = hostname = platform.node() docker_boolean = does_hostname_contain_docker_hash(hash, hostname) # true or false 

I thought something like that would work but I can't even get to call the cat /proc/1/cgroup without an error. If I am in docker I get the error:

>>> from subprocess import call >>> call('from subprocess import call') Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/lib/python2.7/subprocess.py", line 523, in call     return Popen(*popenargs, **kwargs).wait()   File "/usr/lib/python2.7/subprocess.py", line 711, in __init__     errread, errwrite)   File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child     raise child_exception OSError: [Errno 2] No such file or directory 

any ideas how to fix that?


as a side node I was thinking of making my solution portable but docker is suppose to be portable already so if I am in docker this should always work...

like image 567
Charlie Parker Avatar asked May 09 '17 20:05

Charlie Parker


People also ask

Can you see the processes running inside a container from the outside?

Yes this is normal. LXC containers are not virtualisation of hardware, and so there is a single kernel that is running all of the processes.

How to check if you are inside a docker container?

To check inside a Docker container if you are inside a Docker container or not can be done via /proc/1/cgroup. As this post suggests you can to the following: Outside a docker container all entries in /proc/1/cgroup end on / as you can see here: Inside a Docker container some of the control groups will belong to Docker (or LXC):

How do I know if a process is running in Docker?

Using Control Groups We can identify if a process is running in a docker or LXC container from the control group of the init process: The above command displays the contents of the control group file for the process with PID 1. This is the initialization process for a Linux-based operating system.

What happens when you take a snapshot of a docker container?

The process file will be included in the snapshot, causing the Docker daemon in the new VM to think it’s already running. The status of individual containers is accessed via the docker ps command. This emits a table containing the details of all currently running containers.

Why does my Linux process start with / Docker/LXC?

If some of the lines start with /docker or /lxc, the process is running inside the docker or LXC container. They’ll start with only / if the process runs in a host operating system. The control group is a Linux kernel feature to control or track resource usage for a group of processes.


1 Answers

I think the preferred way to do this is through environment variables. If you're creating your Python app from a Dockerfile, you could specify the 'ENV' directive:

https://docs.docker.com/engine/reference/builder/#env

Dockerfile:

... ENV AM_I_IN_A_DOCKER_CONTAINER Yes 

which could then be read from your app with something like:

python_app.py:

import os  SECRET_KEY = os.environ.get('AM_I_IN_A_DOCKER_CONTAINER', False)  if SECRET_KEY:     print('I am running in a Docker container') 
like image 134
Kyle Truong Avatar answered Oct 02 '22 11:10

Kyle Truong