Skip to content

Commit

Permalink
[feature:5023] Add '--force' option into container creation command
Browse files Browse the repository at this point in the history
  • Loading branch information
devcoons committed Apr 23, 2024
1 parent 8651906 commit 8ada74d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cli/command/container/create.go
Expand Up @@ -39,6 +39,7 @@ type createOptions struct {
untrusted bool
pull string // always, missing, never
quiet bool
force bool
}

// NewCreateCommand creates a new cobra.Command for `docker create`
Expand Down Expand Up @@ -69,6 +70,7 @@ func NewCreateCommand(dockerCli command.Cli) *cobra.Command {
flags.StringVar(&options.name, "name", "", "Assign a name to the container")
flags.StringVar(&options.pull, "pull", PullImageMissing, `Pull image before creating ("`+PullImageAlways+`", "|`+PullImageMissing+`", "`+PullImageNever+`")`)
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Suppress the pull output")
flags.BoolVarP(&options.force, "force", "f", false, "Force the creation of the container. (automatically removes the instance if already exists)")

// Add an explicit help that doesn't have a `-h` to prevent the conflict
// with hostname
Expand Down Expand Up @@ -250,6 +252,14 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
}
}

if options.force && len(options.name) != 0 {
dockerCli.Client().ContainerRemove(ctx, options.name, container.RemoveOptions{
RemoveVolumes: false,
RemoveLinks: false,
Force: true,
})
}

hostConfig.ConsoleSize[0], hostConfig.ConsoleSize[1] = dockerCli.Out().GetTtySize()

response, err := dockerCli.Client().ContainerCreate(ctx, config, hostConfig, networkingConfig, platform, options.name)
Expand Down

0 comments on commit 8ada74d

Please sign in to comment.