{"record":{"id":"a6414bd240dce04b","repo":"jesseduffield/lazydocker","slug":"you-cannot-attach-to-a-stopped-container-you-need","errorCode":null,"errorMessage":"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)","messagePattern":"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\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/container.go","lineNumber":102,"sourceCode":"// Restart restarts the container\nfunc (c *Container) Restart() error {\n\tc.Log.Warn(fmt.Sprintf(\"restarting container %s\", c.Name))\n\treturn c.Client.ContainerRestart(context.Background(), c.ID, container.StopOptions{})\n}\n\n// Attach attaches the container\nfunc (c *Container) Attach() (*exec.Cmd, error) {\n\tif !c.DetailsLoaded() {\n\t\treturn nil, errors.New(c.Tr.WaitingForContainerInfo)\n\t}\n\n\t// verify that we can in fact attach to this container\n\tif !c.Details.Config.OpenStdin {\n\t\treturn nil, errors.New(c.Tr.UnattachableContainerError)\n\t}\n\n\tif c.Container.State == \"exited\" {\n\t\treturn nil, errors.New(c.Tr.CannotAttachStoppedContainerError)\n\t}\n\n\tc.Log.Warn(fmt.Sprintf(\"attaching to container %s\", c.Name))\n\t// TODO: use SDK\n\tcmd := c.OSCommand.NewCmd(\"docker\", \"attach\", \"--sig-proxy=false\", c.ID)\n\treturn cmd, nil\n}\n\n// Top returns process information\nfunc (c *Container) Top(ctx context.Context) (container.TopResponse, error) {\n\tdetail, err := c.Inspect()\n\tif err != nil {\n\t\treturn container.TopResponse{}, err\n\t}\n\n\t// check container status\n\tif !detail.State.Running {\n\t\treturn container.TopResponse{}, errors.New(\"container is not running\")","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/jesseduffield/lazydocker/blob/7e7aadc2071d58031bf2daafca1fbd4093efc23f/pkg/commands/container.go#L84-L120","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Start the container first: press 'r' in lazydocker or run `docker start <container>`, then attach.","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.","For containers that exit instantly, add a long-running entrypoint or `tty: true` so the container stays alive and attachable."],"exampleFix":"# before\ndocker attach mycontainer   # fails: container is exited\n\n# after\ndocker start mycontainer && sleep 1 && docker attach --sig-proxy=false mycontainer","handlingStrategy":"validation","validationCode":"if container.Container.State == \"exited\" || container.Container.State == \"created\" {\n    if err := container.Start(); err != nil {\n        return err\n    }\n    // wait for running state before attach\n}","typeGuard":null,"tryCatchPattern":"cmd, err := container.Attach()\nif err != nil {\n    if err.Error() == tr.CannotAttachStoppedContainerError {\n        _ = container.Start()\n        cmd, err = container.Attach() // retry once after start\n    }\n}","preventionTips":["Check the state column in the UI before pressing attach.","Use 'r' to (re)start stopped containers before attaching.","Investigate `docker logs` for crash-looping containers instead of repeatedly attaching."],"tags":["docker","container-attach","container-lifecycle","exited-container"],"backgroundTag":null,"analyzedSha":"7e7aadc2071d58031bf2daafca1fbd4093efc23f","analyzedAt":"2026-08-15T09:51:48.093Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}