docker/cli · error

container name cannot be empty

Error message

container name cannot be empty

What it means

Returned by runRm's per-container worker when, after strings.Trim(ctrID, "/"), the container identifier is empty. This catches arguments that were only slashes (e.g. '//') or expanded to nothing, preventing an empty ID being sent to the daemon's remove API.

Solutions

  1. Ensure the container name/ID argument is non-empty before calling docker rm.
  2. Guard the variable: `CID=${CID:?container id required}` or skip empty entries in loops.
  3. Sanitize inputs from config files (strip blanks) before passing to docker rm.

Example fix

// before
for c in "$@"; do docker rm "$c"; done   # $@ contains ''

// after
for c in "$@"; do [ -n "$c" ] && docker rm "$c"; done
Defensive patterns

Strategy: validation

Validate before calling

func validContainerID(id string) error {
    if strings.TrimSpace(strings.Trim(id, "/")) == "" {
        return errors.New("container name cannot be empty")
    }
    return nil
}

for _, c := range containers {
    if err := validContainerID(c); err != nil {
        return err
    }
}

Prevention

When it happens

Trigger: `docker rm ''`, `docker rm //`, or a variable that expands to empty/slashes. parallelOperation runs each arg through the trim; an all-slash arg becomes empty.

Common situations: Unset shell variable: `docker rm "$CID"` with CID empty. Looping over a list with a trailing empty element. JSON/YAML parsing producing empty container names. Misconfigured cleanup scripts.

Related errors


AI-assisted analysis of docker/cli@4f84911bfe (2026-08-07). Data as JSON: /api/errors/5c5efe13a1ab0f94. Report an issue: GitHub.

Appendix: source

Thrown at cli/command/container/rm.go:77

	flags.BoolVarP(&opts.force, "force", "f", false, "Force the removal of a running container (uses SIGKILL)")
	return cmd
}

// newRemoveCommand adds subcommands for "docker container"; unlike the
// top-level "docker rm", it also adds a "remove" alias to support
// "docker container remove" in addition to "docker container rm".
func newRemoveCommand(dockerCli command.Cli) *cobra.Command {
	cmd := *newRmCommand(dockerCli)
	cmd.Aliases = []string{"rm", "remove"}
	return &cmd
}

func runRm(ctx context.Context, dockerCLI command.Cli, opts *rmOptions) error {
	apiClient := dockerCLI.Client()
	errChan := parallelOperation(ctx, opts.containers, func(ctx context.Context, ctrID string) error {
		ctrID = strings.Trim(ctrID, "/")
		if ctrID == "" {
			return errors.New("container name cannot be empty")
		}
		_, err := apiClient.ContainerRemove(ctx, ctrID, client.ContainerRemoveOptions{
			RemoveVolumes: opts.rmVolumes,
			RemoveLinks:   opts.rmLink,
			Force:         opts.force,
		})
		return err
	})

	var errs []error
	for _, name := range opts.containers {
		if err := <-errChan; err != nil {
			if opts.force && errdefs.IsNotFound(err) {
				continue
			}
			errs = append(errs, err)
			continue
		}

View on GitHub (pinned to 4f84911bfe)