/
gui.go
67 lines (64 loc) · 1.84 KB
/
gui.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package docker
import (
"context"
"fmt"
"log"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
)
// CreateAndRunGUIContainer creates and runs a container for the GUI
func (d DockerClient) CreateAndRunGUIContainer(namespace, image string, port int) error {
// I prepare the container name...
containerName := fmt.Sprintf("%s-gui", namespace)
// ... I prepare the host configuration to open a port...
hostConfig := &container.HostConfig{
PortBindings: nat.PortMap{
"80/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: fmt.Sprintf("%d", port),
},
},
},
}
// ... I prepare the exposed ports...
exposedPorts := nat.PortSet{
"80/tcp": struct{}{},
}
// ... I prepare the container configuration, passing the image, name and ports...
config := &container.Config{
Image: image,
Hostname: containerName,
ExposedPorts: exposedPorts,
}
// ... and I create the container, with the container, host and network configuration
cont, err := d.client.ContainerCreate(
context.Background(),
config,
hostConfig,
nil,
nil,
containerName,
)
// If I get an error...
if err != nil {
// ... and this error is due to a duplicate container...
if strings.HasPrefix(err.Error(), "Error response from daemon: Conflict.") {
// ... I remove the duplicate...
log.Printf("Found container \"%s\", recreating", containerName)
d.RemoveContainer(containerName)
// ... and I restart the creation
return d.CreateAndRunGUIContainer(namespace, image, port)
}
return err
}
// Finally, I start the container
err = d.client.ContainerStart(context.Background(), cont.ID, types.ContainerStartOptions{})
if err != nil {
return err
}
log.Printf("Created GUI container \"%s\" with ID %s", containerName, cont.ID)
return nil
}