cross-rs/cross · error
attempted to create already existing container.
Error message
attempted to create already existing container.
What it means
cross's `create` function refuses to create a Docker container when a container with the same target name already exists. Before creating, it checks whether the container is already present; the bail happens in the else branch when the container exists but is not in a reusable running/created state the function accepts. This is an explicit guard against duplicating build containers.
Solutions
- Remove the stale container: `docker rm -f <container>` (or `cross clean`), then rerun.
- Check for a concurrently running cross process and wait for it to finish.
- Use a different container name/id via CARGO_TARGET/cross config to avoid the collision.
Example fix
// before $ cross build --target aarch64-unknown-linux-gnu error: attempted to create already existing container. // after $ docker ps -a --filter name=cross # find stale container $ docker rm -f cross-aarch64-unknown-linux-gnu $ cross build --target aarch64-unknown-linux-gnu
Defensive patterns
Strategy: retry
Validate before calling
const { execSync } = require('child_process');
function containerExists(name) {
try {
execSync(`docker ps -a --filter name=^/${name}$ --format '{{.Names}}'`, { stdio: 'pipe' });
return true;
} catch { return false; }
} Try / catch
try {
await create(container);
} catch (e) {
if (String(e.message).includes('already existing container')) {
await removeContainer(container); // docker rm -f
return create(container); // retry once after cleanup
}
throw e;
} Prevention
- Clean up leftover cross containers after interrupted builds (docker rm -f).
- Avoid running multiple cross builds concurrently against the same target.
- Add a pre-build step that checks for and removes stale containers.
When it happens
Trigger: Calling `create` when a container with the same name already exists on the Docker daemon (e.g. `cross build` after a previous run left a stale container behind, or a concurrently running cross invocation created it first).
Common situations: A previous cross build was killed without cleaning up its container; running cross in parallel from multiple processes; container name collisions from custom `CROSS_CONTAINER_OPTS` naming.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- container was running.
- container was exited.
- Refusing to push without tag or branch. Specify a…
- unexpected progress type: expected plain, auto, or tty and…
- cannot make definite Image from unqualified PossibleImage
AI-assisted analysis of cross-rs/cross@8c1a8aa4b6 (2026-09-13).
Data as JSON: /api/errors/7a76941723734dd7.
Report an issue: GitHub.
Appendix: source
Thrown at src/docker/shared.rs:555
exists: AtomicBool::new(false),
}
}
pub fn create(engine: Engine, name: String) -> Result<()> {
// SAFETY: guarded by an atomic swap
unsafe {
#[allow(static_mut_refs)]
if !CHILD_CONTAINER.exists.swap(true, Ordering::SeqCst) {
CHILD_CONTAINER.info = Some(ChildContainerInfo {
engine,
name,
timeout: NO_TIMEOUT,
color_choice: ColorChoice::Never,
verbosity: Verbosity::Quiet,
});
Ok(())
} else {
eyre::bail!("attempted to create already existing container.");
}
}
}
// the static functions have been placed by the internal functions to
// verify the internal functions are wrapped in atomic load/stores.
pub fn exists(&self) -> bool {
self.exists.load(Ordering::SeqCst)
}
pub fn exists_static() -> bool {
// SAFETY: an atomic load.
#[allow(static_mut_refs)]
unsafe {
CHILD_CONTAINER.exists()
}
}View on GitHub (pinned to 8c1a8aa4b6)