docker/cli ยท error
container name cannot be empty
Error message
container name cannot be empty
What it means
Raised in runRm for `docker rm` when one of the container arguments, after trimming leading/trailing slashes, is an empty string. The CLI validates this client-side per container (removals run in parallel) so it fails fast instead of sending an empty ID to the ContainerRemove API.
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 e9452d6e78)
Solutions
- Guard script-generated arguments: only call docker rm when the ID list is non-empty (e.g. ids=$(docker ps -aq); [ -n "$ids" ] && docker rm $ids)
- Filter blank lines before passing to docker rm, e.g. ... | grep . | xargs -r docker rm
- Check for stray quoting that turns an empty variable into an explicit "" argument
Example fix
# before docker rm "$(docker ps -aq --filter status=exited)" # empty result -> docker rm "" # after docker ps -aq --filter status=exited | xargs -r docker rm
When it happens
Trigger: `docker rm ""`, `docker rm /` or `docker rm //` (slashes are trimmed to empty), or an argument list built from a variable/command substitution that yields an empty element, e.g. docker rm $ids where ids contains an empty entry.
Common situations: Shell scripts doing `docker rm $(docker ps -aq --filter ...)` where the subshell output is empty or contains blank lines quoted into empty args; xargs/loop pipelines passing empty strings; container names copied with a leading '/' from `docker inspect` plus nothing else.
Related errors
- config file is required
- source can not be empty
- conflicting options: cannot specify both --network-alias and
- valid streams are STDIN, STDOUT and STDERR
- container prune has been cancelled
AI-assisted analysis of docker/cli@e9452d6e78 (2026-08-01).
Data as JSON: /data/errors/5c5efe13a1ab0f94.json.
Report an issue: GitHub.