Engine: inspect gives back HostConfig's Binds, Mounts and Tmpfs, so a recreate keeps its volumes (#610)
Co-authored-by: Elyter <eliottbreg@gmail.com>
This commit is contained in:
1 parent
3e40e54517
commit
2388cd5cc1
6 files changed
+366
-2
No files matched your search
@@ -1842,6 +1842,80 @@ fn mounts(body: &Value, engine: &Engine, image: &kiri_image::Image) -> Result<Mo
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// What inspect gives back of a create's mounts: HostConfig's `Binds`,
|
||||
/// `Mounts` and `Tmpfs` as they came, and `Volumes` (`-v /path`) with the
|
||||
/// image's VOLUME lines, as Docker's Config has them. Tools that recreate a
|
||||
/// container from its inspect (Watchtower, Portainer, Arcane) copy these (#610).
|
||||
fn mount_request(body: &Value, image_cfg: &Value) -> Value {
|
||||
let host = &body["HostConfig"];
|
||||
let mut volumes = serde_json::Map::new();
|
||||
for given in [&body["Volumes"], &image_cfg["config"]["Volumes"]] {
|
||||
for path in given.as_object().into_iter().flat_map(|m| m.keys()) {
|
||||
volumes.entry(path.clone()).or_insert_with(|| json!({}));
|
||||
}
|
||||
}
|
||||
json!({
|
||||
"Binds": host["Binds"],
|
||||
"Mounts": host["Mounts"],
|
||||
"Tmpfs": host["Tmpfs"],
|
||||
"Volumes": if volumes.is_empty() { Value::Null } else { Value::Object(volumes) },
|
||||
})
|
||||
}
|
||||
|
||||
/// [`mount_request`] for inspect. A container saved before it was kept has
|
||||
/// only Docker's `Mounts` entries, so they are made from those: a bind or a
|
||||
/// named volume as a `Binds` line, a tmpfs in `Tmpfs` (or `Mounts` when
|
||||
/// read-only), an anonymous volume in `Volumes` (or `Mounts` when read-only),
|
||||
/// never leaking generated anonymous volume IDs or internal paths.
|
||||
fn inspect_mounts(spec: &Spec, is_anon_volume: impl Fn(&str) -> bool) -> Value {
|
||||
if !spec.mount_request.is_null() {
|
||||
return spec.mount_request.clone();
|
||||
}
|
||||
let (mut binds, mut mounts, mut tmpfs, mut volumes) = (Vec::new(), Vec::new(), serde_json::Map::new(), serde_json::Map::new());
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
for m in &spec.mount_info {
|
||||
let dst = m["Destination"].as_str().unwrap_or_default();
|
||||
if dst.is_empty() || !seen.insert(dst.trim_end_matches('/').to_owned()) {
|
||||
continue;
|
||||
}
|
||||
let rw = m["RW"] != false;
|
||||
let ro = if rw { "" } else { ":ro" };
|
||||
match (m["Type"].as_str(), m["Name"].as_str()) {
|
||||
(Some("bind"), _) => {
|
||||
let src = m["Source"].as_str().unwrap_or_default();
|
||||
if !src.is_empty() {
|
||||
binds.push(format!("{src}:{dst}{ro}"));
|
||||
}
|
||||
}
|
||||
(Some("volume"), Some(name)) if spec.anon_volumes.iter().any(|a| a == name) || is_anon_volume(name) => {
|
||||
if rw {
|
||||
volumes.insert(dst.to_owned(), json!({}));
|
||||
} else {
|
||||
mounts.push(json!({"Type": "volume", "Target": dst, "ReadOnly": true}));
|
||||
}
|
||||
}
|
||||
(Some("volume"), Some(name)) if !name.is_empty() => {
|
||||
binds.push(format!("{name}:{dst}{ro}"));
|
||||
}
|
||||
(Some("tmpfs"), _) => {
|
||||
if rw {
|
||||
tmpfs.insert(dst.to_owned(), json!(""));
|
||||
} else {
|
||||
mounts.push(json!({"Type": "tmpfs", "Target": dst, "ReadOnly": true}));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let or_null = |v: Value, empty: bool| if empty { Value::Null } else { v };
|
||||
json!({
|
||||
"Binds": or_null(json!(binds), binds.is_empty()),
|
||||
"Mounts": or_null(json!(mounts), mounts.is_empty()),
|
||||
"Tmpfs": or_null(json!(tmpfs), tmpfs.is_empty()),
|
||||
"Volumes": or_null(json!(volumes), volumes.is_empty()),
|
||||
})
|
||||
}
|
||||
|
||||
fn volume_json(v: &super::volumes::Volume) -> Value {
|
||||
json!({
|
||||
"Name": v.name,
|
||||
@@ -2402,6 +2476,7 @@ pub(super) fn create(req: &Request, engine: &Engine) -> Result<Value, Error> {
|
||||
pid_mode,
|
||||
mounts: mounts.guest,
|
||||
mount_info: mounts.info,
|
||||
mount_request: mount_request(&body, &image_cfg),
|
||||
volumes: mounts.volumes,
|
||||
anon_volumes: mounts.anon,
|
||||
restart: RestartPolicy {
|
||||
@@ -2643,8 +2718,9 @@ fn container_inspect(engine: &Engine, key: &str) -> Result<Value, Error> {
|
||||
let id = engine.resolve(key)?;
|
||||
let networks: Vec<super::network::Network> = engine.lock_net().all().to_vec();
|
||||
let health = engine.health(&id);
|
||||
let anon_vols: Vec<String> = engine.volume_list().into_iter().filter(|v| v.anonymous).map(|v| v.name).collect();
|
||||
engine.with(&id, |c| {
|
||||
let mut v = inspect(c, &networks);
|
||||
let mut v = inspect(c, &networks, &anon_vols);
|
||||
v["NetworkSettings"]["Networks"] = endpoints(c, &networks);
|
||||
// The old top-level fields describe the default bridge only.
|
||||
if c.spec.network_mode == "bridge" {
|
||||
@@ -2731,7 +2807,7 @@ fn list(req: &Request, engine: &Engine) -> Result<Value, Error> {
|
||||
Ok(Value::Array(rows.into_iter().map(|(_, v)| v).collect()))
|
||||
}
|
||||
|
||||
fn inspect(c: &Container, nets: &[super::network::Network]) -> Value {
|
||||
fn inspect(c: &Container, nets: &[super::network::Network], anon_vols: &[String]) -> Value {
|
||||
let when = |t: Option<std::time::SystemTime>| t.map_or_else(|| time::ZERO.to_owned(), time::rfc3339);
|
||||
let argv = c.argv();
|
||||
let mut v = json!({
|
||||
@@ -2818,6 +2894,12 @@ fn inspect(c: &Container, nets: &[super::network::Network]) -> Value {
|
||||
for key in ["MaskedPaths", "ReadonlyPaths"] {
|
||||
v["HostConfig"][key] = super::hostopts::system_paths(&c.spec.host_opts, key, c.spec.privileged);
|
||||
}
|
||||
// Set apart: more keys in the macro above pass its recursion limit.
|
||||
let mounts = inspect_mounts(&c.spec, |name| anon_vols.iter().any(|a| a == name));
|
||||
v["Config"]["Volumes"] = mounts["Volumes"].clone();
|
||||
for key in ["Binds", "Mounts", "Tmpfs"] {
|
||||
v["HostConfig"][key] = mounts[key].clone();
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
@@ -5229,6 +5311,260 @@ mod network_option_tests {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// Creates a container from `body` (on an image with a VOLUME line) and
|
||||
/// returns its inspect, as `docker inspect` shows it.
|
||||
fn inspect_after_create(name: &str, body: Value) -> Value {
|
||||
let dir = std::env::temp_dir().join(format!("kiri-inspect-mounts-{name}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let engine = super::super::engine::boot_without_vm(&dir);
|
||||
let cfg = json!({"created": "2026-11-20T00:00:00Z", "architecture": "arm64", "os": "linux", "config": {"Cmd": ["true"], "Volumes": {"/var/lib/img": {}}}});
|
||||
let mut w = engine.store.ingest().unwrap();
|
||||
io::Write::write_all(&mut w, &serde_json::to_vec(&cfg).unwrap()).unwrap();
|
||||
let (digest, _) = w.finish().unwrap();
|
||||
engine.store.put_image(kiri_image::archive::from_config("demo:local".into(), &digest, &cfg, Vec::new())).unwrap();
|
||||
let mut body = body;
|
||||
body["Image"] = json!("demo:local");
|
||||
let id = super::create(&Request::internal("POST", "/containers/create", &[], serde_json::to_vec(&body).unwrap()), &engine).unwrap()["Id"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
let v = container_inspect(&engine, &id).unwrap();
|
||||
drop(engine);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
v
|
||||
}
|
||||
|
||||
/// `-v` and Compose's short syntax arrive as Binds, `--mount` and the
|
||||
/// long syntax as Mounts, `--tmpfs` as Tmpfs. Docker gives each back as
|
||||
/// it came, and tools that recreate a container (Watchtower, Portainer,
|
||||
/// Arcane) copy HostConfig: without them the new container has no volumes (#610).
|
||||
#[test]
|
||||
fn inspect_gives_back_binds_mounts_and_tmpfs_as_created() {
|
||||
let src = std::env::temp_dir().join(format!("kiri-inspect-src-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&src).unwrap();
|
||||
let src = src.to_str().unwrap().to_owned();
|
||||
let binds = json!([
|
||||
format!("{src}:/b:ro"),
|
||||
"k610vol:/data:rw",
|
||||
"/var/run/docker.sock:/var/run/docker.sock",
|
||||
"/run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock",
|
||||
]);
|
||||
let mounts = json!([
|
||||
{"Type": "bind", "Source": src, "Target": "/b2", "ReadOnly": true},
|
||||
{"Type": "bind", "Source": "/run/host-services/ssh-auth.sock", "Target": "/ssh2.sock", "ReadOnly": true},
|
||||
{"Type": "volume", "Source": "k610other", "Target": "/data2", "ReadOnly": true, "VolumeOptions": {"NoCopy": true}},
|
||||
{"Type": "volume", "Target": "/anon2", "VolumeOptions": {}},
|
||||
{"Type": "tmpfs", "Target": "/t", "TmpfsOptions": {"SizeBytes": 1048576}},
|
||||
]);
|
||||
let tmpfs = json!({"/t2": "size=2m"});
|
||||
let v = inspect_after_create("given", json!({"HostConfig": {"Binds": binds, "Mounts": mounts, "Tmpfs": tmpfs}}));
|
||||
assert_eq!(v["HostConfig"]["Binds"], binds);
|
||||
assert_eq!(v["HostConfig"]["Mounts"], mounts);
|
||||
assert_eq!(v["HostConfig"]["Tmpfs"], tmpfs);
|
||||
assert_eq!(v["Config"]["Volumes"], json!({"/var/lib/img": {}}));
|
||||
// HostConfig must never leak internal volume or relay socket paths.
|
||||
let hc_text = v["HostConfig"].to_string();
|
||||
assert!(!hc_text.contains("/var/lib/kiri/volumes/"), "HostConfig leaked volume path: {hc_text}");
|
||||
assert!(!hc_text.contains("/run/kiri/host-services/ssh-auth.sock"), "HostConfig leaked host ssh relay path: {hc_text}");
|
||||
assert!(!hc_text.contains("/run/host-services/docker.sock"), "HostConfig leaked vm docker relay path: {hc_text}");
|
||||
assert!(!hc_text.contains("docker.container.sock"), "HostConfig leaked container socket path: {hc_text}");
|
||||
let _ = std::fs::remove_dir_all(&src);
|
||||
}
|
||||
|
||||
/// With no mounts asked for, Docker shows none.
|
||||
#[test]
|
||||
fn inspect_without_mounts_shows_none() {
|
||||
let v = inspect_after_create("none", json!({"HostConfig": {}}));
|
||||
assert_eq!(v["HostConfig"]["Binds"], Value::Null);
|
||||
assert_eq!(v["HostConfig"]["Mounts"], Value::Null);
|
||||
assert_eq!(v["HostConfig"]["Tmpfs"], Value::Null);
|
||||
assert_eq!(v["Config"]["Volumes"], json!({"/var/lib/img": {}}));
|
||||
}
|
||||
|
||||
/// `-v /path` arrives in Config.Volumes; Docker's inspect lists it there
|
||||
/// with the image's VOLUME lines, never in HostConfig.Binds under its
|
||||
/// generated 64-hex volume ID, so a recreate makes fresh anonymous volumes.
|
||||
#[test]
|
||||
fn inspect_lists_anonymous_volumes_in_config_volumes() {
|
||||
let v = inspect_after_create("anon", json!({"Volumes": {"/anon": {}}, "HostConfig": {}}));
|
||||
assert_eq!(v["Config"]["Volumes"], json!({"/anon": {}, "/var/lib/img": {}}));
|
||||
assert_eq!(v["HostConfig"]["Binds"], Value::Null);
|
||||
assert_eq!(v["HostConfig"]["Mounts"], Value::Null);
|
||||
assert_eq!(v["HostConfig"]["Tmpfs"], Value::Null);
|
||||
}
|
||||
|
||||
/// Recreating a container directly from its inspect `Config` + `HostConfig`
|
||||
/// verbatim (as Watchtower, Portainer and Arcane do) succeeds with no
|
||||
/// duplicate mount points and keeps named volumes, `:ro`, `ReadOnly: true`,
|
||||
/// tmpfs, and socket paths while giving anonymous volumes fresh IDs (#610).
|
||||
#[test]
|
||||
fn inspect_recreate_round_trip_keeps_mounts_without_duplicates() {
|
||||
let dir = std::env::temp_dir().join(format!("kiri-inspect-recreate-{}", std::process::id()));
|
||||
let src = std::env::temp_dir().join(format!("kiri-inspect-recreate-src-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let _ = std::fs::remove_dir_all(&src);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
std::fs::create_dir_all(&src).unwrap();
|
||||
let src_s = src.to_str().unwrap().to_owned();
|
||||
let engine = super::super::engine::boot_without_vm(&dir);
|
||||
let cfg = json!({"created": "2026-11-20T00:00:00Z", "architecture": "arm64", "os": "linux", "config": {"Cmd": ["true"], "Volumes": {"/var/lib/img": {}, "/img-overridden": {}}}});
|
||||
let mut w = engine.store.ingest().unwrap();
|
||||
io::Write::write_all(&mut w, &serde_json::to_vec(&cfg).unwrap()).unwrap();
|
||||
let (digest, _) = w.finish().unwrap();
|
||||
engine.store.put_image(kiri_image::archive::from_config("demo:local".into(), &digest, &cfg, Vec::new())).unwrap();
|
||||
|
||||
let body = json!({
|
||||
"Image": "demo:local",
|
||||
"Volumes": {"/anon": {}},
|
||||
"HostConfig": {
|
||||
"Binds": [
|
||||
format!("{src_s}:/b:ro"),
|
||||
"k610vol:/data",
|
||||
"k610img:/img-overridden",
|
||||
"/var/run/docker.sock:/var/run/docker.sock",
|
||||
"/run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock",
|
||||
],
|
||||
"Mounts": [
|
||||
{"Type": "bind", "Source": src_s, "Target": "/b2", "ReadOnly": true},
|
||||
{"Type": "volume", "Source": "k610rovol", "Target": "/rovol", "ReadOnly": true},
|
||||
{"Type": "tmpfs", "Target": "/t", "TmpfsOptions": {"SizeBytes": 1048576}},
|
||||
],
|
||||
"Tmpfs": {"/t2": "size=2m"},
|
||||
}
|
||||
});
|
||||
let id1 = super::create(&Request::internal("POST", "/containers/create", &[], serde_json::to_vec(&body).unwrap()), &engine).unwrap()["Id"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
let v1 = container_inspect(&engine, &id1).unwrap();
|
||||
|
||||
let mut recreate = v1["Config"].clone();
|
||||
recreate["HostConfig"] = v1["HostConfig"].clone();
|
||||
let id2 = super::create(&Request::internal("POST", "/containers/create", &[], serde_json::to_vec(&recreate).unwrap()), &engine).unwrap()["Id"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
let v2 = container_inspect(&engine, &id2).unwrap();
|
||||
|
||||
assert_eq!(v2["HostConfig"]["Binds"], v1["HostConfig"]["Binds"]);
|
||||
assert_eq!(v2["HostConfig"]["Mounts"], v1["HostConfig"]["Mounts"]);
|
||||
assert_eq!(v2["HostConfig"]["Tmpfs"], v1["HostConfig"]["Tmpfs"]);
|
||||
assert_eq!(v2["Config"]["Volumes"], json!({"/anon": {}, "/var/lib/img": {}, "/img-overridden": {}}));
|
||||
|
||||
// Top-level Mounts has every expected destination exactly once.
|
||||
let mounts2 = v2["Mounts"].as_array().unwrap();
|
||||
let dsts: Vec<&str> = mounts2.iter().map(|m| m["Destination"].as_str().unwrap()).collect();
|
||||
let unique: std::collections::HashSet<&str> = dsts.iter().copied().collect();
|
||||
assert_eq!(dsts.len(), unique.len(), "duplicate mount targets after recreate: {dsts:?}");
|
||||
let find = |dst: &str| mounts2.iter().find(|m| m["Destination"] == dst).unwrap_or_else(|| panic!("missing {dst}"));
|
||||
assert_eq!(find("/b")["RW"], false);
|
||||
assert_eq!(find("/b2")["RW"], false);
|
||||
assert_eq!(find("/data")["Name"], "k610vol");
|
||||
assert_eq!(find("/img-overridden")["Name"], "k610img");
|
||||
assert_eq!(find("/rovol")["Name"], "k610rovol");
|
||||
assert_eq!(find("/rovol")["RW"], false);
|
||||
assert_eq!(find("/var/run/docker.sock")["Source"], "/var/run/docker.sock");
|
||||
assert_eq!(find("/run/host-services/ssh-auth.sock")["Source"], "/run/host-services/ssh-auth.sock");
|
||||
assert_eq!(find("/t")["Type"], "tmpfs");
|
||||
assert_eq!(find("/t2")["Type"], "tmpfs");
|
||||
// Anonymous volumes get fresh IDs on recreate instead of being pinned in Binds.
|
||||
let anon1 = v1["Mounts"].as_array().unwrap().iter().find(|m| m["Destination"] == "/anon").unwrap()["Name"].as_str().unwrap();
|
||||
let anon2 = find("/anon")["Name"].as_str().unwrap();
|
||||
assert_ne!(anon1, anon2, "recreated container must get a fresh anonymous volume");
|
||||
assert!(!v1["HostConfig"]["Binds"].to_string().contains(anon1));
|
||||
|
||||
drop(engine);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
let _ = std::fs::remove_dir_all(&src);
|
||||
}
|
||||
|
||||
/// A container saved before `mount_request` was kept has only Docker's
|
||||
/// `Mounts` entries: its Binds, Mounts, Tmpfs and anonymous volumes are
|
||||
/// made from those without leaking `/var/lib/kiri/volumes/...`, internal
|
||||
/// socket paths, or anonymous 64-hex IDs, and recreating from inspect works.
|
||||
#[test]
|
||||
fn inspect_of_an_older_container_derives_its_mounts() {
|
||||
let root = std::env::temp_dir().join(format!("kiri-inspect-older-{}", std::process::id()));
|
||||
let src = std::env::temp_dir().join(format!("kiri-inspect-older-src-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let _ = std::fs::remove_dir_all(&src);
|
||||
std::fs::create_dir_all(&root).unwrap();
|
||||
std::fs::create_dir_all(&src).unwrap();
|
||||
let src_s = src.to_str().unwrap().to_owned();
|
||||
let engine = super::super::engine::boot_without_vm(&root);
|
||||
let cfg = json!({"created": "2026-11-20T00:00:00Z", "architecture": "arm64", "os": "linux", "config": {"Cmd": ["true"]}});
|
||||
let mut w = engine.store.ingest().unwrap();
|
||||
io::Write::write_all(&mut w, &serde_json::to_vec(&cfg).unwrap()).unwrap();
|
||||
let (digest, _) = w.finish().unwrap();
|
||||
let image = kiri_image::archive::from_config("older:1".into(), &digest, &cfg, Vec::new());
|
||||
engine.store.put_image(image.clone()).unwrap();
|
||||
// An anonymous volume borrowed via --volumes-from lives in volumes.json
|
||||
// as anonymous even when not listed on this container's anon_volumes.
|
||||
let borrowed_anon = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
engine.volume_ensure(borrowed_anon, json!({}), true).unwrap();
|
||||
let spec: super::super::engine::Spec = serde_json::from_value(json!({
|
||||
"image": image.name, "image_id": image.config_digest, "entrypoint": [], "cmd": ["true"],
|
||||
"env": [], "cwd": "/", "user": "", "auto_remove": false, "network_mode": "none", "labels": {},
|
||||
"restart": {"name": "no", "max_retries": 0}, "volumes": ["named", "namedro", borrowed_anon], "anon_volumes": ["0123abcd", "deadbeef"],
|
||||
"mount_info": [
|
||||
{"Type": "bind", "Source": src_s, "Destination": "/src", "Mode": "", "RW": false, "Propagation": "rprivate"},
|
||||
{"Type": "bind", "Source": "/var/run/docker.sock", "Destination": "/var/run/docker.sock", "Mode": "", "RW": true, "Propagation": "rprivate"},
|
||||
{"Type": "bind", "Source": "/run/host-services/ssh-auth.sock", "Destination": "/run/host-services/ssh-auth.sock", "Mode": "", "RW": true, "Propagation": "rprivate"},
|
||||
{"Type": "volume", "Name": "named", "Source": "/var/lib/kiri/volumes/named/_data", "Destination": "/data", "Driver": "local", "Mode": "z", "RW": true, "Propagation": ""},
|
||||
{"Type": "volume", "Name": "namedro", "Source": "/var/lib/kiri/volumes/namedro/_data", "Destination": "/dataro", "Driver": "local", "Mode": "z", "RW": false, "Propagation": ""},
|
||||
{"Type": "volume", "Name": "0123abcd", "Source": "/var/lib/kiri/volumes/0123abcd/_data", "Destination": "/anon", "Driver": "local", "Mode": "z", "RW": true, "Propagation": ""},
|
||||
{"Type": "volume", "Name": "deadbeef", "Source": "/var/lib/kiri/volumes/deadbeef/_data", "Destination": "/anonro", "Driver": "local", "Mode": "z", "RW": false, "Propagation": ""},
|
||||
{"Type": "volume", "Name": borrowed_anon, "Source": format!("/var/lib/kiri/volumes/{borrowed_anon}/_data"), "Destination": "/borrowed", "Driver": "local", "Mode": "z", "RW": true, "Propagation": ""},
|
||||
{"Type": "tmpfs", "Source": "", "Destination": "/t", "Mode": "", "RW": true, "Propagation": ""},
|
||||
{"Type": "tmpfs", "Source": "", "Destination": "/tro", "Mode": "", "RW": false, "Propagation": ""},
|
||||
],
|
||||
})).unwrap();
|
||||
let id = engine.create(Some("older".into()), spec, &image).unwrap();
|
||||
let v = container_inspect(&engine, &id).unwrap();
|
||||
assert_eq!(
|
||||
v["HostConfig"]["Binds"],
|
||||
json!([
|
||||
format!("{src_s}:/src:ro"),
|
||||
"/var/run/docker.sock:/var/run/docker.sock",
|
||||
"/run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock",
|
||||
"named:/data",
|
||||
"namedro:/dataro:ro",
|
||||
])
|
||||
);
|
||||
assert_eq!(
|
||||
v["HostConfig"]["Mounts"],
|
||||
json!([
|
||||
{"Type": "volume", "Target": "/anonro", "ReadOnly": true},
|
||||
{"Type": "tmpfs", "Target": "/tro", "ReadOnly": true},
|
||||
])
|
||||
);
|
||||
assert_eq!(v["HostConfig"]["Tmpfs"], json!({"/t": ""}));
|
||||
assert_eq!(v["Config"]["Volumes"], json!({"/anon": {}, "/borrowed": {}}));
|
||||
let hc_text = v["HostConfig"].to_string();
|
||||
assert!(!hc_text.contains("/var/lib/kiri/volumes/"));
|
||||
assert!(!hc_text.contains("0123abcd"));
|
||||
assert!(!hc_text.contains("deadbeef"));
|
||||
assert!(!hc_text.contains(borrowed_anon));
|
||||
|
||||
// Recreating from the legacy container's inspect succeeds with no duplicates.
|
||||
let mut recreate = v["Config"].clone();
|
||||
recreate["HostConfig"] = v["HostConfig"].clone();
|
||||
let id2 = super::create(&Request::internal("POST", "/containers/create", &[], serde_json::to_vec(&recreate).unwrap()), &engine).unwrap()["Id"]
|
||||
.as_str()
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
let v2 = container_inspect(&engine, &id2).unwrap();
|
||||
let dsts: Vec<&str> = v2["Mounts"].as_array().unwrap().iter().map(|m| m["Destination"].as_str().unwrap()).collect();
|
||||
let unique: std::collections::HashSet<&str> = dsts.iter().copied().collect();
|
||||
assert_eq!(dsts.len(), unique.len(), "duplicate mount targets after legacy recreate: {dsts:?}");
|
||||
assert_eq!(dsts.len(), 10);
|
||||
|
||||
drop(engine);
|
||||
let _ = std::fs::remove_dir_all(&root);
|
||||
let _ = std::fs::remove_dir_all(&src);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_and_container_labels_follow_docker() {
|
||||
let dir = std::env::temp_dir().join(format!("kiri-labels-test-{}", std::process::id()));
|
||||
|
||||
@@ -373,6 +373,11 @@ pub struct Spec {
|
||||
/// For `inspect`: Docker's `Mounts` entries.
|
||||
#[serde(default)]
|
||||
pub mount_info: Vec<serde_json::Value>,
|
||||
/// For `inspect`: HostConfig's `Binds`, `Mounts` and `Tmpfs` as create
|
||||
/// gave them, and `Volumes` (Config's, with the image's VOLUME lines).
|
||||
/// Null for a container saved before they were kept (#610).
|
||||
#[serde(default)]
|
||||
pub mount_request: serde_json::Value,
|
||||
/// Named volumes it uses (kept while it exists), and the anonymous ones
|
||||
/// created for it (deleted with it on `--rm` or `rm -v`).
|
||||
#[serde(default)]
|
||||
|
||||
@@ -13,6 +13,12 @@ A log of every merged bug fix in Kiri, newest first, together with any later bug
|
||||
|
||||
## Fixes
|
||||
|
||||
### #759 Engine: inspect gives back HostConfig's Binds, Mounts and Tmpfs, so a recreate keeps its volumes (unreleased)
|
||||
- Fixes: #610 Engine: `docker inspect` drops `HostConfig.Binds`, `HostConfig.Mounts`, `HostConfig.Tmpfs` and `Config.Volumes`, so Watchtower, Portainer and Arcane lose volumes and binds when recreating containers
|
||||
- Caused: none known
|
||||
- Caused by: -
|
||||
- Check: `inspect-binds`, `inspect-mounts-tmpfs`, `inspect-recreate-mounts`
|
||||
|
||||
### #728 Networks: NetworkMode bridge with other networks in EndpointsConfig joins only those, as Docker does (unreleased)
|
||||
- Fixes: #727 Engine: `NetworkMode: "bridge"` + one `EndpointsConfig` network joins both networks with bridge DNS, so `k3d cluster create` agents can't reach the server
|
||||
- Caused: none known
|
||||
|
||||
@@ -1093,6 +1093,16 @@ t volumes-from-unknown "No such container: vfnosuch" bash -c "o=\$($D run --rm -
|
||||
# The anonymous volume stays while another container still uses it.
|
||||
t volumes-from-anon-kept "got:a" bash -c "$D rm -v vfsrc >/dev/null; $D run --rm --volumes-from vfdst alpine sh -c 'echo got:\$(cat /anon/f)'"
|
||||
$D rm -f -v vfsrc vfdst >/dev/null 2>&1; $D volume rm tvfrom $VFANON >/dev/null 2>&1
|
||||
# Inspect gives back the mounts as create got them, in HostConfig and in
|
||||
# Config.Volumes. Watchtower, Portainer and Arcane recreate a container from
|
||||
# its inspect, and so keep its binds, volumes, tmpfs and sockets (#610).
|
||||
$D rm -f -v hcm hcm2 >/dev/null 2>&1
|
||||
$D run --rm -v thcm:/data -v thcmro:/rodata alpine sh -c 'echo kept > /data/f; echo rokept > /rodata/f'
|
||||
$D create --name hcm -v "$BIND:/bind:ro" -v thcm:/data -v /anon -v /var/run/docker.sock:/var/run/docker.sock -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock --mount type=volume,source=thcmro,target=/rodata,readonly --mount type=tmpfs,target=/t --tmpfs /t2:size=1m alpine sh -c 'd=$(cat /data/f 2>/dev/null || echo MISSING); r=$(cat /rodata/f 2>/dev/null || echo MISSING); b=$(cat /bind/hello.txt 2>/dev/null || echo MISSING); (echo x > /bind/w) 2>&1 | grep -qi "read-only" && bro=ro || bro=bad; (echo x > /rodata/w) 2>&1 | grep -qi "read-only" && vro=ro || vro=bad; mount | grep -q "tmpfs on /t " && t1=ok || t1=no; mount | grep -q "tmpfs on /t2 " && t2=ok || t2=no; test -S /var/run/docker.sock && ds=ok || ds=no; test -S /run/host-services/ssh-auth.sock && ss=ok || ss=no; echo "$d|$r|$b|bro=$bro|vro=$vro|t1=$t1|t2=$t2|ds=$ds|ss=$ss"' >/dev/null
|
||||
t inspect-binds "ok" bash -c "$D inspect -f '{{json .HostConfig.Binds}}' hcm | python3 -c 'import json, sys; got = set(json.load(sys.stdin) or []); want = {sys.argv[1], \"thcm:/data\", \"/var/run/docker.sock:/var/run/docker.sock\", \"/run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock\"}; print(\"ok\" if got == want else sorted(got))' \"$BIND:/bind:ro\""
|
||||
t inspect-mounts-tmpfs 'ok {"/t2":"size=1m"} {"/anon":{}}' bash -c "m=\$($D inspect -f '{{json .HostConfig.Mounts}}' hcm | python3 -c 'import json, sys; ms = json.load(sys.stdin) or []; ok = len(ms) == 2 and {\"Type\": \"volume\", \"Source\": \"thcmro\", \"Target\": \"/rodata\", \"ReadOnly\": True} in ms and {\"Type\": \"tmpfs\", \"Target\": \"/t\"} in ms; print(\"ok\" if ok else ms)'); echo \"\$m \$($D inspect -f '{{json .HostConfig.Tmpfs}} {{json .Config.Volumes}}' hcm)\""
|
||||
t inspect-recreate-mounts "code=201 kept|rokept|from the mac|bro=ro|vro=ro|t1=ok|t2=ok|ds=ok|ss=ok anon=fresh" bash -c "a1=\$($D inspect -f '{{range .Mounts}}{{if eq .Destination \"/anon\"}}{{.Name}}{{end}}{{end}}' hcm); b=\$(curl -s --unix-socket '$SOCK' http://d/v1.47/containers/hcm/json | python3 -c 'import json, sys; c = json.load(sys.stdin); print(json.dumps(dict(c[\"Config\"], HostConfig=c[\"HostConfig\"])))'); code=\$(curl -s -o /dev/null -w '%{http_code}' --unix-socket '$SOCK' -X POST -H 'Content-Type: application/json' -d \"\$b\" 'http://d/v1.47/containers/create?name=hcm2'); out=\$($D start -a hcm2); a2=\$($D inspect -f '{{range .Mounts}}{{if eq .Destination \"/anon\"}}{{.Name}}{{end}}{{end}}' hcm2); [ -n \"\$a1\" ] && [ -n \"\$a2\" ] && [ \"\$a1\" != \"\$a2\" ] && an=fresh || an=\"same:\$a1:\$a2\"; echo \"code=\$code \$out anon=\$an\""
|
||||
$D rm -f -v hcm hcm2 >/dev/null 2>&1; $D volume rm thcm thcmro >/dev/null 2>&1
|
||||
t anon-from-image "volume:/var/lib/postgresql/data" bash -c "$D run -d --name pgv -e POSTGRES_PASSWORD=x postgres:17-alpine >/dev/null; $D inspect -f '{{range .Mounts}}{{.Type}}:{{.Destination}}{{end}}' pgv"
|
||||
# Waits for the entrypoint's "init process complete" and then a working query,
|
||||
# not for pg_isready: on first start Postgres runs a temporary server to
|
||||
|
||||
@@ -896,6 +896,10 @@ rm -f /tmp/alpine529.tar /tmp/deny-clone3.py /tmp/exercise529.sh /tmp/lh529.log
|
||||
echo "enosys=$e1 eperm=$e2"
|
||||
SH
|
||||
t clone3-seccomp-fallback "enosys=67108864:0::/|67108864:0::/|0::/sub eperm=67108864:0::/|67108864:0::/|0::/sub" "apk add -q util-linux util-linux-misc iproute2 python3 >/dev/null 2>&1; sh /clone3-seccomp-test.sh"
|
||||
# Inspect returns HostConfig.Binds, HostConfig.Mounts, HostConfig.Tmpfs and
|
||||
# Config.Volumes so recreating a container from its inspect JSON (Watchtower,
|
||||
# Portainer, Arcane) keeps volumes, :ro, tmpfs and socket binds (#610).
|
||||
t inspect-recreate-mounts "binds=ok code=201 kept|rokept|from-host|bro=ro|vro=ro|t1=ok|t2=ok|ds=ok|ss=ok anon=fresh" "docker rm -f -v lhcm lhcm2 >/dev/null 2>&1; docker volume rm lthcm lthcmro >/dev/null 2>&1; rm -rf /tmp/k610-bind && mkdir -p /tmp/k610-bind && echo from-host > /tmp/k610-bind/hello.txt; docker run --rm -v lthcm:/data -v lthcmro:/rodata alpine sh -c 'echo kept > /data/f; echo rokept > /rodata/f'; docker create --name lhcm -v /tmp/k610-bind:/bind:ro -v lthcm:/data -v /anon -v /var/run/docker.sock:/var/run/docker.sock -v /run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock --mount type=volume,source=lthcmro,target=/rodata,readonly --mount type=tmpfs,target=/t --tmpfs /t2:size=1m alpine sh -c 'd=\$(cat /data/f 2>/dev/null || echo MISSING); r=\$(cat /rodata/f 2>/dev/null || echo MISSING); b=\$(cat /bind/hello.txt 2>/dev/null || echo MISSING); (echo x > /bind/w) 2>&1 | grep -qi \"read-only\" && bro=ro || bro=bad; (echo x > /rodata/w) 2>&1 | grep -qi \"read-only\" && vro=ro || vro=bad; mount | grep -q \"tmpfs on /t \" && t1=ok || t1=no; mount | grep -q \"tmpfs on /t2 \" && t2=ok || t2=no; test -S /var/run/docker.sock && ds=ok || ds=no; test -S /run/host-services/ssh-auth.sock && ss=ok || ss=no; echo \"\$d|\$r|\$b|bro=\$bro|vro=\$vro|t1=\$t1|t2=\$t2|ds=\$ds|ss=\$ss\"' >/dev/null; binds=\$(docker inspect -f '{{json .HostConfig.Binds}}' lhcm | python3 -c 'import json, sys; got = set(json.load(sys.stdin) or []); want = {\"/tmp/k610-bind:/bind:ro\", \"lthcm:/data\", \"/var/run/docker.sock:/var/run/docker.sock\", \"/run/host-services/ssh-auth.sock:/run/host-services/ssh-auth.sock\"}; print(\"ok\" if got == want else sorted(got))'); a1=\$(docker inspect -f '{{range .Mounts}}{{if eq .Destination \"/anon\"}}{{.Name}}{{end}}{{end}}' lhcm); b=\$(curl -s --unix-socket /root/.kiri/run/docker.sock http://d/v1.47/containers/lhcm/json | python3 -c 'import json, sys; c = json.load(sys.stdin); print(json.dumps(dict(c[\"Config\"], HostConfig=c[\"HostConfig\"])))'); code=\$(curl -s -o /dev/null -w '%{http_code}' --unix-socket /root/.kiri/run/docker.sock -X POST -H 'Content-Type: application/json' -d \"\$b\" 'http://d/v1.47/containers/create?name=lhcm2'); out=\$(docker start -a lhcm2); a2=\$(docker inspect -f '{{range .Mounts}}{{if eq .Destination \"/anon\"}}{{.Name}}{{end}}{{end}}' lhcm2); [ -n \"\$a1\" ] && [ -n \"\$a2\" ] && [ \"\$a1\" != \"\$a2\" ] && an=fresh || an=\"same:\$a1:\$a2\"; docker rm -f -v lhcm lhcm2 >/dev/null 2>&1; docker volume rm lthcm lthcmro >/dev/null 2>&1; rm -rf /tmp/k610-bind; echo \"binds=\$binds code=\$code \$out anon=\$an\""
|
||||
$D rm -f "$N" >/dev/null
|
||||
# An engine in a user namespace (nested Docker, Sysbox, Coder, CI; #597),
|
||||
# where the kernel refuses mknod of a device: an image layer with a
|
||||
|
||||
@@ -239,3 +239,6 @@ container-marker-link #131 a symlink at /.dockerenv in the image must not be fol
|
||||
diff-no-marker #131 an untouched /.dockerenv created by the engine must not appear in docker diff
|
||||
export-has-marker #131 docker export must include /.dockerenv as Docker does
|
||||
commit-no-marker #131 docker commit must not include an untouched /.dockerenv in the committed layer
|
||||
inspect-binds #610 docker inspect omitted HostConfig.Binds, dropping -v binds, named volumes and socket paths on container recreate
|
||||
inspect-mounts-tmpfs #610 docker inspect omitted HostConfig.Mounts, HostConfig.Tmpfs and Config.Volumes
|
||||
inspect-recreate-mounts #610 recreating a container from its inspect Config and HostConfig (Watchtower, Portainer, Arcane) lost its volumes, read-only binds, tmpfs and socket mounts
|
||||
|
Can't render this file because it contains an unexpected character in line 2 and column 52.
|
Reference in new issue
Block a user