docker/cli · error

cannot attach to a restarting container, wait until it is ru

Error message

cannot attach to a restarting container, wait until it is running

What it means

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.

Source

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

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",
		Args:  cli.ExactArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			containerID := args[0]
			return RunAttach(cmd.Context(), dockerCLI, containerID, &opts)
		},
		Annotations: map[string]string{

View on GitHub (pinned to e9452d6e78)

Solutions

  1. 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.
  2. Wait until the state is running, then attach: `docker inspect -f '{{.State.Status}}' <container>` should report 'running' (poll or use `docker events`).
  3. If it never stabilizes, stop the loop (`docker update --restart=no <container> && docker stop <container>`), fix the underlying error, and start it again.

Example fix

# before
docker restart myapp && docker attach myapp   # attaches mid-restart
# after
docker restart myapp
until [ "$(docker inspect -f '{{.State.Status}}' myapp)" = running ]; do :; done
docker attach myapp

When it happens

Trigger: `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.

Common situations: 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.

Related errors


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