{"record":{"id":"236d1b39dbbb5248","repo":"GoogleContainerTools/skaffold","slug":"failed-to-remove-old-container-s-for-image-s-w","errorCode":null,"errorMessage":"failed to remove old container %s for image %s: %w","messagePattern":"failed to remove old container (.+?) for image (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/deploy/docker/deploy.go","lineNumber":168,"sourceCode":"\n\treturn nil\n}\n\nfunc (d *Deployer) ConfigName() string {\n\treturn d.configName\n}\n\n// deploy creates a container in the local docker daemon from a build artifact's image.\nfunc (d *Deployer) deploy(ctx context.Context, out io.Writer, artifact graph.Artifact) error {\n\tif !stringslice.Contains(d.cfg.Images, artifact.ImageName) {\n\t\t// TODO(nkubala)[07/20/21]: should we error out in this case?\n\t\tolog.Entry(ctx).Warnf(\"skipping deploy for image %s since it was not built by Skaffold\", artifact.ImageName)\n\t\treturn nil\n\t}\n\tif container, found := d.tracker.ContainerForImage(artifact.ImageName); found {\n\t\tolog.Entry(ctx).Debugf(\"removing old container %s for image %s\", container.ID, artifact.ImageName)\n\t\tif err := d.client.Delete(ctx, out, container.ID); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to remove old container %s for image %s: %w\", container.ID, artifact.ImageName, err)\n\t\t}\n\t\td.portManager.RelinquishPorts(container.Name)\n\t}\n\tif d.cfg.UseCompose {\n\t\t// TODO(nkubala): implement\n\t\treturn fmt.Errorf(\"docker compose not yet supported by skaffold\")\n\t}\n\n\tcontainerCfg, err := d.containerConfigFromImage(ctx, artifact.Tag)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tcontainerName := d.getContainerName(ctx, artifact.ImageName)\n\topts := dockerutil.ContainerCreateOpts{\n\t\tName:            containerName,\n\t\tNetwork:         d.network,\n\t\tContainerConfig: containerCfg,","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/deploy/docker/deploy.go#L150-L186","documentation":"Before deploying a new container for an image, the Docker deployer removes the previous container tracked for that image. If `client.Delete` fails to remove the old container, the error is wrapped as `failed to remove old container %s for image %s`.","triggerScenarios":"The previously deployed container cannot be deleted — e.g. Docker daemon reports it as in-use, is in a removing/restarting state, the container ID no longer exists (stale tracker state), or daemon connectivity/permission issues.","commonSituations":"Stale skaffold state after a killed run leaving a ghost container reference; container is being removed concurrently (`removal in progress`); Docker daemon restart mid-deploy; volume mounts or other containers attached preventing removal.","solutions":["Manually remove the stale container: `docker rm -f <containerID>` then redeploy","If the tracker is stale (container already gone), the ID may be dangling — run `docker container prune` and retry","Check `docker ps -a` for containers stuck in a removing state and resolve dependents (volumes, networks)","Restart the Docker daemon if removal requests hang"],"exampleFix":"// shell fix\ndocker rm -f <container-id>\nskaffold dev","handlingStrategy":"retry","validationCode":"id := tracker.ContainerForImage(imageName)\nif id != \"\" {\n    if out, err := exec.Command(\"docker\", \"inspect\", id).CombinedOutput(); err != nil {\n        // stale tracker entry; container already gone — safe to proceed or prune\n        _ = out\n    }\n}\n","typeGuard":null,"tryCatchPattern":"err := skaffoldDeploy()\nif err != nil && strings.Contains(err.Error(), \"failed to remove old container\") {\n    // extract container id and force-remove, then retry deploy\n    if id := extractContainerID(err); id != \"\" {\n        exec.Command(\"docker\", \"rm\", \"-f\", id).Run()\n    }\n    err = skaffoldDeploy()\n}\n","preventionTips":["Gracefully stop skaffold dev sessions so tracker state stays consistent","Prune dead containers (`docker container prune`) after crashes or daemon restarts","Watch for containers stuck in removal state caused by attached volumes/networks"],"tags":["docker","container-lifecycle","deploy"],"backgroundTag":"container-delete-failed","analyzedSha":"a1189de023efc32d4b8e11f395acc678aa555011","analyzedAt":"2026-09-05T12:09:27.064Z","contentChangedAt":"2026-09-05T12:09:27.064Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}