{"id":"d35c1cf1e7f2bb8d","repo":"docker/cli","slug":"cannot-attach-to-a-restarting-container-wait-unti","errorCode":null,"errorMessage":"cannot attach to a restarting container, wait until it is running","messagePattern":"cannot attach to a restarting container, wait until it is running","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/container/attach.go","lineNumber":37,"sourceCode":"type AttachOptions struct {\n\tNoStdin    bool\n\tProxy      bool\n\tDetachKeys string\n}\n\nfunc inspectContainerAndCheckState(ctx context.Context, apiClient client.APIClient, args string) (*container.InspectResponse, error) {\n\tc, err := apiClient.ContainerInspect(ctx, args, client.ContainerInspectOptions{})\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tif !c.Container.State.Running {\n\t\treturn nil, errors.New(\"cannot attach to a stopped container, start it first\")\n\t}\n\tif c.Container.State.Paused {\n\t\treturn nil, errors.New(\"cannot attach to a paused container, unpause it first\")\n\t}\n\tif c.Container.State.Restarting {\n\t\treturn nil, errors.New(\"cannot attach to a restarting container, wait until it is running\")\n\t}\n\n\treturn &c.Container, nil\n}\n\n// newAttachCommand creates a new cobra.Command for `docker attach`\nfunc newAttachCommand(dockerCLI command.Cli) *cobra.Command {\n\tvar opts AttachOptions\n\n\tcmd := &cobra.Command{\n\t\tUse:   \"attach [OPTIONS] CONTAINER\",\n\t\tShort: \"Attach local standard input, output, and error streams to a running container\",\n\t\tArgs:  cli.ExactArgs(1),\n\t\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tcontainerID := args[0]\n\t\t\treturn RunAttach(cmd.Context(), dockerCLI, containerID, &opts)\n\t\t},\n\t\tAnnotations: map[string]string{","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/docker/cli/blob/e9452d6e785f6e365712b9d71bd7517591773c86/cli/command/container/attach.go#L19-L55","documentation":"Raised by inspectContainerAndCheckState in cli/command/container/attach.go when the container's State.Restarting is true. During a restart the old process is gone and the new one isn't up yet, so there is no stable stdio to attach to; the CLI tells the user to wait until the container is running again.","triggerScenarios":"`docker attach <container>` while the daemon is cycling the container under a restart policy (on-failure/always/unless-stopped) or during an in-flight `docker restart`, so ContainerInspect reports State.Restarting == true.","commonSituations":"Crash-looping containers whose process exits immediately and the restart policy keeps relaunching it — the container spends most of its time in Restarting; attaching right after `docker restart` in a script without waiting; health-check-driven restarts by orchestrators.","solutions":["Diagnose the crash loop instead of attaching: `docker logs --tail 100 <container>` and `docker inspect -f '{{.State.ExitCode}} {{.RestartCount}}' <container>` usually reveal why it keeps restarting.","Wait until the state is running, then attach: `docker inspect -f '{{.State.Status}}' <container>` should report 'running' (poll or use `docker events`).","If it never stabilizes, stop the loop (`docker update --restart=no <container> && docker stop <container>`), fix the underlying error, and start it again."],"exampleFix":"# before\ndocker restart myapp && docker attach myapp   # attaches mid-restart\n# after\ndocker restart myapp\nuntil [ \"$(docker inspect -f '{{.State.Status}}' myapp)\" = running ]; do :; done\ndocker attach myapp","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":["docker-cli","container-lifecycle","attach","restart-policy"],"analyzedSha":"e9452d6e785f6e365712b9d71bd7517591773c86","analyzedAt":"2026-08-01T07:09:22.474Z","schemaVersion":2}