diff --git a/CHANGELOG.md b/CHANGELOG.md index 7eef6433..a899523c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # ArangoDB Starter Changelog ## [master](https://github.com/arangodb-helper/arangodb/tree/master) (N/A) +- Add --docker.force-bind-ports option to add ports for custom networks ## [0.15.8](https://github.com/arangodb-helper/arangodb/tree/0.15.8) (2023-06-02) - Add passing ARANGODB_SERVER_DIR env variable when starting arangod instances diff --git a/main.go b/main.go index 1ed14df1..e1bf264d 100644 --- a/main.go +++ b/main.go @@ -188,6 +188,7 @@ func init() { f.BoolVar(&opts.docker.netHost, "docker.net-host", false, "Run containers with --net=host") f.Lookup("docker.net-host").Deprecated = "use --docker.net-mode=host instead" f.StringVar(&opts.docker.networkMode, "docker.net-mode", "", "Run containers with --net=") + f.BoolVar(&opts.docker.forceBindPorts, "docker.force-bind-ports", false, "If true, container will publish ports even on custom Docker network") f.BoolVar(&opts.docker.privileged, "docker.privileged", false, "Run containers with --privileged") f.BoolVar(&opts.docker.tty, "docker.tty", true, "Run containers with TTY enabled") @@ -732,6 +733,7 @@ func mustPrepareService(generateAutoKeyFile bool) (*service.Service, service.Boo DockerUser: opts.docker.user, DockerGCDelay: opts.docker.gcDelay, DockerNetworkMode: opts.docker.networkMode, + DockerForceBindPorts: opts.docker.forceBindPorts, DockerPrivileged: opts.docker.privileged, DockerTTY: opts.docker.tty, ProjectVersion: projectVersion, diff --git a/options.go b/options.go index 540395bb..4f91cb07 100644 --- a/options.go +++ b/options.go @@ -95,6 +95,7 @@ type starterOptions struct { gcDelay time.Duration netHost bool // Deprecated networkMode string + forceBindPorts bool privileged bool tty bool } diff --git a/service/runner_docker.go b/service/runner_docker.go index 051244f6..d0126c02 100644 --- a/service/runner_docker.go +++ b/service/runner_docker.go @@ -52,7 +52,7 @@ const ( // NewDockerRunner creates a runner that starts processes in a docker container. func NewDockerRunner(log zerolog.Logger, endpoint, arangodImage, arangoSyncImage string, imagePullPolicy ImagePullPolicy, user, volumesFrom string, gcDelay time.Duration, - networkMode string, privileged, tty bool) (Runner, error) { + networkMode string, forceBindPorts, privileged, tty bool) (Runner, error) { os.Setenv("DOCKER_HOST", endpoint) client, err := docker.NewClientFromEnv() @@ -70,6 +70,7 @@ func NewDockerRunner(log zerolog.Logger, endpoint, arangodImage, arangoSyncImage containerIDs: make(map[string]time.Time), gcDelay: gcDelay, networkMode: networkMode, + forceBindPorts: forceBindPorts, privileged: privileged, tty: tty, }, nil @@ -89,6 +90,7 @@ type dockerRunner struct { gcOnce sync.Once gcDelay time.Duration networkMode string + forceBindPorts bool privileged bool tty bool } @@ -271,17 +273,11 @@ func (r *dockerRunner) start(image string, command string, args []string, envs m } if r.networkMode != "" && r.networkMode != "default" { opts.HostConfig.NetworkMode = r.networkMode - } else { - for _, p := range ports { - dockerPort := docker.Port(fmt.Sprintf("%d/tcp", p)) - opts.Config.ExposedPorts[dockerPort] = struct{}{} - opts.HostConfig.PortBindings[dockerPort] = []docker.PortBinding{ - { - HostIP: "0.0.0.0", - HostPort: strconv.Itoa(p), - }, - } + if r.forceBindPorts { + r.addPortBindings(ports, &opts) } + } else { + r.addPortBindings(ports, &opts) } r.log.Debug().Msgf("Creating container %s", containerName) c, err := r.client.CreateContainer(opts) @@ -335,6 +331,19 @@ func (r *dockerRunner) start(image string, command string, args []string, envs m }, nil } +func (r *dockerRunner) addPortBindings(ports []int, opts *docker.CreateContainerOptions) { + for _, p := range ports { + dockerPort := docker.Port(fmt.Sprintf("%d/tcp", p)) + opts.Config.ExposedPorts[dockerPort] = struct{}{} + opts.HostConfig.PortBindings[dockerPort] = []docker.PortBinding{ + { + HostIP: "0.0.0.0", + HostPort: strconv.Itoa(p), + }, + } + } +} + // imageExists looks for a local image and returns true it it exists, false otherwise. func (r *dockerRunner) imageExists(ctx context.Context, image string) (bool, error) { found := false diff --git a/service/service.go b/service/service.go index 8e85e53a..aa24fbbc 100644 --- a/service/service.go +++ b/service/service.go @@ -91,6 +91,7 @@ type Config struct { DockerUser string DockerGCDelay time.Duration DockerNetworkMode string + DockerForceBindPorts bool DockerPrivileged bool DockerTTY bool RunningInDocker bool @@ -164,7 +165,7 @@ func (c Config) CreateRunner(log zerolog.Logger) (Runner, Config, bool) { if c.UseDockerRunner() { runner, err := NewDockerRunner(log, c.DockerEndpoint, c.DockerArangodImage, c.DockerArangoSyncImage, c.DockerImagePullPolicy, c.DockerUser, c.DockerContainerName, - c.DockerGCDelay, c.DockerNetworkMode, c.DockerPrivileged, c.DockerTTY) + c.DockerGCDelay, c.DockerNetworkMode, c.DockerForceBindPorts, c.DockerPrivileged, c.DockerTTY) if err != nil { log.Fatal().Err(err).Msg("Failed to create docker runner") }