Skip to content

Commit e8caad1

Browse files
gloursndeloof
authored andcommitted
move dry-run support from alpha to main command
Signed-off-by: Guillaume Lours <[email protected]>
1 parent 77dc9b5 commit e8caad1

File tree

6 files changed

+98
-31
lines changed

6 files changed

+98
-31
lines changed

cmd/compose/alpha.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
package compose
1616

1717
import (
18-
"context"
19-
2018
"github.com/docker/compose/v2/pkg/api"
2119
"github.com/spf13/cobra"
2220
)
@@ -33,26 +31,7 @@ func alphaCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
3331
}
3432
cmd.AddCommand(
3533
watchCommand(p, backend),
36-
dryRunRedirectCommand(p),
3734
vizCommand(p, backend),
3835
)
3936
return cmd
4037
}
41-
42-
// Temporary alpha command as the dry-run will be implemented with a flag
43-
func dryRunRedirectCommand(p *ProjectOptions) *cobra.Command {
44-
cmd := &cobra.Command{
45-
Use: "dry-run -- [COMMAND...]",
46-
Short: "EXPERIMENTAL - Dry run command allow you to test a command without applying changes",
47-
PreRunE: Adapt(func(ctx context.Context, args []string) error {
48-
return nil
49-
}),
50-
RunE: AdaptCmd(func(ctx context.Context, cmd *cobra.Command, args []string) error {
51-
rootCmd := cmd.Root()
52-
rootCmd.SetArgs(append([]string{"compose", "--dry-run"}, args...))
53-
return rootCmd.Execute()
54-
}),
55-
ValidArgsFunction: completeServiceNames(p),
56-
}
57-
return cmd
58-
}

cmd/compose/compose.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,12 @@ func RootCommand(streams command.Cli, backend api.Service) *cobra.Command { //no
399399
c.Flags().StringVar(&ansi, "ansi", "auto", `Control when to print ANSI control characters ("never"|"always"|"auto")`)
400400
c.Flags().IntVar(&parallel, "parallel", -1, `Control max parallelism, -1 for unlimited`)
401401
c.Flags().BoolVarP(&version, "version", "v", false, "Show the Docker Compose version information")
402+
c.PersistentFlags().BoolVar(&dryRun, "dry-run", false, "Execute command in dry run mode")
402403
c.Flags().MarkHidden("version") //nolint:errcheck
403404
c.Flags().BoolVar(&noAnsi, "no-ansi", false, `Do not print ANSI control characters (DEPRECATED)`)
404405
c.Flags().MarkHidden("no-ansi") //nolint:errcheck
405406
c.Flags().BoolVar(&verbose, "verbose", false, "Show more output")
406407
c.Flags().MarkHidden("verbose") //nolint:errcheck
407-
c.Flags().BoolVar(&dryRun, "dry-run", false, "Execute command in dry run mode")
408-
c.Flags().MarkHidden("dry-run") //nolint:errcheck
409408
return c
410409
}
411410

docs/reference/compose.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Define and run multi-container applications with Docker.
4141
|:-----------------------|:--------------|:--------|:----------------------------------------------------------------------------------------------------|
4242
| `--ansi` | `string` | `auto` | Control when to print ANSI control characters ("never"\|"always"\|"auto") |
4343
| `--compatibility` | | | Run compose in backward compatibility mode |
44+
| `--dry-run` | | | Execute command in dry run mode |
4445
| `--env-file` | `stringArray` | | Specify an alternate environment file. |
4546
| `-f`, `--file` | `stringArray` | | Compose configuration files |
4647
| `--parallel` | `int` | `-1` | Control max parallelism, -1 for unlimited |
@@ -169,3 +170,49 @@ If flags are explicitly set on the command line, the associated environment vari
169170

170171
Setting the `COMPOSE_IGNORE_ORPHANS` environment variable to `true` will stop docker compose from detecting orphaned
171172
containers for the project.
173+
174+
175+
### Use Dry Run mode to test your command
176+
177+
Use `--dry-run` flag to test a command without changing your application stack state.
178+
Dry Run mode will show you all the steps Compose will apply by executing the command, for example:
179+
```console
180+
$ docker compose --dry-run up --build -d
181+
[+] Pulling 1/1
182+
✔ DRY-RUN MODE - db Pulled 0.9s
183+
[+] Running 10/8
184+
✔ DRY-RUN MODE - build service backend 0.0s
185+
✔ DRY-RUN MODE - ==> ==> writing image dryRun-754a08ddf8bcb1cf22f310f09206dd783d42f7dd 0.0s
186+
✔ DRY-RUN MODE - ==> ==> naming to nginx-golang-mysql-backend 0.0s
187+
✔ DRY-RUN MODE - Network nginx-golang-mysql_default Created 0.0s
188+
✔ DRY-RUN MODE - Container nginx-golang-mysql-db-1 Created 0.0s
189+
✔ DRY-RUN MODE - Container nginx-golang-mysql-backend-1 Created 0.0s
190+
✔ DRY-RUN MODE - Container nginx-golang-mysql-proxy-1 Created 0.0s
191+
✔ DRY-RUN MODE - Container nginx-golang-mysql-db-1 Healthy 0.5s
192+
✔ DRY-RUN MODE - Container nginx-golang-mysql-backend-1 Started 0.0s
193+
✔ DRY-RUN MODE - Container nginx-golang-mysql-proxy-1 Started Started
194+
```
195+
You could see that the first step will be to pull the image defined by `db` service, then build the `backend` service.
196+
After that, containers will be created, the `db` service started and the `backend` and `proxy` will wait until `db` service is healthy to start.
197+
198+
The Dry Run mode is not supported by all commands, especially by the command which doesn't change the state of a Compose stack
199+
such as `ps`, `ls`, `logs` for example.
200+
201+
Here the list of commands supporting `--dry-run` flag:
202+
* build
203+
* cp
204+
* create
205+
* down
206+
* exec
207+
* kill
208+
* pause
209+
* pull
210+
* push
211+
* remove
212+
* restart
213+
* run
214+
* start
215+
* stop
216+
* unpause
217+
* up
218+

