-
-
Save viggy28/5b524baf005d029e4bad2ec16cb09dca to your computer and use it in GitHub Desktop.
Run a custom Docker container with port 8080 and environment variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"context" | |
"fmt" | |
"github.com/docker/docker/client" | |
natting "github.com/docker/go-connections/nat" | |
"github.com/docker/docker/api/types/container" | |
network "github.com/docker/docker/api/types/network" | |
"github.com/docker/docker/api/types" | |
) | |
func runContainer(client *client.Client, imagename string, containername string, port string, inputEnv []string) error { | |
// Define a PORT opening | |
newport, err := natting.NewPort("tcp", port) | |
if err != nil { | |
fmt.Println("Unable to create docker port") | |
return err | |
} | |
// Configured hostConfig: | |
// https://godoc.org/github.com/docker/docker/api/types/container#HostConfig | |
hostConfig := &container.HostConfig{ | |
PortBindings: natting.PortMap{ | |
newport: []natting.PortBinding{ | |
{ | |
HostIP: "0.0.0.0", | |
HostPort: port, | |
}, | |
}, | |
}, | |
RestartPolicy: container.RestartPolicy{ | |
Name: "always", | |
}, | |
LogConfig: container.LogConfig{ | |
Type: "json-file", | |
Config: map[string]string{}, | |
}, | |
} | |
// Define Network config (why isn't PORT in here...?: | |
// https://godoc.org/github.com/docker/docker/api/types/network#NetworkingConfig | |
networkConfig := &network.NetworkingConfig{ | |
EndpointsConfig: map[string]*network.EndpointSettings{}, | |
} | |
gatewayConfig := &network.EndpointSettings{ | |
Gateway: "gatewayname", | |
} | |
networkConfig.EndpointsConfig["bridge"] = gatewayConfig | |
// Define ports to be exposed (has to be same as hostconfig.portbindings.newport) | |
exposedPorts := map[natting.Port]struct{}{ | |
newport: struct{}{}, | |
} | |
// Configuration | |
// https://godoc.org/github.com/docker/docker/api/types/container#Config | |
config := &container.Config{ | |
Image: imagename, | |
Env: inputEnv, | |
ExposedPorts: exposedPorts, | |
Hostname: fmt.Sprintf("%s-hostnameexample", imagename), | |
} | |
// Creating the actual container. This is "nil,nil,nil" in every example. | |
cont, err := client.ContainerCreate( | |
context.Background(), | |
config, | |
hostConfig, | |
networkConfig, | |
containername, | |
) | |
if err != nil { | |
log.Println(err) | |
return err | |
} | |
// Run the actual container | |
client.ContainerStart(context.Background(), cont.ID, types.ContainerStartOptions{}) | |
log.Printf("Container %s is created", cont.ID) | |
return nil | |
} | |
func main() { | |
cli, err := client.NewEnvClient() | |
if err != nil { | |
log.Fatalf("Unable to create docker client") | |
} | |
imagename := "imagename" | |
containername := "containername" | |
portopening := "8080" | |
inputEnv := []string{fmt.Sprintf("LISTENINGPORT=%s", portopening)} | |
err = runContainer(cli, imagename, containername, portopening, inputEnv) | |
if err != nil { | |
log.Println(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment