/
create.go
318 lines (306 loc) · 8.98 KB
/
create.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package container
import (
"strings"
co "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
"github.com/go-zoox/cli"
"github.com/go-zoox/core-utils/fmt"
"github.com/go-zoox/docker"
"github.com/go-zoox/docker/container"
)
func Create() *cli.Command {
return &cli.Command{
Name: "create",
Usage: "create a new container",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Assign a name to the container",
},
&cli.StringSliceFlag{
Name: "env",
Aliases: []string{"e"},
Usage: "Set environment variables",
},
&cli.StringSliceFlag{
Name: "volume",
Aliases: []string{"v"},
Usage: "Bind mount a volume",
},
&cli.StringSliceFlag{
Name: "publish",
Aliases: []string{"p"},
Usage: "Publish a container's port(s) to the host",
},
&cli.StringFlag{
Name: "workdir",
Aliases: []string{"w"},
Usage: "Working directory inside the container",
},
&cli.StringFlag{
Name: "user",
Aliases: []string{"u"},
Usage: "Username or UID (format: <name|uid>[:<group|gid>])",
},
&cli.StringFlag{
Name: "hostname",
Usage: "Container host name",
},
&cli.StringSliceFlag{
Name: "entrypoint",
Usage: "Overwrite the default ENTRYPOINT of the image",
},
&cli.StringFlag{
Name: "health-cmd",
Usage: "Command to run to check health",
},
&cli.StringFlag{
Name: "health-interval",
Usage: "Time between running the check (ms|s|m|h) (default 0s)",
},
&cli.StringFlag{
Name: "health-timeout",
Usage: "Maximum time to allow one check to run (ms|s|m|h) (default 0s)",
},
&cli.IntFlag{
Name: "health-retries",
Usage: "Consecutive failures needed to report unhealthy",
},
&cli.StringFlag{
Name: "health-start-period",
Usage: "Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s)",
},
&cli.StringSliceFlag{
Name: "label",
Aliases: []string{"l"},
Usage: "Set meta data on a container",
},
&cli.StringFlag{
Name: "mac-address",
Usage: "Container MAC address (e.g. 92:d0:c6:0a:29:33)",
},
&cli.BoolFlag{
Name: "tty",
Usage: "Allocate a pseudo-TTY",
},
&cli.StringFlag{
Name: "restart",
Usage: "Restart policy to apply when a container exits (default \"no\")",
},
&cli.IntFlag{
Name: "restart-max-attempts",
Usage: "Maximum restart count for the container (default 0)",
},
&cli.BoolFlag{
Name: "privileged",
Usage: "Give extended privileges to this container",
},
&cli.StringSliceFlag{
Name: "link",
Usage: "Add link to another container",
},
&cli.BoolFlag{
Name: "rm",
Usage: "Automatically remove the container when it exits",
},
&cli.Int64Flag{
Name: "cpu-period",
Usage: "Limit CPU CFS (Completely Fair Scheduler) period",
},
&cli.Int64Flag{
Name: "cpu-quota",
Usage: "Limit CPU CFS (Completely Fair Scheduler) quota",
},
&cli.Int64Flag{
Name: "cpu-shares",
Usage: "CPU shares (relative weight)",
},
&cli.StringFlag{
Name: "cpuset-cpus",
Usage: "CPUs in which to allow execution (0-3, 0,1)",
},
&cli.StringFlag{
Name: "cpuset-mems",
Usage: "MEMs in which to allow execution (0-3, 0,1)",
},
&cli.Int64Flag{
Name: "cpu-rt-period",
Usage: "Limit CPU real-time period in microseconds",
},
&cli.Int64Flag{
Name: "cpu-rt-runtime",
Usage: "Limit CPU real-time runtime in microseconds",
},
&cli.Int64Flag{
Name: "cpus",
Usage: "Number of CPUs",
},
&cli.Int64Flag{
Name: "cpu-percent",
Usage: "CPU percent",
},
&cli.Int64Flag{
Name: "memory",
Usage: "Memory limit",
},
&cli.Int64Flag{
Name: "memory-reservation",
Usage: "Memory soft limit",
},
&cli.Int64Flag{
Name: "memory-swap",
Usage: "Swap limit equal to memory plus swap: '-1' to enable unlimited swap",
},
&cli.Int64Flag{
Name: "memory-swappiness",
Usage: "Tune container memory swappiness (0 to 100)",
},
&cli.StringSliceFlag{
Name: "dns",
Usage: "Set custom DNS servers",
},
&cli.StringSliceFlag{
Name: "dns-search",
Usage: "Set custom DNS search domains",
},
&cli.StringSliceFlag{
Name: "dns-opt",
Usage: "Set DNS options",
},
&cli.StringSliceFlag{
Name: "security-opt",
Usage: "Security Options",
},
&cli.StringFlag{
Name: "platform",
Usage: "Set platform if server is multi-platform capable",
},
},
Action: func(ctx *cli.Context) error {
client, err := docker.New()
if err != nil {
return err
}
image := ctx.Args().First()
if image == "" {
return fmt.Errorf("image is required")
}
cmd := ctx.Args().Tail()
response, err := client.Container().Create(ctx.Context, func(opt *container.CreateOptions) {
opt.Name = ctx.String("name")
// container
opt.Container.Image = image
opt.Container.Env = ctx.StringSlice("env")
opt.Container.Cmd = cmd
opt.Container.Volumes = map[string]struct{}{}
for _, volumeKV := range ctx.StringSlice("volume") {
kv := strings.SplitN(volumeKV, ":", 2)
volumeKey, volumeValue := kv[0], kv[1]
opt.Host.Mounts = append(opt.Host.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: volumeKey,
Target: volumeValue,
// ReadOnly: false,
})
}
opt.Container.ExposedPorts = nat.PortSet{}
for _, portKV := range ctx.StringSlice("publish") {
kv := strings.SplitN(portKV, ":", 2)
portKey, portValue := kv[0], kv[1]
opt.Container.ExposedPorts[nat.Port(portKey)] = struct{}{}
opt.Host.PortBindings = nat.PortMap{}
opt.Host.PortBindings[nat.Port(portKey)] = []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: portValue,
},
}
}
opt.Container.WorkingDir = ctx.String("workdir")
opt.Container.User = ctx.String("user")
opt.Container.Hostname = ctx.String("hostname")
opt.Container.Entrypoint = ctx.StringSlice("entrypoint")
opt.Container.Healthcheck = &co.HealthConfig{
Test: []string{
"sh", ctx.String("health-cmd"),
},
Interval: ctx.Duration("health-interval"),
Timeout: ctx.Duration("health-timeout"),
Retries: ctx.Int("health-retries"),
StartPeriod: ctx.Duration("health-start-period"),
}
opt.Container.Labels = map[string]string{}
for _, labelKV := range ctx.StringSlice("label") {
kv := strings.SplitN(labelKV, "=", 2)
labelKey, labelValue := kv[0], kv[1]
opt.Container.Labels[labelKey] = labelValue
}
opt.Container.MacAddress = ctx.String("mac-address")
opt.Container.Tty = ctx.Bool("tty")
// host
opt.Host.RestartPolicy = co.RestartPolicy{
Name: co.RestartPolicyMode(ctx.String("restart")),
MaximumRetryCount: ctx.Int("restart-max-attempts"),
}
opt.Host.Privileged = ctx.Bool("privileged")
opt.Host.Links = ctx.StringSlice("link")
opt.Host.AutoRemove = ctx.Bool("rm")
// Resources
opt.Host.Resources.CPUPeriod = ctx.Int64("cpu-period")
opt.Host.Resources.CPUQuota = ctx.Int64("cpu-quota")
opt.Host.Resources.CPUShares = ctx.Int64("cpu-shares")
opt.Host.Resources.CpusetCpus = ctx.String("cpuset-cpus")
opt.Host.Resources.CpusetMems = ctx.String("cpuset-mems")
opt.Host.Resources.CPURealtimePeriod = ctx.Int64("cpu-rt-period")
opt.Host.Resources.CPURealtimeRuntime = ctx.Int64("cpu-rt-runtime")
// Applicable to Windows
opt.Host.Resources.CPUCount = ctx.Int64("cpus")
opt.Host.Resources.CPUPercent = ctx.Int64("cpu-percent")
//
opt.Host.Resources.Memory = ctx.Int64("memory")
opt.Host.Resources.MemoryReservation = ctx.Int64("memory-reservation")
opt.Host.Resources.MemorySwap = ctx.Int64("memory-swap")
ms := ctx.Int64("memory-swappiness")
opt.Host.Resources.MemorySwappiness = &ms
//
opt.Host.DNS = ctx.StringSlice("dns")
opt.Host.DNSSearch = ctx.StringSlice("dns-search")
opt.Host.DNSOptions = ctx.StringSlice("dns-opt")
//
opt.Host.SecurityOpt = ctx.StringSlice("security-opt")
// Network
opt.Network.EndpointsConfig = map[string]*network.EndpointSettings{}
opt.Network.EndpointsConfig["bridge"] = &network.EndpointSettings{
NetworkID: "bridge",
}
// platform
// examples:
// linux/amd64
// linux/arm64
platform := ctx.String("platform")
for _, pl := range strings.Split(platform, ",") {
if pl == "" {
continue
}
kv := strings.Split(pl, "/")
platformOS, platformArch := kv[0], kv[1]
var platformVariant string
if len(kv) >= 3 {
platformVariant = kv[2]
}
opt.Platform.OS = platformOS
opt.Platform.Architecture = platformArch
opt.Platform.Variant = platformVariant
}
})
if err != nil {
return err
}
fmt.Println(response.ID)
return nil
},
}
}