{"record":{"id":"c1b95d7e7c8e5ebc","repo":"docker/compose","slug":"service-q-is-not-running","errorCode":null,"errorMessage":"service %q is not running","messagePattern":"service %q is not running","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/compose/containers.go","lineNumber":107,"sourceCode":"\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\n}\n","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/docker/compose/blob/ddc4b044b62e9f715212ea4143fa830fac76382f/pkg/compose/containers.go#L89-L125","documentation":"getSpecifiedContainer's no-index variant: the ContainerList call with project/service filters returned zero containers, so no container can be picked for the service. Used by operations that need a live container (exec, run-style flows), so a stopped/removed/never-created service fails here rather than later.","triggerScenarios":"`docker compose exec web sh` (or any API built on getSpecifiedContainer without an index) when service 'web' has no containers matching the label filters — never created, removed by down/prune, or created but not running and All=false in the listing.","commonSituations":"Running exec in CI before `up -d` finishes; containers exited due to crash loops so they no longer appear in running-only lists; wrong project directory/name scoping the label filters to another project's containers.","solutions":["Confirm state: `docker compose ps -a` — if the service is exited, fix the crash or `docker compose start web`","If missing entirely, create it: `docker compose up -d web`","Verify you're in the right project (directory or -p name) so the label filters match your containers"],"exampleFix":"# before\ndocker compose exec web sh   # service not running\n\n# after\ndocker compose up -d web\ndocker compose exec web sh","handlingStrategy":"validation","validationCode":"list, _ := 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    ),\n})\nif len(list.Items) == 0 { return fmt.Errorf(\"service %s not running; run `up -d` first\", service) }","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"is not running\") {\n    // check `compose ps -a`: start or recreate the service, then retry the command\n}","preventionTips":["Chain commands as `docker compose up -d && docker compose exec ...` so failures stop the chain","For crash-looping services, fix the container exit before exec-based debugging"],"tags":["containers","exec","not-running","service"],"backgroundTag":null,"analyzedSha":"ddc4b044b62e9f715212ea4143fa830fac76382f","analyzedAt":"2026-08-15T13:31:42.319Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}