Configurable auto remove (#440)

* enable autoremove to be configurable

* bump version
This commit is contained in:
Minghe
2019-12-31 14:29:01 +08:00
committed by GitHub
parent a1ccbd6cab
commit 3d7f7b0ad1
5 changed files with 36 additions and 3 deletions

14
config/env.go Normal file
View File

@@ -0,0 +1,14 @@
package config
import (
"os"
)
// DisableContainerAutoremove to tell if to run container with --rm
var DisableContainerAutoremove = false
func init() {
if os.Getenv("DISABLE_CONTAINER_AUTOREMOVE") == "true" {
DisableContainerAutoremove = true
}
}

17
config/env_test.go Normal file
View File

@@ -0,0 +1,17 @@
package config
import (
"os"
"testing"
)
var _ = func() (_ struct{}) {
os.Setenv("DISABLE_CONTAINER_AUTOREMOVE", "true")
return
}()
func TestEnvLoad(t *testing.T) {
if !DisableContainerAutoremove {
t.Fatalf("should be true after set")
}
}

View File

@@ -23,6 +23,7 @@ import (
"github.com/docker/go-connections/nat"
"github.com/google/go-querystring/query"
"github.com/google/uuid"
fxConfig "github.com/metrue/fx/config"
containerruntimes "github.com/metrue/fx/container_runtimes"
"github.com/metrue/fx/types"
"github.com/metrue/fx/utils"
@@ -402,7 +403,7 @@ func (api *API) StartContainer(ctx context.Context, name string, image string, b
}
hostConfig := &dockerTypesContainer.HostConfig{
AutoRemove: true,
AutoRemove: !fxConfig.DisableContainerAutoremove,
PortBindings: portMap,
}

View File

@@ -18,6 +18,7 @@ import (
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
"github.com/google/uuid"
fxConfig "github.com/metrue/fx/config"
containerruntimes "github.com/metrue/fx/container_runtimes"
"github.com/metrue/fx/types"
"github.com/metrue/fx/utils"
@@ -161,7 +162,7 @@ func (d *Docker) StartContainer(ctx context.Context, name string, image string,
}
hostConfig := &dockerTypesContainer.HostConfig{
AutoRemove: true,
AutoRemove: !fxConfig.DisableContainerAutoremove,
PortBindings: portMap,
}
resp, err := d.ContainerCreate(ctx, config, hostConfig, nil, name)

2
fx.go
View File

@@ -17,7 +17,7 @@ import (
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
const version = "0.8.85"
const version = "0.8.86"
func init() {
go checkForUpdate()