Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/cardinal_pythonlib/docker.py : 83%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#!/usr/bin/env python
2# cardinal_pythonlib/docker.py
4"""
5===============================================================================
7 Original code copyright (C) 2009-2021 Rudolf Cardinal (rudolf@pobox.com).
9 This file is part of cardinal_pythonlib.
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
15 https://www.apache.org/licenses/LICENSE-2.0
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
23===============================================================================
25**Support functions for Docker.**
26"""
28import os
31def running_under_docker() -> bool:
32 """
33 Are we running inside a Docker container?
35 As per
36 https://stackoverflow.com/questions/43878953/how-does-one-detect-if-one-is-running-within-a-docker-container-within-python
37 ... but without leaving a file open.
38 """ # noqa
39 # 1. Does /.dockerenv exist?
40 if os.path.exists('/.dockerenv'):
41 return True
42 # 2. Is there a line containing "docker" in /proc/self/cgroup?
43 path = '/proc/self/cgroup'
44 if os.path.isfile(path):
45 with open(path) as f:
46 for line in f:
47 if "docker" in line:
48 return True
49 return False