runningContainerId function
Returns the Docker container ID of the current process, or null.
Reads /proc/self/cgroup and looks for lines whose cgroup path starts with
/docker/. The 64-character hex string that follows is the container ID.
Returns null if:
/proc/self/cgroupdoes not exist (non-Linux platforms),- the process is not running inside a Docker container, or
- any I/O error occurs.
Implementation
String? runningContainerId() {
final cgroupFile = File('/proc/self/cgroup');
if (!cgroupFile.existsSync()) {
return null;
}
for (final line in cgroupFile.readAsLinesSync()) {
final path = line.split(':').last;
if (path.startsWith('/docker/')) {
return path.substring('/docker/'.length);
}
}
return null;
}