docker/compose · error
service %q is not running container #%d
Error message
service %q is not running container #%d
What it means
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`.
Source
Thrown at pkg/compose/containers.go:105
return f
}
func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, all bool, serviceName string, containerIndex int) (container.Summary, error) {
defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
if containerIndex > 0 {
defaultFilters.Add("label", containerNumberFilter(containerIndex))
}
res, err := s.apiClient().ContainerList(ctx, client.ContainerListOptions{
Filters: defaultFilters,
All: all,
})
if err != nil {
return container.Summary{}, err
}
containers := res.Items
if len(containers) < 1 {
if containerIndex > 0 {
return container.Summary{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
}
return container.Summary{}, fmt.Errorf("service %q is not running", serviceName)
}
// Sort by container number first, then put one-off containers at the end
sort.Slice(containers, func(i, j int) bool {
numberLabelX, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
numberLabelY, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
IsOneOffLabelTrueX := containers[i].Labels[api.OneoffLabel] == "True"
IsOneOffLabelTrueY := containers[j].Labels[api.OneoffLabel] == "True"
if IsOneOffLabelTrueX || IsOneOffLabelTrueY {
return !IsOneOffLabelTrueX && IsOneOffLabelTrueY
}
return numberLabelX < numberLabelY
})
return containers[0], nilView on GitHub (pinned to ddc4b044b6)
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
Example fix
# before docker compose exec --index 3 web sh # only 2 replicas # after docker compose up -d --scale web=3 docker compose exec --index 3 web sh
Defensive patterns
Strategy: validation
Validate before calling
// before exec --index N, confirm a container with that number label exists
list, _ := dockerCli.Client().ContainerList(ctx, client.ContainerListOptions{
All: true,
Filters: filters.NewArgs(
filters.Arg("label", "com.docker.compose.project="+project),
filters.Arg("label", "com.docker.compose.service="+service),
filters.Arg("label", fmt.Sprintf("com.docker.compose.container-number=%d", index)),
),
})
if len(list.Items) == 0 { return fmt.Errorf("no replica #%d for %s", index, service) } Try / catch
if err != nil && strings.Contains(err.Error(), "is not running container #") {
// re-check replica count (compose ps) and retry without --index or after scaling up
} Prevention
- Query `docker compose ps` for live replica numbers instead of hardcoding --index
- Scale before index-addressed commands in scripts
When it happens
Trigger: `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.
Common situations: 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.
Related errors
- deleting paths in %s: %w
- no container found for project %q: %w
- service %q is not running
- no container(s) found with the following name(s): %s
- not found
AI-assisted analysis of docker/compose@ddc4b044b6 (2026-08-15).
Data as JSON: /api/errors/847aebca17e08e17.
Report an issue: GitHub.