docker/cli · error

cannot attach to a stopped container, start it first

Error message

cannot attach to a stopped container, start it first

What it means

Raised by inspectContainerAndCheckState in cli/command/container/attach.go: before attaching streams, `docker attach` inspects the container and requires State.Running to be true. Attaching to a stopped container is meaningless — there is no live process whose stdio could be streamed — so the CLI rejects it client-side instead of letting the attach hang or fail obscurely.

Source

Thrown at cli/command/container/attach.go:31

	"github.com/moby/sys/signal"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

// AttachOptions group options for `attach` command
type AttachOptions struct {
	NoStdin    bool
	Proxy      bool
	DetachKeys string
}

func inspectContainerAndCheckState(ctx context.Context, apiClient client.APIClient, args string) (*container.InspectResponse, error) {
	c, err := apiClient.ContainerInspect(ctx, args, client.ContainerInspectOptions{})
	if err != nil {
		return nil, err
	}
	if !c.Container.State.Running {
		return nil, errors.New("cannot attach to a stopped container, start it first")
	}
	if c.Container.State.Paused {
		return nil, errors.New("cannot attach to a paused container, unpause it first")
	}
	if c.Container.State.Restarting {
		return nil, errors.New("cannot attach to a restarting container, wait until it is running")
	}

	return &c.Container, nil
}

// newAttachCommand creates a new cobra.Command for `docker attach`
func newAttachCommand(dockerCLI command.Cli) *cobra.Command {
	var opts AttachOptions

	cmd := &cobra.Command{
		Use:   "attach [OPTIONS] CONTAINER",
		Short: "Attach local standard input, output, and error streams to a running container",

View on GitHub (pinned to e9452d6e78)

Solutions

  1. Start the container first: `docker start <container>`, then attach — or do both at once with `docker start -ai <container>`.
  2. Check why it stopped before restarting: `docker ps -a` for the exit code and `docker logs <container>` for output.
  3. If you wanted a shell in a running container rather than its main process, use `docker exec -it <container> sh` (exec also requires it to be running).

Example fix

# before
docker attach myapp   # Error: cannot attach to a stopped container
# after
docker start -ai myapp

When it happens

Trigger: `docker attach <container>` where ContainerInspect reports State.Running == false — the container exited, was stopped with `docker stop`, or was created (`docker create`) but never started.

Common situations: Attaching to a container whose main process crashed immediately (bad entrypoint, missing binary) so it looks 'there' in `docker ps -a` but isn't running; race conditions in scripts that attach right after a container exits; confusing `docker create` with `docker run` and attaching before any start.

Related errors


AI-assisted analysis of docker/cli@e9452d6e78 (2026-08-01). Data as JSON: /data/errors/08e2cc79a96600e1.json. Report an issue: GitHub.