Skip to content

Commit

Permalink
Merge #387
Browse files Browse the repository at this point in the history
387: Docker in docker r=reitermarkus a=urso

When using docker-in-docker, for example in dev or CI containers, one
normally gives access to the host docker environment to the parent container.
If the container spins up another child container, and wishes
to share a directory, one needs to compute the correct paths from
view of the host system.

### How it Works
If the environment variable `CROSS_DOCKER_IN_DOCKER=true`, we call 
`docker inspect $HOSTNAME`, to learn about details of the current container.
The `Mounts` field contains information about all mounts from the host
into the current container. The `GraphDriver` field gives us information
about the storage driver and the location of the contains root directory on the host system. Based on these information we compute
a table of all container mounts (`source->destination`).

When starting a child container we adapt all paths to be mounted, that have
a prefix in the table, to start with source. For example when mounting a
dev containers `/usr/local/cargo` directory, we will actually mount
`/var/lib/docker/overlay2/<parent container id>/merged/usr/local/cargo`.

If the project itself is mounted into the parent container, we will not
use the overlay directory, but find the project path on the host system.

### Limitations 

Finding the mount point for the containers root directory is only
implemented 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.

Co-authored-by: Steffen Siering <[email protected]>
  • Loading branch information
bors[bot] and urso authored Apr 13, 2020
2 parents 47f325a + f2c70b3 commit 8e8615b
Show file tree
Hide file tree
Showing 7 changed files with 337 additions and 11 deletions.
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

0 comments on commit 8e8615b

Please sign in to comment.