From fafedcae46eecb49523f2441436a2fc4ca37f876 Mon Sep 17 00:00:00 2001 From: remleduff Date: Tue, 31 Oct 2023 10:06:05 -0400 Subject: [PATCH] Update target-chrome-docker's runningInDocker check for cgroupsv2 (#478) * Update target-chrome-docker's runningInDocker check for cgroupsv2 This fixes docker-in-docker for me, particularly in gitlab. Without this, it tries to connect to 127.0.0.1 rather than the container's correct IP address and fails with a timeout. * Fix formatting --------- Co-authored-by: Joel 'Aaron' Cohen Co-authored-by: Joel Arvidsson --- packages/target-chrome-docker/src/get-network-host.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/target-chrome-docker/src/get-network-host.js b/packages/target-chrome-docker/src/get-network-host.js index 2c7221ed..4fcffbbf 100644 --- a/packages/target-chrome-docker/src/get-network-host.js +++ b/packages/target-chrome-docker/src/get-network-host.js @@ -3,10 +3,12 @@ const fs = require('fs-extra'); const getNetworkHost = async (execute, dockerId) => { let host = '127.0.0.1'; - // https://tuhrig.de/how-to-know-you-are-inside-a-docker-container/ + // https://stackoverflow.com/questions/68816329/how-to-get-docker-container-id-from-within-the-container-with-cgroup-v2 const runningInsideDocker = - fs.existsSync('/proc/1/cgroup') && - /docker/.test(fs.readFileSync('/proc/1/cgroup', 'utf8')); + fs.existsSync('/proc/self/mountinfo') && + /\/docker\/containers\//.test( + fs.readFileSync('/proc/self/mountinfo', 'utf8') + ); // If we are running inside a docker container, our spawned docker chrome instance will be a sibling on the default // bridge, which means we can talk directly to it via its IP address.