Skip to content

Commit 5fdcaa0

Browse files
authored
Merge pull request docker#10529 from glours/dry-run-up-support
add dry-run support to up command
2 parents 01afe52 + 2e4faf8 commit 5fdcaa0

File tree

2 files changed

+57
-13
lines changed

2 files changed

+57
-13
lines changed

pkg/api/dryrunclient.go

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ type DryRunKey struct{}
5858

5959
// DryRunClient implements APIClient by delegating to implementation functions. This allows lazy init and per-method overrides
6060
type DryRunClient struct {
61-
apiClient client.APIClient
62-
execs sync.Map
63-
resolver *imagetools.Resolver
61+
apiClient client.APIClient
62+
containers []moby.Container
63+
execs sync.Map
64+
resolver *imagetools.Resolver
6465
}
6566

6667
type execDetails struct {
@@ -79,9 +80,10 @@ func NewDryRunClient(apiClient client.APIClient, cli *command.DockerCli) (*DryRu
7980
return nil, err
8081
}
8182
return &DryRunClient{
82-
apiClient: apiClient,
83-
execs: sync.Map{},
84-
resolver: imagetools.New(configFile),
83+
apiClient: apiClient,
84+
containers: []moby.Container{},
85+
execs: sync.Map{},
86+
resolver: imagetools.New(configFile),
8587
}, nil
8688
}
8789

@@ -99,15 +101,36 @@ func (d *DryRunClient) ContainerAttach(ctx context.Context, container string, op
99101

100102
func (d *DryRunClient) ContainerCreate(ctx context.Context, config *containerType.Config, hostConfig *containerType.HostConfig,
101103
networkingConfig *network.NetworkingConfig, platform *specs.Platform, containerName string) (containerType.CreateResponse, error) {
102-
return containerType.CreateResponse{ID: "dryRunId"}, nil
104+
d.containers = append(d.containers, moby.Container{
105+
ID: containerName,
106+
Names: []string{containerName},
107+
Labels: config.Labels,
108+
HostConfig: struct {
109+
NetworkMode string `json:",omitempty"`
110+
}{},
111+
})
112+
return containerType.CreateResponse{ID: containerName}, nil
103113
}
104114

105115
func (d *DryRunClient) ContainerInspect(ctx context.Context, container string) (moby.ContainerJSON, error) {
106116
containerJSON, err := d.apiClient.ContainerInspect(ctx, container)
107117
if err != nil {
118+
id := "dryRunId"
119+
for _, c := range d.containers {
120+
if c.ID == container {
121+
id = container
122+
}
123+
}
108124
return moby.ContainerJSON{
109125
ContainerJSONBase: &moby.ContainerJSONBase{
110-
ID: "dryRunId",
126+
ID: id,
127+
Name: container,
128+
State: &moby.ContainerState{
129+
Status: "running", // needed for --wait option
130+
Health: &moby.Health{
131+
Status: moby.Healthy, // needed for healthcheck control
132+
},
133+
},
111134
},
112135
Mounts: nil,
113136
Config: &containerType.Config{},
@@ -121,6 +144,21 @@ func (d *DryRunClient) ContainerKill(ctx context.Context, container, signal stri
121144
return nil
122145
}
123146

147+
func (d *DryRunClient) ContainerList(ctx context.Context, options moby.ContainerListOptions) ([]moby.Container, error) {
148+
caller := getCallingFunction()
149+
switch caller {
150+
case "start":
151+
return d.containers, nil
152+
case "getContainers":
153+
if len(d.containers) == 0 {
154+
var err error
155+
d.containers, err = d.apiClient.ContainerList(ctx, options)
156+
return d.containers, err
157+
}
158+
}
159+
return d.apiClient.ContainerList(ctx, options)
160+
}
161+
124162
func (d *DryRunClient) ContainerPause(ctx context.Context, container string) error {
125163
return nil
126164
}
@@ -246,7 +284,13 @@ func (d *DryRunClient) NetworkRemove(ctx context.Context, networkName string) er
246284
}
247285

248286
func (d *DryRunClient) VolumeCreate(ctx context.Context, options volume.CreateOptions) (volume.Volume, error) {
249-
return volume.Volume{}, ErrNotImplemented
287+
return volume.Volume{
288+
ClusterVolume: nil,
289+
Driver: options.Driver,
290+
Labels: options.Labels,
291+
Name: options.Name,
292+
Options: options.DriverOpts,
293+
}, nil
250294
}
251295

252296
func (d *DryRunClient) VolumeRemove(ctx context.Context, volumeID string, force bool) error {
@@ -324,10 +368,6 @@ func (d *DryRunClient) ContainerInspectWithRaw(ctx context.Context, container st
324368
return d.apiClient.ContainerInspectWithRaw(ctx, container, getSize)
325369
}
326370

327-
func (d *DryRunClient) ContainerList(ctx context.Context, options moby.ContainerListOptions) ([]moby.Container, error) {
328-
return d.apiClient.ContainerList(ctx, options)
329-
}
330-
331371
func (d *DryRunClient) ContainerLogs(ctx context.Context, container string, options moby.ContainerLogsOptions) (io.ReadCloser, error) {
332372
return d.apiClient.ContainerLogs(ctx, container, options)
333373
}

pkg/compose/up.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func (s *composeService) Up(ctx context.Context, project *types.Project, options
4848
if options.Start.Attach == nil {
4949
return err
5050
}
51+
if s.dryRun {
52+
fmt.Fprintln(s.stdout(), "end of 'compose up' output, interactive run is not supported in dry-run mode")
53+
return err
54+
}
5155

5256
printer := newLogPrinter(options.Start.Attach)
5357

0 commit comments

Comments
 (0)