Skip to content

Commit

Permalink
Fix Docker container ID detection on cgroup v2
Browse files Browse the repository at this point in the history
Use an alternative newer hack for finding the current docker container ID for
the network mode detection (for Airbyte/dbt) containers.

Credit: https://stackoverflow.com/a/71823877
  • Loading branch information
mildbyte committed Jan 31, 2023
1 parent fe859a7 commit 3c9bf1f
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions splitgraph/ingestion/airbyte/docker_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ def detect_network_mode() -> str:
# receiver with net:host. Inside Docker we have to use the host's Docker socket and
# attach the container to our own network so that it can also use our own params.

# Credit: https://stackoverflow.com/a/71823877

# This also applies in case we're running a source against a database that's also running
# in Docker -- we want to mimic sgr too.
if os.path.exists("/.dockerenv"):
with open("/proc/1/cgroup", "r") as f:
match = re.search(r"^.*/docker/([0-9a-f]{64})$", f.read(), re.MULTILINE)
with open("/proc/self/mountinfo", "r") as f:
match = re.search(
r"^.*/containers/([0-9a-f]{64})/.*$",
f.read(),
re.MULTILINE,
)
if not match:
raise AssertionError("Could not detect Docker container ID")
return f"container:{match.group(1)}"
Expand Down

0 comments on commit 3c9bf1f

Please sign in to comment.