{"record":{"id":"7a76941723734dd7","repo":"cross-rs/cross","slug":"attempted-to-create-already-existing-container","errorCode":null,"errorMessage":"attempted to create already existing container.","messagePattern":"attempted to create already existing container\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/docker/shared.rs","lineNumber":555,"sourceCode":"            exists: AtomicBool::new(false),\n        }\n    }\n\n    pub fn create(engine: Engine, name: String) -> Result<()> {\n        // SAFETY: guarded by an atomic swap\n        unsafe {\n            #[allow(static_mut_refs)]\n            if !CHILD_CONTAINER.exists.swap(true, Ordering::SeqCst) {\n                CHILD_CONTAINER.info = Some(ChildContainerInfo {\n                    engine,\n                    name,\n                    timeout: NO_TIMEOUT,\n                    color_choice: ColorChoice::Never,\n                    verbosity: Verbosity::Quiet,\n                });\n                Ok(())\n            } else {\n                eyre::bail!(\"attempted to create already existing container.\");\n            }\n        }\n    }\n\n    // the static functions have been placed by the internal functions to\n    // verify the internal functions are wrapped in atomic load/stores.\n\n    pub fn exists(&self) -> bool {\n        self.exists.load(Ordering::SeqCst)\n    }\n\n    pub fn exists_static() -> bool {\n        // SAFETY: an atomic load.\n        #[allow(static_mut_refs)]\n        unsafe {\n            CHILD_CONTAINER.exists()\n        }\n    }","sourceCodeStart":537,"sourceCodeEnd":573,"githubUrl":"https://github.com/cross-rs/cross/blob/8c1a8aa4b661711f4b7b6ac07c2e8929ce2f7d27/src/docker/shared.rs#L537-L573","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\n$ cross build --target aarch64-unknown-linux-gnu\nerror: attempted to create already existing container.\n// after\n$ docker ps -a --filter name=cross   # find stale container\n$ docker rm -f cross-aarch64-unknown-linux-gnu\n$ cross build --target aarch64-unknown-linux-gnu","handlingStrategy":"retry","validationCode":"const { execSync } = require('child_process');\nfunction containerExists(name) {\n  try {\n    execSync(`docker ps -a --filter name=^/${name}$ --format '{{.Names}}'`, { stdio: 'pipe' });\n    return true;\n  } catch { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await create(container);\n} catch (e) {\n  if (String(e.message).includes('already existing container')) {\n    await removeContainer(container); // docker rm -f\n    return create(container); // retry once after cleanup\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["docker","container","conflict","build-tooling"],"backgroundTag":"file-already-exists","analyzedSha":"8c1a8aa4b661711f4b7b6ac07c2e8929ce2f7d27","analyzedAt":"2026-09-13T15:10:43.988Z","contentChangedAt":"2026-09-13T15:10:43.988Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}