{"record":{"id":"4d14ad05a5807296","repo":"docker/compose","slug":"index-requires-one-service-to-be-selected","errorCode":null,"errorMessage":"--index requires one service to be selected","messagePattern":"--index requires one service to be selected","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/compose/logs.go","lineNumber":57,"sourceCode":"\tuntil      string\n\tnoColor    bool\n\tnoPrefix   bool\n\ttimestamps bool\n}\n\nfunc logsCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *BackendOptions) *cobra.Command {\n\topts := logsOptions{\n\t\tProjectOptions: p,\n\t}\n\tlogsCmd := &cobra.Command{\n\t\tUse:   \"logs [OPTIONS] [SERVICE...]\",\n\t\tShort: \"View output from containers\",\n\t\tRunE: Adapt(func(ctx context.Context, args []string) error {\n\t\t\treturn runLogs(ctx, dockerCli, backendOptions, opts, args)\n\t\t}),\n\t\tPreRunE: func(cmd *cobra.Command, args []string) error {\n\t\t\tif opts.index > 0 && len(args) != 1 {\n\t\t\t\treturn errors.New(\"--index requires one service to be selected\")\n\t\t\t}\n\t\t\treturn nil\n\t\t},\n\t\tValidArgsFunction: completeServiceNames(dockerCli, p),\n\t}\n\tflags := logsCmd.Flags()\n\tflags.BoolVarP(&opts.follow, \"follow\", \"f\", false, \"Follow log output\")\n\tflags.SetAnnotation(\"follow\", annotation.ExternalURL, []string{\"https://docs.docker.com/reference/cli/docker/container/logs/#follow\"}) //nolint:errcheck\n\tflags.IntVar(&opts.index, \"index\", 0, \"index of the container if service has multiple replicas\")\n\tflags.StringVar(&opts.since, \"since\", \"\", \"Show logs since timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)\")\n\tflags.SetAnnotation(\"since\", annotation.ExternalURL, []string{\"https://docs.docker.com/reference/cli/docker/container/logs/#since\"}) //nolint:errcheck\n\tflags.StringVar(&opts.until, \"until\", \"\", \"Show logs before a timestamp (e.g. 2013-01-02T13:23:37Z) or relative (e.g. 42m for 42 minutes)\")\n\tflags.SetAnnotation(\"until\", annotation.ExternalURL, []string{\"https://docs.docker.com/reference/cli/docker/container/logs/#until\"}) //nolint:errcheck\n\tflags.BoolVar(&opts.noColor, \"no-color\", false, \"Produce monochrome output\")\n\tflags.BoolVar(&opts.noPrefix, \"no-log-prefix\", false, \"Don't print prefix in logs\")\n\tflags.BoolVarP(&opts.timestamps, \"timestamps\", \"t\", false, \"Show timestamps\")\n\tflags.SetAnnotation(\"timestamps\", annotation.ExternalURL, []string{\"https://docs.docker.com/reference/cli/docker/container/logs/#timestamps\"}) //nolint:errcheck\n\tflags.StringVarP(&opts.tail, \"tail\", \"n\", \"all\", \"Number of lines to show from the end of the logs for each container\")","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/docker/compose/blob/ddc4b044b62e9f715212ea4143fa830fac76382f/cmd/compose/logs.go#L39-L75","documentation":"docker compose logs --index N selects one specific replica of a service by ordinal, which is only meaningful when exactly one service is named. The PreRunE in cmd/compose/logs.go rejects the combination when opts.index > 0 and the number of positional SERVICE arguments is not exactly 1 (zero or 2+ services).","triggerScenarios":"`docker compose logs --index 2` with no service argument, or `docker compose logs --index 1 web db` with two services. Note --index 0 (the default) never triggers the error because the guard requires index > 0.","commonSituations":"Scripts that always pass --index assuming it applies to a previously selected service; scaling a service with --scale and then inspecting logs while forgetting to name the service; copy-pasting a command template that appends multiple service names.","solutions":["Name exactly one service when using --index: `docker compose logs --index 2 web`.","Drop --index if you want logs from all services or all replicas.","For a single-replica service, plain `docker compose logs web` is sufficient."],"exampleFix":"# before\ndocker compose logs --index 1 web db\n\n# after\ndocker compose logs --index 1 web","handlingStrategy":"validation","validationCode":"# exactly one SERVICE argument when INDEX > 0\nif [ \"$INDEX\" -gt 0 ] 2>/dev/null && [ \"$#\" -ne 1 ]; then\n  echo \"--index requires exactly one SERVICE argument\" >&2\n  exit 1\nfi\ndocker compose logs --index \"$INDEX\" \"$@\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only reach for --index on scaled services with a single service named.","Default index to 0/omit it in shared script templates."],"tags":["cli","logs","flags","validation"],"backgroundTag":null,"analyzedSha":"ddc4b044b62e9f715212ea4143fa830fac76382f","analyzedAt":"2026-08-15T13:31:42.319Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}