Skip to content

Commit e90df62

Browse files
authored
Merge pull request docker#10763 from ndeloof/exec_index
when --index is not set select first service container
2 parents be22bc7 + b0af2de commit e90df62

File tree

11 files changed

+41
-43
lines changed

11 files changed

+41
-43
lines changed

cmd/compose/cp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ func copyCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
6464
}
6565

6666
flags := copyCmd.Flags()
67-
flags.IntVar(&opts.index, "index", 0, "Index of the container if there are multiple instances of a service .")
68-
flags.BoolVar(&opts.all, "all", false, "Copy to all the containers of the service.")
67+
flags.IntVar(&opts.index, "index", 0, "index of the container if service has multiple replicas")
68+
flags.BoolVar(&opts.all, "all", false, "copy to all the containers of the service.")
6969
flags.MarkHidden("all") //nolint:errcheck
70-
flags.MarkDeprecated("all", "By default all the containers of the service will get the source file/directory to be copied.") //nolint:errcheck
70+
flags.MarkDeprecated("all", "by default all the containers of the service will get the source file/directory to be copied.") //nolint:errcheck
7171
flags.BoolVarP(&opts.followLink, "follow-link", "L", false, "Always follow symbol link in SRC_PATH")
7272
flags.BoolVarP(&opts.copyUIDGID, "archive", "a", false, "Archive mode (copy all uid/gid information)")
7373

cmd/compose/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func execCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *c
6565

6666
runCmd.Flags().BoolVarP(&opts.detach, "detach", "d", false, "Detached mode: Run command in the background.")
6767
runCmd.Flags().StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables")
68-
runCmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if there are multiple instances of a service [default: 1].")
68+
runCmd.Flags().IntVar(&opts.index, "index", 0, "index of the container if service has multiple replicas")
6969
runCmd.Flags().BoolVarP(&opts.privileged, "privileged", "", false, "Give extended privileges to the process.")
7070
runCmd.Flags().StringVarP(&opts.user, "user", "u", "", "Run the command as this user.")
7171
runCmd.Flags().BoolVarP(&opts.noTty, "no-TTY", "T", !streams.Out().IsTerminal(), "Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY.")

cmd/compose/port.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func portCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *c
5757
ValidArgsFunction: completeServiceNames(p),
5858
}
5959
cmd.Flags().StringVar(&opts.protocol, "protocol", "tcp", "tcp or udp")
60-
cmd.Flags().IntVar(&opts.index, "index", 1, "index of the container if service has multiple replicas")
60+
cmd.Flags().IntVar(&opts.index, "index", 0, "index of the container if service has multiple replicas")
6161
return cmd
6262
}
6363

docs/reference/compose_cp.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ Copy files/folders between a service container and the local filesystem
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:----------------------|:------|:--------|:----------------------------------------------------------------------|
10-
| `-a`, `--archive` | | | Archive mode (copy all uid/gid information) |
11-
| `--dry-run` | | | Execute command in dry run mode |
12-
| `-L`, `--follow-link` | | | Always follow symbol link in SRC_PATH |
13-
| `--index` | `int` | `0` | Index of the container if there are multiple instances of a service . |
8+
| Name | Type | Default | Description |
9+
|:----------------------|:------|:--------|:--------------------------------------------------------|
10+
| `-a`, `--archive` | | | Archive mode (copy all uid/gid information) |
11+
| `--dry-run` | | | Execute command in dry run mode |
12+
| `-L`, `--follow-link` | | | Always follow symbol link in SRC_PATH |
13+
| `--index` | `int` | `0` | index of the container if service has multiple replicas |
1414

1515

1616
<!---MARKER_GEN_END-->

docs/reference/compose_exec.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ Execute a command in a running container.
55

66
### Options
77

