Skip to content
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

Docker in docker #387

Merged
merged 1 commit into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ semver = "0.9"
toml = "0.5"
which = { version = "3.1.0", default_features = false }
shell-escape = "0.1.4"
serde_json = "1.0.48"

[target.'cfg(not(windows))'.dependencies]
nix = "0.15"
Expand Down
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,49 @@ RUN dpkg --add-architecture arm64 && \
$ docker build -t my/image:tag path/to/where/the/Dockerfile/resides
```

### Docker in Docker

When running `cross` from inside a docker container, `cross` needs access to
the hosts docker daemon itself. This is normally achieved by mounting the
docker daemons socket `/var/run/docker.sock`. For example:

```
$ docker run -v /var/run/docker.sock:/var/run/docker.sock -v .:/project \
-w /project my/development-image:tag cross build --target mips64-unknown-linux-gnuabi64
```

The image running `cross` requires the rust development tools to be installed.

With this setup `cross` must find and mount the correct host paths into the
container used for cross compilation. This includes the original project directory as
well as the root path of the parent container to give access to the rust build
tools.

To inform `cross` that it is running inside a container set `CROSS_DOCKER_IN_DOCKER=true`.

A development or CI container can be created like this:

```
FROM rust:1

# set CROSS_DOCKER_IN_DOCKER to inform `cross` that it is executed from within a container
ENV CROSS_DOCKER_IN_DOCKER=true

# install `cross`
RUN cargo install cross

...

```

**Limitations**: Finding the mount point for the containers root directory is
currently only available for the overlayfs2 storage driver. In order to access
the parent containers rust setup, the child container mounts the parents
overlayfs. The parent must not be stopped before the child container, as the
overlayfs can not be unmounted correctly by Docker if the child container still
accesses it.


### Passing environment variables into the build environment

By default, `cross` does not pass any environment variables into the build
Expand Down
3 changes: 3 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ jobs:
- bash: echo "##vso[task.setvariable variable=TAG]${BUILD_SOURCEBRANCH##refs/tags/}"
displayName: Set TAG Variable
condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/')
- bash: cargo test
displayName: Run unit tests
timeoutInMinutes: 5
- bash: ./build-docker-image.sh "${TARGET}"
displayName: Build Docker Image
timeoutInMinutes: 360
Expand Down
22 changes: 16 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::str::FromStr;
use std::{env, path::PathBuf};

use crate::Target;
use crate::cargo::Subcommand;
use crate::rustc::TargetList;
use crate::Target;

pub struct Args {
pub all: Vec<String>,
pub subcommand: Option<Subcommand>,
pub target: Option<Target>,
pub target_dir: Option<PathBuf>,
pub docker_in_docker: bool,
}

pub fn parse(target_list: &TargetList) -> Args {
Expand All @@ -27,7 +29,10 @@ pub fn parse(target_list: &TargetList) -> Args {
all.push(t);
}
} else if arg.starts_with("--target=") {
target = arg.splitn(2, '=').nth(1).map(|s| Target::from(&*s, target_list));
target = arg
.splitn(2, '=')
.nth(1)
.map(|s| Target::from(&*s, target_list));
all.push(arg);
} else if arg == "--target-dir" {
all.push(arg);
Expand All @@ -41,19 +46,24 @@ pub fn parse(target_list: &TargetList) -> Args {
all.push(format!("--target-dir=/target"));
}
} else {
if !arg.starts_with('-') && sc.is_none() {
sc = Some(Subcommand::from(arg.as_ref()));
}
if !arg.starts_with('-') && sc.is_none() {
sc = Some(Subcommand::from(arg.as_ref()));
}

all.push(arg.to_string());
all.push(arg.to_string());
}
}
}

let docker_in_docker = env::var("CROSS_DOCKER_IN_DOCKER")
.map(|s| bool::from_str(&s).unwrap_or_default())
.unwrap_or_default();

Args {
all,
subcommand: sc,
target,
target_dir,
docker_in_docker,
}
}
Loading