forked from docker/compose
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: MohammadHasan Akbari <[email protected]>
- Loading branch information
Showing
11 changed files
with
411 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package compose | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/docker/cli/cli/command" | ||
"github.com/docker/cli/opts" | ||
"github.com/docker/compose/v2/pkg/api" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type commitOptions struct { | ||
*ProjectOptions | ||
|
||
service string | ||
reference string | ||
|
||
pause bool | ||
comment string | ||
author string | ||
changes opts.ListOpts | ||
|
||
index int | ||
} | ||
|
||
func commitCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command { | ||
options := commitOptions{ | ||
ProjectOptions: p, | ||
} | ||
cmd := &cobra.Command{ | ||
Use: "commit [OPTIONS] SERVICE [REPOSITORY[:TAG]]", | ||
Short: "Create a new image from a service container's changes", | ||
Args: cobra.RangeArgs(1, 2), | ||
PreRunE: Adapt(func(ctx context.Context, args []string) error { | ||
options.service = args[0] | ||
if len(args) > 1 { | ||
options.reference = args[1] | ||
} | ||
|
||
return nil | ||
}), | ||
RunE: Adapt(func(ctx context.Context, args []string) error { | ||
return runCommit(ctx, dockerCli, backend, options) | ||
}), | ||
ValidArgsFunction: completeServiceNames(dockerCli, p), | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.IntVar(&options.index, "index", 0, "index of the container if service has multiple replicas.") | ||
|
||
flags.BoolVarP(&options.pause, "pause", "p", true, "Pause container during commit") | ||
flags.StringVarP(&options.comment, "message", "m", "", "Commit message") | ||
flags.StringVarP(&options.author, "author", "a", "", `Author (e.g., "John Hannibal Smith <[email protected]>")`) | ||
options.changes = opts.NewListOpts(nil) | ||
flags.VarP(&options.changes, "change", "c", "Apply Dockerfile instruction to the created image") | ||
|
||
return cmd | ||
} | ||
|
||
func runCommit(ctx context.Context, dockerCli command.Cli, backend api.Service, options commitOptions) error { | ||
projectName, err := options.toProjectName(ctx, dockerCli) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
commitOptions := api.CommitOptions{ | ||
Service: options.service, | ||
Reference: options.reference, | ||
Pause: options.pause, | ||
Comment: options.comment, | ||
Author: options.author, | ||
Changes: options.changes, | ||
Index: options.index, | ||
} | ||
|
||
return backend.Commit(ctx, projectName, commitOptions) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# docker compose commit | ||
|
||
<!---MARKER_GEN_START--> | ||
Create a new image from a service container's changes | ||
|
||
### Options | ||
|
||
| Name | Type | Default | Description | | ||
|:------------------|:---------|:--------|:-----------------------------------------------------------| | ||
| `-a`, `--author` | `string` | | Author (e.g., "John Hannibal Smith <[email protected]>") | | ||
| `-c`, `--change` | `list` | | Apply Dockerfile instruction to the created image | | ||
| `--dry-run` | `bool` | | Execute command in dry run mode | | ||
| `--index` | `int` | `0` | index of the container if service has multiple replicas. | | ||
| `-m`, `--message` | `string` | | Commit message | | ||
| `-p`, `--pause` | `bool` | `true` | Pause container during commit | | ||
|
||
|
||
<!---MARKER_GEN_END--> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
command: docker compose commit | ||
short: Create a new image from a service container's changes | ||
long: Create a new image from a service container's changes | ||
usage: docker compose commit [OPTIONS] SERVICE [REPOSITORY[:TAG]] | ||
pname: docker compose | ||
plink: docker_compose.yaml | ||
options: | ||
- option: author | ||
shorthand: a | ||
value_type: string | ||
description: Author (e.g., "John Hannibal Smith <[email protected]>") | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
- option: change | ||
shorthand: c | ||
value_type: list | ||
description: Apply Dockerfile instruction to the created image | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
- option: index | ||
value_type: int | ||
default_value: "0" | ||
description: index of the container if service has multiple replicas. | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
- option: message | ||
shorthand: m | ||
value_type: string | ||
description: Commit message | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
- option: pause | ||
shorthand: p | ||
value_type: bool | ||
default_value: "true" | ||
description: Pause container during commit | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
inherited_options: | ||
- option: dry-run | ||
value_type: bool | ||
default_value: "false" | ||
description: Execute command in dry run mode | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
deprecated: false | ||
hidden: false | ||
experimental: false | ||
experimentalcli: false | ||
kubernetes: false | ||
swarm: false | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
Copyright 2020 Docker Compose CLI authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package compose | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/docker/compose/v2/pkg/api" | ||
"github.com/docker/compose/v2/pkg/progress" | ||
containerType "github.com/docker/docker/api/types/container" | ||
) | ||
|
||
func (s *composeService) Commit(ctx context.Context, projectName string, options api.CommitOptions) error { | ||
return progress.RunWithTitle(ctx, func(ctx context.Context) error { | ||
return s.commit(ctx, projectName, options) | ||
}, s.stdinfo(), "Committing") | ||
} | ||
|
||
func (s *composeService) commit(ctx context.Context, projectName string, options api.CommitOptions) error { | ||
projectName = strings.ToLower(projectName) | ||
|
||
container, err := s.getSpecifiedContainer(ctx, projectName, oneOffInclude, false, options.Service, options.Index) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clnt := s.dockerCli.Client() | ||
|
||
w := progress.ContextWriter(ctx) | ||
|
||
name := getCanonicalContainerName(container) | ||
msg := fmt.Sprintf("Commit %s", name) | ||
|
||
w.Event(progress.Event{ | ||
ID: name, | ||
Text: msg, | ||
Status: progress.Working, | ||
StatusText: "Committing", | ||
}) | ||
|
||
if s.dryRun { | ||
w.Event(progress.Event{ | ||
ID: name, | ||
Text: msg, | ||
Status: progress.Done, | ||
StatusText: "Committed", | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
response, err := clnt.ContainerCommit(ctx, container.ID, containerType.CommitOptions{ | ||
Reference: options.Reference, | ||
Comment: options.Comment, | ||
Author: options.Author, | ||
Changes: options.Changes.GetAll(), | ||
Pause: options.Pause, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w.Event(progress.Event{ | ||
ID: name, | ||
Text: msg, | ||
Status: progress.Done, | ||
StatusText: fmt.Sprintf("Committed as %s", response.ID), | ||
}) | ||
|
||
return nil | ||
} |
Oops, something went wrong.