docs/reference/compose_alpha.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ Experimental commands
55

66
### Subcommands
77

8-
| Name | Description |
9-
|:--------------------------------------|:-----------------------------------------------------------------------------------------------------|
10-
| [`dry-run`](compose_alpha_dry-run.md) | EXPERIMENTAL - Dry run command allow you to test a command without applying changes |
11-
| [`viz`](compose_alpha_viz.md) | EXPERIMENTAL - Generate a graphviz graph from your compose file |
12-
| [`watch`](compose_alpha_watch.md) | EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated |
8+
| Name | Description |
9+
|:----------------------------------|:-----------------------------------------------------------------------------------------------------|
10+
| [`viz`](compose_alpha_viz.md) | EXPERIMENTAL - Generate a graphviz graph from your compose file |
11+
| [`watch`](compose_alpha_watch.md) | EXPERIMENTAL - Watch build context for service and rebuild/refresh containers when files are updated |
1312

1413

1514

docs/reference/docker_compose.yaml

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,51 @@ long: |-
117117
118118
Setting the `COMPOSE_IGNORE_ORPHANS` environment variable to `true` will stop docker compose from detecting orphaned
119119
containers for the project.
120+
121+
122+
### Use Dry Run mode to test your command
123+
124+
Use `--dry-run` flag to test a command without changing your application stack state.
125+
Dry Run mode will show you all the steps Compose will apply by executing the command, for example:
126+
```console
127+
$ docker compose --dry-run up --build -d
128+
[+] Pulling 1/1
129+
✔ DRY-RUN MODE - db Pulled 0.9s
130+
[+] Running 10/8
131+
✔ DRY-RUN MODE - build service backend 0.0s
132+
✔ DRY-RUN MODE - ==> ==> writing image dryRun-754a08ddf8bcb1cf22f310f09206dd783d42f7dd 0.0s
133+
✔ DRY-RUN MODE - ==> ==> naming to nginx-golang-mysql-backend 0.0s
134+
✔ DRY-RUN MODE - Network nginx-golang-mysql_default Created 0.0s
135+
✔ DRY-RUN MODE - Container nginx-golang-mysql-db-1 Created 0.0s
136+
✔ DRY-RUN MODE - Container nginx-golang-mysql-backend-1 Created 0.0s
137+
✔ DRY-RUN MODE - Container nginx-golang-mysql-proxy-1 Created 0.0s
138+
✔ DRY-RUN MODE - Container nginx-golang-mysql-db-1 Healthy 0.5s
139+
✔ DRY-RUN MODE - Container nginx-golang-mysql-backend-1 Started 0.0s
140+
✔ DRY-RUN MODE - Container nginx-golang-mysql-proxy-1 Started Started
141+
```
142+
You could see that the first step will be to pull the image defined by `db` service, then build the `backend` service.
143+
After that, containers will be created, the `db` service started and the `backend` and `proxy` will wait until `db` service is healthy to start.
144+
145+
The Dry Run mode is not supported by all commands, especially by the command which doesn't change the state of a Compose stack
146+
such as `ps`, `ls`, `logs` for example.
147+
148+
Here the list of commands supporting `--dry-run` flag:
149+
* build
150+
* cp
151+
* create
152+
* down
153+
* exec
154+
* kill
155+
* pause
156+
* pull
157+
* push
158+
* remove
159+
* restart
160+
* run
161+
* start
162+
* stop
163+
* unpause
164+
* up
120165
usage: docker compose
121166
pname: docker
122167
plink: docker.yaml
@@ -199,7 +244,7 @@ options:
199244
default_value: "false"
200245
description: Execute command in dry run mode
201246
deprecated: false
202-
hidden: true
247+
hidden: false
203248
experimental: false
204249
experimentalcli: false
205250
kubernetes: false

docs/reference/docker_compose_alpha.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ long: Experimental commands
44
pname: docker compose
55
plink: docker_compose.yaml
66
cname:
7-
- docker compose alpha dry-run
87
- docker compose alpha viz
98
- docker compose alpha watch
109
clink:
11-
- docker_compose_alpha_dry-run.yaml
1210
- docker_compose_alpha_viz.yaml
1311
- docker_compose_alpha_watch.yaml
1412
deprecated: false

0 commit comments

Comments
 (0)