Skip to content

probe for root dir instead of guessing #1588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/docker/shared.rs
Original file line number Diff line number Diff line change
@@ -1349,22 +1349,26 @@
Ok(image)
}

fn docker_inspect_self_mountinfo(engine: &Engine, msg_info: &mut MessageInfo) -> Result<String> {
fn docker_find_self_mountinfo(engine: &Engine, msg_info: &mut MessageInfo) -> Result<String> {
if cfg!(not(target_os = "linux")) {
eyre::bail!("/proc/self/mountinfo is unavailable when target_os != linux");
}

msg_info.debug("attempting to use /proc/self/mountinfo to find container id")?;

let docker_mount = engine.command().args(["info", "--format", "{{.DockerRootDir}}"]).run_and_get_stdout(msg_info).wrap_err("failed to get docker root dir")?;

// The ID for the current Docker container might be in mountinfo,
// somewhere in a mount root. Full IDs are 64-char hexadecimal
// strings, so the first matching path segment in a mount root
// containing /docker/ is likely to be what we're looking for. See:
// strings.
// See:
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt
// https://community.toradex.com/t/15240/4
let mountinfo = file::read("/proc/self/mountinfo")?;
let container_id = mountinfo
.lines()
.filter_map(|s| s.split(' ').nth(3))
.filter(|s| s.contains("/docker/"))
.filter(|s| s.contains(&docker_mount))
.flat_map(|s| s.split('/'))
.find(|s| s.len() == 64 && s.as_bytes().iter().all(u8::is_ascii_hexdigit))
.ok_or_else(|| eyre::eyre!("couldn't find container id in mountinfo"))?;
@@ -1377,7 +1381,7 @@

fn docker_inspect_self(engine: &Engine, msg_info: &mut MessageInfo) -> Result<String> {
// Try to find the container ID by looking at HOSTNAME, and fallback to
// parsing `/proc/self/mountinfo` if HOSTNAME is unset or if there's no
// parsing other data if HOSTNAME is unset or if there's no
// container that matches it (necessary e.g. when the container uses
// `--network=host`, which is act's default, see issue #1321).
// If `docker inspect` fails with unexpected output, skip the fallback
@@ -1397,7 +1401,7 @@
// likely indicating that the hostname isn't a valid container ID.
if array.is_empty() {
msg_info.debug("docker inspect found no containers matching HOSTNAME, retrying using mountinfo")?;
return docker_inspect_self_mountinfo(engine, msg_info);
return docker_find_self_mountinfo(engine, msg_info);
}
}
}
@@ -1410,7 +1414,7 @@
}
} else {
msg_info.debug("HOSTNAME environment variable is unset")?;
docker_inspect_self_mountinfo(engine, msg_info)
docker_find_self_mountinfo(engine, msg_info)
}
}

@@ -1747,7 +1751,7 @@
}

#[test]
#[cfg_attr(cross_sandboxed, ignore)]

Check warning on line 1754 in src/docker/shared.rs

GitHub Actions / docker-in-docker

unexpected `cfg` condition name: `cross_sandboxed`
fn test_host() -> Result<()> {
let vars = unset_env();
let mount_finder = MountFinder::new(vec![]);
Loading