{"record":{"id":"847aebca17e08e17","repo":"docker/compose","slug":"service-q-is-not-running-container-d","errorCode":null,"errorMessage":"service %q is not running container #%d","messagePattern":"service %q is not running container #(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/compose/containers.go","lineNumber":105,"sourceCode":"\treturn f\n}\n\nfunc (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, all bool, serviceName string, containerIndex int) (container.Summary, error) {\n\tdefaultFilters := getDefaultFilters(projectName, oneOff, serviceName)\n\tif containerIndex > 0 {\n\t\tdefaultFilters.Add(\"label\", containerNumberFilter(containerIndex))\n\t}\n\tres, err := s.apiClient().ContainerList(ctx, client.ContainerListOptions{\n\t\tFilters: defaultFilters,\n\t\tAll:     all,\n\t})\n\tif err != nil {\n\t\treturn container.Summary{}, err\n\t}\n\tcontainers := res.Items\n\tif len(containers) < 1 {\n\t\tif containerIndex > 0 {\n\t\t\treturn container.Summary{}, fmt.Errorf(\"service %q is not running container #%d\", serviceName, containerIndex)\n\t\t}\n\t\treturn container.Summary{}, fmt.Errorf(\"service %q is not running\", serviceName)\n\t}\n\n\t// Sort by container number first, then put one-off containers at the end\n\tsort.Slice(containers, func(i, j int) bool {\n\t\tnumberLabelX, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])\n\t\tnumberLabelY, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])\n\t\tIsOneOffLabelTrueX := containers[i].Labels[api.OneoffLabel] == \"True\"\n\t\tIsOneOffLabelTrueY := containers[j].Labels[api.OneoffLabel] == \"True\"\n\n\t\tif IsOneOffLabelTrueX || IsOneOffLabelTrueY {\n\t\t\treturn !IsOneOffLabelTrueX && IsOneOffLabelTrueY\n\t\t}\n\n\t\treturn numberLabelX < numberLabelY\n\t})\n\treturn containers[0], nil","sourceCodeStart":87,"sourceCodeEnd":123,"githubUrl":"https://github.com/docker/compose/blob/ddc4b044b62e9f715212ea4143fa830fac76382f/pkg/compose/containers.go#L87-L123","documentation":"getSpecifiedContainer lists containers for the project plus a com.docker.compose.container-number label filter for the requested index; when the filtered list is empty and an explicit index (>0) was requested, this specific error is returned. It is the backing error for index-addressed operations such as `docker compose exec --index N` and `docker compose logs --index N`.","triggerScenarios":"`docker compose exec --index 3 web sh` when the service runs fewer than 3 replicas (or none); the container-number label filter matches no container because scaling decreased, the numbered container was recreated, or `--all` was not passed and that replica is stopped.","commonSituations":"Scripts hardcoding `--index` against autoscaled services; using --index right after `up --scale web=2` reduced replicas; addressing a stopped replica (needs All=true listing) that the default running-only filter hides.","solutions":["Check the actual replica count and numbers: `docker compose ps web` (container numbers are in the names, e.g. web-2)","Scale up first (`docker compose up -d --scale web=3`) or drop --index to let Compose pick any running replica","If the target replica exists but is stopped, start it (`docker compose start web`) before the indexed command"],"exampleFix":"# before\ndocker compose exec --index 3 web sh   # only 2 replicas\n\n# after\ndocker compose up -d --scale web=3\ndocker compose exec --index 3 web sh","handlingStrategy":"validation","validationCode":"// before exec --index N, confirm a container with that number label exists\nlist, _ := dockerCli.Client().ContainerList(ctx, client.ContainerListOptions{\n    All: true,\n    Filters: filters.NewArgs(\n        filters.Arg(\"label\", \"com.docker.compose.project=\"+project),\n        filters.Arg(\"label\", \"com.docker.compose.service=\"+service),\n        filters.Arg(\"label\", fmt.Sprintf(\"com.docker.compose.container-number=%d\", index)),\n    ),\n})\nif len(list.Items) == 0 { return fmt.Errorf(\"no replica #%d for %s\", index, service) }","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"is not running container #\") {\n    // re-check replica count (compose ps) and retry without --index or after scaling up\n}","preventionTips":["Query `docker compose ps` for live replica numbers instead of hardcoding --index","Scale before index-addressed commands in scripts"],"tags":["containers","exec","scale","index","not-found"],"backgroundTag":null,"analyzedSha":"ddc4b044b62e9f715212ea4143fa830fac76382f","analyzedAt":"2026-08-15T13:31:42.319Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}