feat: auto-remove container

This commit is contained in:
fengcaiwen
2023-09-30 22:41:34 +08:00
committed by naison
parent 4d10bd4124
commit fb93cb5748
2 changed files with 13 additions and 11 deletions

View File

@@ -226,16 +226,24 @@ func (d *Options) Main(ctx context.Context, tempContainerConfig *containerConfig
type ConfigList []*RunConfig
func (l ConfigList) Remove(ctx context.Context, cli *client.Client) error {
var remove = false
for _, runConfig := range l {
if runConfig.hostConfig.AutoRemove {
remove = true
break
}
}
if !remove {
return nil
}
for _, runConfig := range l {
err := cli.NetworkDisconnect(ctx, runConfig.containerName, runConfig.containerName, true)
if err != nil {
log.Debug(err)
}
if runConfig.hostConfig.AutoRemove {
err = cli.ContainerRemove(ctx, runConfig.containerName, types.ContainerRemoveOptions{Force: true})
if err != nil {
log.Debug(err)
}
err = cli.ContainerRemove(ctx, runConfig.containerName, types.ContainerRemoveOptions{Force: true})
if err != nil {
log.Debug(err)
}
}
i, err := cli.NetworkInspect(ctx, config.ConfigMapPodTrafficManager, types.NetworkInspectOptions{})

View File

@@ -132,12 +132,6 @@ func run(ctx context.Context, runConfig *RunConfig, cli *client.Client, c *comma
func runFirst(ctx context.Context, runConfig *RunConfig, cli *apiclient.Client, dockerCli *command.DockerCli) (id string, err error) {
rand.New(rand.NewSource(time.Now().UnixNano()))
defer func() {
if err != nil && runConfig.hostConfig.AutoRemove {
_ = cli.ContainerRemove(ctx, id, types.ContainerRemoveOptions{Force: true})
}
}()
stdout, stderr := dockerCli.Out(), dockerCli.Err()
client := dockerCli.Client()