Skip to content

Commit 7821fac

Browse files
Support Remote Docker tcp://docker:2375
1 parent 5d35b3f commit 7821fac

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

src/docker.rs

+49-15
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ pub fn docker_command(subcommand: &str) -> Result<Command> {
2424
if let Ok(ce) = get_container_engine() {
2525
let mut command = Command::new(ce);
2626
command.arg(subcommand);
27-
command.args(&["--userns", "host"]);
2827
Ok(command)
2928
} else {
3029
Err("no container engine found; install docker or podman".into())
3130
}
3231
}
3332

33+
pub fn docker_run_command() -> Result<Command> {
34+
let mut command = docker_command("run")?;
35+
command.args(&["--userns", "host"]);
36+
Ok(command)
37+
}
38+
3439
/// Register binfmt interpreters
3540
pub fn register(target: &Target, verbose: bool) -> Result<()> {
3641
let cmd = if target.is_windows() {
@@ -42,14 +47,22 @@ pub fn register(target: &Target, verbose: bool) -> Result<()> {
4247
binfmt-support qemu-user-static"
4348
};
4449

45-
docker_command("run")?
50+
docker_run_command()?
4651
.arg("--privileged")
4752
.arg("--rm")
4853
.arg("ubuntu:16.04")
4954
.args(&["sh", "-c", cmd])
5055
.run(verbose)
5156
}
5257

58+
fn copy_files(dir: &PathBuf, target: &str, verbose: bool) -> Result<()> {
59+
docker_command("cp")?
60+
.args(&[format!("{}", dir.display()), format!("helper:{}", target)])
61+
.run_and_get_status(verbose)?;
62+
63+
Ok(())
64+
}
65+
5366
pub fn run(target: &Target,
5467
args: &[String],
5568
target_dir: &Option<PathBuf>,
@@ -87,6 +100,22 @@ pub fn run(target: &Target,
87100
let mount_root = mount_finder.find_mount_path(&root);
88101
let sysroot = mount_finder.find_mount_path(&sysroot);
89102

103+
docker_command("volume")?
104+
.args(&["create", "data-volume"])
105+
.run_and_get_status(verbose)?;
106+
107+
docker_command("create")?
108+
.args(&["-v", "data-volume:/cross"])
109+
.args(&["--name", "helper", "busybox", "sh", "-c", &format!("mkdir /cross/target && chown {user}:{grp} /cross/target && chown -R {user}:{grp} /cross/*", user = id::user(), grp = id::group())])
110+
.run_and_get_status(verbose)?;
111+
112+
copy_files(&cargo_dir, "/cross", verbose)?;
113+
copy_files(&xargo_dir, "/cross", verbose)?;
114+
copy_files(&sysroot, "/cross", verbose)?;
115+
copy_files(&mount_root, "/cross", verbose)?;
116+
117+
docker_command("start")?.arg("helper").run_and_get_status(verbose)?;
118+
90119
let mut cmd = if uses_xargo {
91120
SafeCommand::new("xargo")
92121
} else {
@@ -97,7 +126,7 @@ pub fn run(target: &Target,
97126

98127
let runner = None;
99128

100-
let mut docker = docker_command("run")?;
129+
let mut docker = docker_run_command()?;
101130

102131
if let Some(toml) = toml {
103132
let validate_env_var = |var: &str| -> Result<()> {
@@ -146,9 +175,9 @@ pub fn run(target: &Target,
146175
}
147176

148177
docker
149-
.args(&["-e", "XARGO_HOME=/xargo"])
150-
.args(&["-e", "CARGO_HOME=/cargo"])
151-
.args(&["-e", "CARGO_TARGET_DIR=/target"])
178+
.args(&["-e", &format!("XARGO_HOME=/cross/{}", xargo_dir.file_name().unwrap().to_str().unwrap())])
179+
.args(&["-e", &format!("CARGO_HOME=/cross/{}", cargo_dir.file_name().unwrap().to_str().unwrap())])
180+
.args(&["-e", "CARGO_TARGET_DIR=/cross/target"])
152181
.args(&["-e", &format!("USER={}", id::username().unwrap().unwrap())]);
153182

154183
if let Ok(value) = env::var("QEMU_STRACE") {
@@ -166,14 +195,8 @@ pub fn run(target: &Target,
166195

167196
docker
168197
.args(&["-e", &format!("CROSS_RUNNER={}", runner.unwrap_or_else(String::new))])
169-
.args(&["-v", &format!("{}:/xargo:Z", xargo_dir.display())])
170-
.args(&["-v", &format!("{}:/cargo:Z", cargo_dir.display())])
171-
// Prevent `bin` from being mounted inside the Docker container.
172-
.args(&["-v", "/cargo/bin"])
173-
.args(&["-v", &format!("{}:/{}:Z", mount_root.display(), mount_root.display())])
174-
.args(&["-v", &format!("{}:/rust:Z,ro", sysroot.display())])
175-
.args(&["-v", &format!("{}:/target:Z", target_dir.display())])
176-
.args(&["-w", &mount_root.display().to_string()]);
198+
.args(&["-v", "data-volume:/cross"])
199+
.args(&["-w", &format!("/cross/{}", mount_root.file_name().unwrap().to_str().unwrap())]);
177200

178201
if atty::is(Stream::Stdin) {
179202
docker.arg("-i");
@@ -184,7 +207,18 @@ pub fn run(target: &Target,
184207

185208
docker
186209
.arg(&image(toml, target)?)
187-
.args(&["sh", "-c", &format!("PATH=$PATH:/rust/bin {:?}", cmd)])
210+
.args(&["sh", "-c", &format!("PATH=$PATH:/cross/{}/bin {:?}", sysroot.file_name().unwrap().to_str().unwrap(), cmd)])
211+
.run_and_get_status(verbose)?;
212+
213+
docker_command("cp")?
214+
.args(&["helper:/cross/target", &format!("{}", target_dir.parent().unwrap().display())])
215+
.run_and_get_status(verbose)?;
216+
217+
docker_command("rm")?
218+
.arg("helper")
219+
.run_and_get_status(verbose)?;
220+
docker_command("volume")?
221+
.args(&["rm", "data-volume"])
188222
.run_and_get_status(verbose)
189223
}
190224

0 commit comments

Comments
 (0)