jesseduffield/lazydocker · error

You cannot attach to a stopped container, you need to start

Error message

You cannot attach to a stopped container, you need to start it first (which you can actually do with the 'r' key) (yes I'm too lazy to do this automatically for you) (pretty cool that I get to communicate one-on-one with you in the form of an error message though)

What it means

Thrown by Container.Attach() when the container's reported State is "exited". Docker cannot attach to a stopped container because there is no living process to attach a terminal to. The message (written half-jokingly) points you at lazydocker's 'r' key, which restarts the stopped container.

Source

Thrown at pkg/commands/container.go:102

// Restart restarts the container
func (c *Container) Restart() error {
	c.Log.Warn(fmt.Sprintf("restarting container %s", c.Name))
	return c.Client.ContainerRestart(context.Background(), c.ID, container.StopOptions{})
}

// Attach attaches the container
func (c *Container) Attach() (*exec.Cmd, error) {
	if !c.DetailsLoaded() {
		return nil, errors.New(c.Tr.WaitingForContainerInfo)
	}

	// verify that we can in fact attach to this container
	if !c.Details.Config.OpenStdin {
		return nil, errors.New(c.Tr.UnattachableContainerError)
	}

	if c.Container.State == "exited" {
		return nil, errors.New(c.Tr.CannotAttachStoppedContainerError)
	}

	c.Log.Warn(fmt.Sprintf("attaching to container %s", c.Name))
	// TODO: use SDK
	cmd := c.OSCommand.NewCmd("docker", "attach", "--sig-proxy=false", c.ID)
	return cmd, nil
}

// Top returns process information
func (c *Container) Top(ctx context.Context) (container.TopResponse, error) {
	detail, err := c.Inspect()
	if err != nil {
		return container.TopResponse{}, err
	}

	// check container status
	if !detail.State.Running {
		return container.TopResponse{}, errors.New("container is not running")

View on GitHub (pinned to 7e7aadc207)

Solutions

  1. Start the container first: press 'r' in lazydocker or run `docker start <container>`, then attach.
  2. If the container keeps exiting, check why before attaching: `docker logs <container>` (the 'l' view in lazydocker) — most exits are app-level crashes or missing configuration.
  3. For containers that exit instantly, add a long-running entrypoint or `tty: true` so the container stays alive and attachable.

Example fix

# before
docker attach mycontainer   # fails: container is exited

# after
docker start mycontainer && sleep 1 && docker attach --sig-proxy=false mycontainer
Defensive patterns

Strategy: validation

Validate before calling

if container.Container.State == "exited" || container.Container.State == "created" {
    if err := container.Start(); err != nil {
        return err
    }
    // wait for running state before attach
}

Try / catch

cmd, err := container.Attach()
if err != nil {
    if err.Error() == tr.CannotAttachStoppedContainerError {
        _ = container.Start()
        cmd, err = container.Attach() // retry once after start
    }
}

Prevention

When it happens

Trigger: Calling Container.Attach() after the container's state has transitioned to "exited" — e.g. the process inside crashed or was stopped with `docker stop`, and the UI list still shows it (stopped containers are listed too).

Common situations: A service crashes on startup (bad config, missing env var) and the user immediately tries to attach to read output; user stops a container and forgets it is stopped; containers that exit immediately after start in a restart loop.

Related errors


AI-assisted analysis of jesseduffield/lazydocker@7e7aadc207 (2026-08-15). Data as JSON: /api/errors/a6414bd240dce04b. Report an issue: GitHub.