Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --docker.force-bind-ports option to add ports for custom networks #365

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=<value>")
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")

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type starterOptions struct {
gcDelay time.Duration
netHost bool // Deprecated
networkMode string
forceBindPorts bool
privileged bool
tty bool
}
Expand Down
31 changes: 20 additions & 11 deletions service/runner_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -89,6 +90,7 @@ type dockerRunner struct {
gcOnce sync.Once
gcDelay time.Duration
networkMode string
forceBindPorts bool
privileged bool
tty bool
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type Config struct {
DockerUser string
DockerGCDelay time.Duration
DockerNetworkMode string
DockerForceBindPorts bool
DockerPrivileged bool
DockerTTY bool
RunningInDocker bool
Expand Down Expand Up @@ -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")
}
Expand Down