-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDockerSvc.go
88 lines (73 loc) · 2.32 KB
/
DockerSvc.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package containersvc
import (
"context"
"fmt"
"log"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
)
type DockerService struct {
cli *client.Client
}
// type Neo4jGraphRepo struct {
// drv neo4j.DriverWithContext
// }
func NewDockerService() (*DockerService, error) {
// Create a new Docker client
client, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, fmt.Errorf("failed to create Docker client: %v", err)
}
return &DockerService{cli: client}, nil
}
func (d *DockerService) CreateContainer() error {
// // Create a new Docker client
// cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
// if err != nil {
// return fmt.Errorf("failed to create Docker client: %v", err)
// }
ctx := context.Background()
// Define the ClickHouse container options
containerName := "xray-clickhouse-server"
imageName := "clickhouse/clickhouse-server:latest"
containerConfig := &container.Config{
Image: imageName,
Cmd: []string{"/entrypoint.sh"}, // Specify the default entrypoint
ExposedPorts: nat.PortSet{
"9000/tcp": {},
"8123/tcp": {},
},
}
hostConfig := &container.HostConfig{
PortBindings: map[nat.Port][]nat.PortBinding{
"9000/tcp": {{HostPort: "9001"}},
"8123/tcp": {{HostPort: "8124"}},
},
}
// Pull options with authentication
pullOpts := types.ImagePullOptions{
All: true,
}
_, err := d.cli.ImagePull(ctx, imageName, pullOpts)
if err != nil {
return fmt.Errorf("failed to pull ClickHouse image: %v", err)
}
// Wait a moment to ensure the pull has completed
time.Sleep(2 * time.Second)
// Create the container
log.Println("Creating ClickHouse container...")
resp, err := d.cli.ContainerCreate(ctx, containerConfig, hostConfig, nil, nil, containerName)
if err != nil {
return fmt.Errorf("failed to create ClickHouse container: %v", err)
}
// Start the container
log.Println("Starting ClickHouse container...")
if err := d.cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return fmt.Errorf("failed to start ClickHouse container: %v", err)
}
log.Printf("ClickHouse server is now running in container '%s'.\n", containerName)
return nil
}