{"record":{"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/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/container/attach.go#L19-L55","documentation":"inspectContainerAndCheckState returns errors.New(\"cannot attach to a restarting container, wait until it is running\") at line 37 when c.Container.State.Restarting is true. During a restart policy triggered cycle the container is transitional (not yet Running), so attach is rejected; the caller should wait for the container to reach a stable running state.","triggerScenarios":"Running `docker attach <id>` against a container with a restart policy (e.g. --restart=always/unless-stopped/on-failure) that is currently mid-restart — the process exited and the daemon is restarting it.","commonSituations":"Containers that crash-loop with a restart policy; attaching immediately after a host/daemon restart; flapping services; attaching during a restart-policy backoff window.","solutions":["Wait for the container to reach Running: `docker wait` is not suitable here; poll `docker inspect -f '{{.State.Running}}' <id>` until true, or use `until docker inspect -f '{{.State.Status}}' id | grep -q running; do ...`.","If it crash-loops, inspect `docker logs <id>` and fix the crash cause (entrypoint, missing config).","Temporarily remove/loosen the restart policy to inspect, or `docker update --restart=no <id>`."],"exampleFix":"# before (container mid-restart)\ndocker attach web\n# after — wait for running then attach\nuntil [ \"$(docker inspect -f '{{.State.Running}}' web)\" = \"true\" ]; do sleep 1; done\ndocker attach web","handlingStrategy":"validation","validationCode":"c, err := cli.Client().ContainerInspect(ctx, id, client.ContainerInspectOptions{})\nif err != nil { return err }\nif c.State.Restarting {\n    // wait for Running: poll or back off\n    return fmt.Errorf(\"container %s is restarting; retry shortly\", id)\n}","typeGuard":null,"tryCatchPattern":"for attempt := 0; attempt < maxRetry; attempt++ {\n    if _, err := inspectContainerAndCheckState(ctx, cli.Client(), id); err == nil { break }\n    if !strings.Contains(err.Error(), \"restarting container\") { return err }\n    // brief wait, then retry (use a timer, not a busy loop)\n}","preventionTips":["For crash-looping containers, inspect `docker logs` to fix the root cause first.","Consider `docker update --restart=no` to stop the restart cycle while debugging.","Poll State.Running before attach rather than attaching blindly."],"tags":["container","attach","restart","lifecycle","validation"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}