-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.go
More file actions
116 lines (96 loc) · 3.01 KB
/
Copy pathdocker.go
File metadata and controls
116 lines (96 loc) · 3.01 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package executor
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/client"
)
func StartExecutionSession(ctx context.Context, containerId string) (*types.HijackedResponse, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
containerInfo, err := cli.ContainerInspect(ctx, containerId)
if err != nil {
return nil, fmt.Errorf("failed to inspect container: %w", err)
}
if !containerInfo.State.Running {
return nil, fmt.Errorf("container is not running")
}
exec, err := cli.ContainerExecCreate(ctx, containerId, container.ExecOptions{
Cmd: []string{"sh", "-l"},
AttachStderr: true,
AttachStdout: true,
AttachStdin: true,
Tty: true,
Env: []string{"TERM=xterm"},
})
if err != nil {
return nil, fmt.Errorf("error startign the exec %v", err)
}
attach, err := cli.ContainerExecAttach(ctx, exec.ID, container.ExecAttachOptions{})
if err != nil {
return nil, err
}
return &attach, nil
}
func GetSecureContainerConfig(image, source, target, projectID string) (*container.Config, *container.HostConfig) {
config := &container.Config{
Image: image,
WorkingDir: "/home/developer/workspace",
Cmd: []string{"tail", "-f", "/dev/null"},
Tty: true,
OpenStdin: true,
}
hostConfig := &container.HostConfig{
ReadonlyRootfs: true,
Binds: []string{},
Mounts: []mount.Mount{
{
Type: mount.TypeVolume,
Source: "collab-ide_persistence", // The full volume name
Target: "/home/developer/workspace", // Destination inside the terminal container
ReadOnly: false, // Ensure it's read-write (rw)
VolumeOptions: &mount.VolumeOptions{
Subpath: projectID, // 🚀 THE FIX: Targets ONLY this project's subfolder!
},
},
},
Tmpfs: map[string]string{
"/tmp": "rw,exec,size=64m,uid=1000,gid=1000",
"/root": "rw,exec,size=256m",
},
CapDrop: []string{
"ALL",
},
SecurityOpt: []string{"no-new-privileges:true"},
}
return config, hostConfig
}
func CreateAndStartContainer(ctx context.Context, image, source, target, projectID string) (string, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return "", err
}
config, hostConfig := GetSecureContainerConfig(image, source, target, projectID)
resp, err := cli.ContainerCreate(ctx, config, hostConfig, nil, nil, "")
if err != nil {
return "", err
}
if err := cli.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
return "", err
}
return resp.ID, nil
}
func RemoveContainer(containerId string) error {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
return cli.ContainerRemove(ctx, containerId, container.RemoveOptions{
Force: true,
})
}