8-
| Name | Type | Default | Description |
9-
|:------------------|:--------------|:--------|:----------------------------------------------------------------------------------|
10-
| `-d`, `--detach` | | | Detached mode: Run command in the background. |
11-
| `--dry-run` | | | Execute command in dry run mode |
12-
| `-e`, `--env` | `stringArray` | | Set environment variables |
13-
| `--index` | `int` | `1` | index of the container if there are multiple instances of a service [default: 1]. |
14-
| `-T`, `--no-TTY` | | | Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY. |
15-
| `--privileged` | | | Give extended privileges to the process. |
16-
| `-u`, `--user` | `string` | | Run the command as this user. |
17-
| `-w`, `--workdir` | `string` | | Path to workdir directory for this command. |
8+
| Name | Type | Default | Description |
9+
|:------------------|:--------------|:--------|:---------------------------------------------------------------------------------|
10+
| `-d`, `--detach` | | | Detached mode: Run command in the background. |
11+
| `--dry-run` | | | Execute command in dry run mode |
12+
| `-e`, `--env` | `stringArray` | | Set environment variables |
13+
| `--index` | `int` | `0` | index of the container if service has multiple replicas |
14+
| `-T`, `--no-TTY` | | | Disable pseudo-TTY allocation. By default `docker compose exec` allocates a TTY. |
15+
| `--privileged` | | | Give extended privileges to the process. |
16+
| `-u`, `--user` | `string` | | Run the command as this user. |
17+
| `-w`, `--workdir` | `string` | | Path to workdir directory for this command. |
1818

1919

2020
<!---MARKER_GEN_END-->

docs/reference/compose_port.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Print the public port for a port binding.
88
| Name | Type | Default | Description |
99
|:-------------|:---------|:--------|:--------------------------------------------------------|
1010
| `--dry-run` | | | Execute command in dry run mode |
11-
| `--index` | `int` | `1` | index of the container if service has multiple replicas |
11+
| `--index` | `int` | `0` | index of the container if service has multiple replicas |
1212
| `--protocol` | `string` | `tcp` | tcp or udp |
1313

1414

docs/reference/docker_compose_cp.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ options:
1010
- option: all
1111
value_type: bool
1212
default_value: "false"
13-
description: Copy to all the containers of the service.
13+
description: copy to all the containers of the service.
1414
deprecated: true
1515
hidden: true
1616
experimental: false
@@ -42,8 +42,7 @@ options:
4242
- option: index
4343
value_type: int
4444
default_value: "0"
45-
description: |
46-
Index of the container if there are multiple instances of a service .
45+
description: index of the container if service has multiple replicas
4746
deprecated: false
4847
hidden: false
4948
experimental: false

docs/reference/docker_compose_exec.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ options:
3333
swarm: false
3434
- option: index
3535
value_type: int
36-
default_value: "1"
37-
description: |
38-
index of the container if there are multiple instances of a service [default: 1].
36+
default_value: "0"
37+
description: index of the container if service has multiple replicas
3938
deprecated: false
4039
hidden: false
4140
experimental: false

docs/reference/docker_compose_port.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plink: docker_compose.yaml
77
options:
88
- option: index
99
value_type: int
10-
default_value: "1"
10+
default_value: "0"
1111
description: index of the container if service has multiple replicas
1212
deprecated: false
1313
hidden: false

pkg/compose/containers.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"sort"
23+
"strconv"
2324

2425
"github.com/docker/compose/v2/pkg/api"
2526
"github.com/docker/compose/v2/pkg/utils"
@@ -72,7 +73,9 @@ func getDefaultFilters(projectName string, oneOff oneOff, selectedServices ...st
7273

7374
func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName string, oneOff oneOff, stopped bool, serviceName string, containerIndex int) (moby.Container, error) {
7475
defaultFilters := getDefaultFilters(projectName, oneOff, serviceName)
75-
defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
76+
if containerIndex > 0 {
77+
defaultFilters = append(defaultFilters, containerNumberFilter(containerIndex))
78+
}
7679
containers, err := s.apiClient().ContainerList(ctx, moby.ContainerListOptions{
7780
Filters: filters.NewArgs(
7881
defaultFilters...,
@@ -83,8 +86,16 @@ func (s *composeService) getSpecifiedContainer(ctx context.Context, projectName
8386
return moby.Container{}, err
8487
}
8588
if len(containers) < 1 {
86-
return moby.Container{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
89+
if containerIndex > 0 {
90+
return moby.Container{}, fmt.Errorf("service %q is not running container #%d", serviceName, containerIndex)
91+
}
92+
return moby.Container{}, fmt.Errorf("service %q is not running", serviceName)
8793
}
94+
sort.Slice(containers, func(i, j int) bool {
95+
x, _ := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
96+
y, _ := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
97+
return x < y
98+
})
8899
container := containers[0]
89100
return container, nil
90101
}

0 commit comments

Comments
 (0)