-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
166 lines (137 loc) · 5.3 KB
/
main.go
File metadata and controls
166 lines (137 loc) · 5.3 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)
// getContainerFromID retrieves container information by container ID
func getContainerFromID(cli *client.Client, containerID string) (*types.ContainerJSON, error) {
ctx := context.Background()
// Get detailed container information
containerInfo, err := cli.ContainerInspect(ctx, containerID)
if err != nil {
return nil, fmt.Errorf("failed to inspect container %s: %v", containerID, err)
}
return &containerInfo, nil
}
// getStateJSON fetches the state.json file from the Docker runtime directory
func getStateJSON(containerID string) ([]byte, error) {
// Construct the path to the state.json file
statePath := filepath.Join("/var/run/docker/runtime-runc/moby", containerID, "state.json")
// Check if the file exists
if _, err := os.Stat(statePath); os.IsNotExist(err) {
return nil, fmt.Errorf("state.json file not found at %s", statePath)
}
// Read the state.json file
stateData, err := os.ReadFile(statePath)
if err != nil {
return nil, fmt.Errorf("failed to read state.json file: %v", err)
}
return stateData, nil
}
// createAndStartContainer creates a new container from an image and starts it
// If sourceContainerID is provided, the new container will share namespaces with the source
func createAndStartContainer(cli *client.Client, imageName string, containerName string, sourceContainerID string, cmd []string, interactive bool) (string, error) {
ctx := context.Background()
hostConfig := &container.HostConfig{
AutoRemove: false,
}
// If source container ID is provided, share namespaces with it
if sourceContainerID != "" {
hostConfig.PidMode = container.PidMode("container:" + sourceContainerID)
hostConfig.NetworkMode = container.NetworkMode("container:" + sourceContainerID)
hostConfig.IpcMode = container.IpcMode("container:" + sourceContainerID)
}
config := &container.Config{
Image: imageName,
Cmd: cmd,
AttachStdin: interactive,
AttachStdout: interactive,
AttachStderr: interactive,
OpenStdin: interactive,
StdinOnce: false,
Tty: interactive,
}
// Create the container
resp, err := cli.ContainerCreate(ctx, config, hostConfig, nil, nil, containerName)
if err != nil {
return "", fmt.Errorf("failed to create container: %v", err)
}
containerID := resp.ID
fmt.Printf("Created container: %s (ID: %s)\n", containerName, containerID)
// Start the container
err = cli.ContainerStart(ctx, containerID, container.StartOptions{})
if err != nil {
return "", fmt.Errorf("failed to start container %s: %v", containerID, err)
}
fmt.Printf("Successfully started container: %s\n", containerID)
return containerID, nil
}
func copyStateJSON(cli *client.Client, source string, destination string, stateJSON []byte) error {
ctx := context.Background()
destStatePath := filepath.Join("/var/run/docker/runtime-runc/moby", destination, "state.json")
err := os.WriteFile(destStatePath, stateJSON, 0644)
if err != nil {
return fmt.Errorf("failed to write state.json to destination container %s: %v", destination, err)
}
fmt.Printf("Copied state.json from %s to %s\n", source, destination)
// Start the destination container with the new state
err = cli.ContainerStart(ctx, destination, container.StartOptions{})
if err != nil {
return fmt.Errorf("failed to start destination container %s: %v", destination, err)
}
fmt.Printf("Started destination container with copied state: %s\n", destination)
return nil
}
func main() {
// Command line flags
name := flag.String("name", "cloned-cont", "Name for the new container")
image := flag.String("image", "alpine", "Image to use for the new container")
cmdStr := flag.String("cmd", "sleep infinity", "Command to run in the container")
interactive := flag.Bool("it", false, "Run container in interactive mode with TTY")
target := flag.String("target", "", "Target container ID to clone namespaces from")
flag.Parse()
// If target not provided via flag, check positional arg or prompt
containerID := *target
if containerID == "" && flag.NArg() > 0 {
containerID = flag.Arg(0)
}
if containerID == "" {
fmt.Print("Enter container ID: ")
fmt.Scan(&containerID)
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.45"))
if err != nil {
panic(err)
}
containerInfo, err := getContainerFromID(cli, containerID)
if err != nil {
log.Fatalf("Error getting container: %v", err)
}
// Print container information
fmt.Printf("Container ID: %s\n", containerInfo.ID)
fmt.Printf("Container Name: %s\n", containerInfo.Name)
fmt.Printf("Container State: %s\n", containerInfo.State.Status)
fmt.Printf("Container Image: %s\n", containerInfo.Config.Image)
// Parse command string into slice
cmd := parseCommand(*cmdStr)
// Create and start container sharing namespaces with source
copyContainerID, err := createAndStartContainer(cli, *image, *name, containerInfo.ID, cmd, *interactive)
if err != nil {
log.Fatalf("Failed to create container: %v", err)
}
fmt.Printf("Successfully created container '%s': %s\n", *name, copyContainerID)
}
func parseCommand(cmdStr string) []string {
if cmdStr == "" {
return []string{"/bin/sh", "-c", "sleep infinity"}
}
return strings.Fields(cmdStr)
}