GoogleContainerTools/skaffold · error

failed to remove old container %s for image %s: %w

Error message

failed to remove old container %s for image %s: %w

What it means

Skaffold's Docker-based verifier wraps an underlying Docker client error encountered while removing a stale container that was previously started for the same verify test's image. Before starting a new container, it stops and deletes the old one so ports and names are free; if the Docker API Remove call fails, this error surfaces the container ID and image name along with the wrapped cause.

Source

Thrown at pkg/skaffold/verify/docker/verify.go:209

			})
		}
		s.Go(func() error {
			return v.createAndRunContainer(ctx, out, na, *testCase)
		})
	}
	v.TrackBuildArtifacts(builds)
	return s.Wait()
}

// createAndRunContainer creates and runs a container in the local docker daemon from the specified verify image.
func (v *Verifier) createAndRunContainer(ctx context.Context, out io.Writer, artifact graph.Artifact, tc latest.VerifyTestCase) error {
	out, ctx = output.WithEventContext(ctx, out, constants.Verify, tc.Name)

	if container, found := v.tracker.ContainerForImage(artifact.ImageName); found {
		olog.Entry(ctx).Debugf("removing old container %s for image %s", container.ID, artifact.ImageName)
		v.client.Stop(ctx, container.ID, nil)
		if err := v.client.Remove(ctx, container.ID); err != nil {
			return fmt.Errorf("failed to remove old container %s for image %s: %w", container.ID, artifact.ImageName, err)
		}
		v.portManager.RelinquishPorts(container.Name)
	}
	containerCfg, err := v.containerConfigFromImage(ctx, artifact.Tag)
	if err != nil {
		return err
	}

	// user has set the container Entrypoint, use user value
	if len(tc.Container.Command) != 0 {
		containerCfg.Entrypoint = tc.Container.Command
	}

	// user has set the container Cmd values, use user value
	if len(tc.Container.Args) != 0 {
		containerCfg.Cmd = tc.Container.Args
	}

View on GitHub (pinned to a1189de023)

Solutions

  1. Check the wrapped cause (`docker ps -a`, `docker logs`) to see why removal failed and fix the daemon-level issue first
  2. Remove the stale container manually: `docker rm -f <containerID>` then re-run verify
  3. Restart the Docker daemon if it is unresponsive or returning internal errors
  4. Avoid running concurrent skaffold verify/deploy runs against the same image names
  5. Update Docker/Skaffold if a known API incompatibility causes removal failures

Example fix

// before: stale tracked container blocks verify
$ docker rm -f <oldContainerID>
$ skaffold verify
// after: verify proceeds, creating a fresh container for the image
Defensive patterns

Strategy: validation

Validate before calling

if c, found := tracker.ContainerForImage(artifact.ImageName); found {
    if err := client.Remove(ctx, c.ID); err != nil {
        // fall back to force removal / manual `docker rm -f <id>`
        _ = client.Stop(ctx, c.ID, nil)
    }
}

Type guard

func containerExists(ctx context.Context, c ContainerClient, id string) bool {
    _, err := c.Inspect(ctx, id)
    return err == nil
}

Try / catch

out, err := verifier.Verify(ctx, out)
if err != nil && strings.Contains(err.Error(), "failed to remove old container") {
    // inspect wrapped error, clean up manually, retry once
}

Prevention

When it happens

Trigger: createAndRunContainer finds an existing tracked container for the artifact image (v.tracker.ContainerForImage) and v.client.Remove(ctx, container.ID) returns an error, e.g. because the container was already removed externally, is in a state that forbids removal, or the Docker daemon is unreachable/returning 500s.

Common situations: The container was deleted by another process or `docker rm` while a verify run was in flight; a race where two skaffold verify invocations target the same image; Docker daemon restarting or resource issues (device busy, dependent containers); stale tracker state pointing at a removed container ID.

Related errors


AI-assisted analysis of GoogleContainerTools/skaffold@a1189de023 (2026-09-05). Data as JSON: /api/errors/1c3708c64744605f. Report an issue: GitHub.