Skip to content
Merged
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
32 changes: 21 additions & 11 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ const CROSS_IMAGE: &str = "ghcr.io/cross-rs";
const DOCKER: &str = "docker";
const PODMAN: &str = "podman";

// determine if the container engine is docker. this fixes issues with
// any aliases (#530), and doesn't fail if an executable suffix exists.
fn get_is_docker(ce: std::path::PathBuf, verbose: bool) -> Result<bool> {
let stdout = Command::new(ce)
.arg("--help")
.run_and_get_stdout(verbose)?
.to_lowercase();

Ok(stdout.contains("docker") && !stdout.contains("emulate"))
}

fn get_container_engine() -> Result<std::path::PathBuf, which::Error> {
if let Ok(ce) = env::var("CROSS_CONTAINER_ENGINE") {
which::which(ce)
Expand Down Expand Up @@ -126,6 +137,7 @@ pub fn run(
let runner = config.runner(target)?;

let mut docker = docker_command("run")?;
let is_docker = get_is_docker(get_container_engine().unwrap(), verbose)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this can't fail due to the above command: docker_command ensures that get_container_engine is not an error.


for ref var in config.env_passthrough(target)? {
validate_env_var(var)?;
Expand Down Expand Up @@ -179,17 +191,15 @@ pub fn run(
}

// We need to specify the user for Docker, but not for Podman.
if let Ok(ce) = get_container_engine() {
if ce.ends_with(DOCKER) {
docker.args(&[
"--user",
&format!(
"{}:{}",
env::var("CROSS_CONTAINER_UID").unwrap_or_else(|_| id::user().to_string()),
env::var("CROSS_CONTAINER_GID").unwrap_or_else(|_| id::group().to_string()),
),
]);
}
if is_docker {
docker.args(&[
"--user",
&format!(
"{}:{}",
env::var("CROSS_CONTAINER_UID").unwrap_or_else(|_| id::user().to_string()),
env::var("CROSS_CONTAINER_GID").unwrap_or_else(|_| id::group().to_string()),
),
]);
}

docker
Expand Down