github.com/moby/docker@v26.1.3+incompatible/integration/internal/container/ops.go (about) 1 package container 2 3 import ( 4 "maps" 5 "strings" 6 7 "github.com/docker/docker/api/types/container" 8 "github.com/docker/docker/api/types/mount" 9 "github.com/docker/docker/api/types/network" 10 "github.com/docker/docker/api/types/strslice" 11 "github.com/docker/go-connections/nat" 12 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 13 ) 14 15 // WithName sets the name of the container 16 func WithName(name string) func(*TestContainerConfig) { 17 return func(c *TestContainerConfig) { 18 c.Name = name 19 } 20 } 21 22 // WithLinks sets the links of the container 23 func WithLinks(links ...string) func(*TestContainerConfig) { 24 return func(c *TestContainerConfig) { 25 c.HostConfig.Links = links 26 } 27 } 28 29 // WithImage sets the image of the container 30 func WithImage(image string) func(*TestContainerConfig) { 31 return func(c *TestContainerConfig) { 32 c.Config.Image = image 33 } 34 } 35 36 // WithCmd sets the comannds of the container 37 func WithCmd(cmds ...string) func(*TestContainerConfig) { 38 return func(c *TestContainerConfig) { 39 c.Config.Cmd = strslice.StrSlice(cmds) 40 } 41 } 42 43 // WithNetworkMode sets the network mode of the container 44 func WithNetworkMode(mode string) func(*TestContainerConfig) { 45 return func(c *TestContainerConfig) { 46 c.HostConfig.NetworkMode = container.NetworkMode(mode) 47 } 48 } 49 50 // WithDNS sets external DNS servers for the container 51 func WithDNS(dns []string) func(*TestContainerConfig) { 52 return func(c *TestContainerConfig) { 53 c.HostConfig.DNS = append([]string(nil), dns...) 54 } 55 } 56 57 // WithSysctls sets sysctl options for the container 58 func WithSysctls(sysctls map[string]string) func(*TestContainerConfig) { 59 return func(c *TestContainerConfig) { 60 c.HostConfig.Sysctls = maps.Clone(sysctls) 61 } 62 } 63 64 // WithExposedPorts sets the exposed ports of the container 65 func WithExposedPorts(ports ...string) func(*TestContainerConfig) { 66 return func(c *TestContainerConfig) { 67 c.Config.ExposedPorts = map[nat.Port]struct{}{} 68 for _, port := range ports { 69 c.Config.ExposedPorts[nat.Port(port)] = struct{}{} 70 } 71 } 72 } 73 74 // WithTty sets the TTY mode of the container 75 func WithTty(tty bool) func(*TestContainerConfig) { 76 return func(c *TestContainerConfig) { 77 c.Config.Tty = tty 78 } 79 } 80 81 // WithWorkingDir sets the working dir of the container 82 func WithWorkingDir(dir string) func(*TestContainerConfig) { 83 return func(c *TestContainerConfig) { 84 c.Config.WorkingDir = dir 85 } 86 } 87 88 // WithMount adds an mount 89 func WithMount(m mount.Mount) func(*TestContainerConfig) { 90 return func(c *TestContainerConfig) { 91 c.HostConfig.Mounts = append(c.HostConfig.Mounts, m) 92 } 93 } 94 95 // WithVolume sets the volume of the container 96 func WithVolume(target string) func(*TestContainerConfig) { 97 return func(c *TestContainerConfig) { 98 if c.Config.Volumes == nil { 99 c.Config.Volumes = map[string]struct{}{} 100 } 101 c.Config.Volumes[target] = struct{}{} 102 } 103 } 104 105 // WithBind sets the bind mount of the container 106 func WithBind(src, target string) func(*TestContainerConfig) { 107 return func(c *TestContainerConfig) { 108 c.HostConfig.Binds = append(c.HostConfig.Binds, src+":"+target) 109 } 110 } 111 112 // WithBindRaw sets the bind mount of the container 113 func WithBindRaw(s string) func(*TestContainerConfig) { 114 return func(c *TestContainerConfig) { 115 c.HostConfig.Binds = append(c.HostConfig.Binds, s) 116 } 117 } 118 119 // WithTmpfs sets a target path in the container to a tmpfs, with optional options 120 // (separated with a colon). 121 func WithTmpfs(targetAndOpts string) func(config *TestContainerConfig) { 122 return func(c *TestContainerConfig) { 123 if c.HostConfig.Tmpfs == nil { 124 c.HostConfig.Tmpfs = make(map[string]string) 125 } 126 127 target, opts, _ := strings.Cut(targetAndOpts, ":") 128 c.HostConfig.Tmpfs[target] = opts 129 } 130 } 131 132 func WithMacAddress(networkName, mac string) func(config *TestContainerConfig) { 133 return func(c *TestContainerConfig) { 134 if c.NetworkingConfig.EndpointsConfig == nil { 135 c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{} 136 } 137 if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil { 138 c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{} 139 } 140 c.NetworkingConfig.EndpointsConfig[networkName].MacAddress = mac 141 } 142 } 143 144 // WithIPv4 sets the specified ip for the specified network of the container 145 func WithIPv4(networkName, ip string) func(*TestContainerConfig) { 146 return func(c *TestContainerConfig) { 147 if c.NetworkingConfig.EndpointsConfig == nil { 148 c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{} 149 } 150 if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil { 151 c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{} 152 } 153 if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil { 154 c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{} 155 } 156 c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv4Address = ip 157 } 158 } 159 160 // WithIPv6 sets the specified ip6 for the specified network of the container 161 func WithIPv6(networkName, ip string) func(*TestContainerConfig) { 162 return func(c *TestContainerConfig) { 163 if c.NetworkingConfig.EndpointsConfig == nil { 164 c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{} 165 } 166 if v, ok := c.NetworkingConfig.EndpointsConfig[networkName]; !ok || v == nil { 167 c.NetworkingConfig.EndpointsConfig[networkName] = &network.EndpointSettings{} 168 } 169 if c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig == nil { 170 c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig = &network.EndpointIPAMConfig{} 171 } 172 c.NetworkingConfig.EndpointsConfig[networkName].IPAMConfig.IPv6Address = ip 173 } 174 } 175 176 func WithEndpointSettings(nw string, config *network.EndpointSettings) func(*TestContainerConfig) { 177 return func(c *TestContainerConfig) { 178 if c.NetworkingConfig.EndpointsConfig == nil { 179 c.NetworkingConfig.EndpointsConfig = map[string]*network.EndpointSettings{} 180 } 181 if _, ok := c.NetworkingConfig.EndpointsConfig[nw]; !ok { 182 c.NetworkingConfig.EndpointsConfig[nw] = config 183 } 184 } 185 } 186 187 // WithLogDriver sets the log driver to use for the container 188 func WithLogDriver(driver string) func(*TestContainerConfig) { 189 return func(c *TestContainerConfig) { 190 c.HostConfig.LogConfig.Type = driver 191 } 192 } 193 194 // WithAutoRemove sets the container to be removed on exit 195 func WithAutoRemove(c *TestContainerConfig) { 196 c.HostConfig.AutoRemove = true 197 } 198 199 // WithPidsLimit sets the container's "pids-limit 200 func WithPidsLimit(limit *int64) func(*TestContainerConfig) { 201 return func(c *TestContainerConfig) { 202 if c.HostConfig == nil { 203 c.HostConfig = &container.HostConfig{} 204 } 205 c.HostConfig.PidsLimit = limit 206 } 207 } 208 209 // WithRestartPolicy sets container's restart policy 210 func WithRestartPolicy(policy container.RestartPolicyMode) func(c *TestContainerConfig) { 211 return func(c *TestContainerConfig) { 212 c.HostConfig.RestartPolicy.Name = policy 213 } 214 } 215 216 // WithUser sets the user 217 func WithUser(user string) func(c *TestContainerConfig) { 218 return func(c *TestContainerConfig) { 219 c.Config.User = user 220 } 221 } 222 223 // WithAdditionalGroups sets the additional groups for the container 224 func WithAdditionalGroups(groups ...string) func(c *TestContainerConfig) { 225 return func(c *TestContainerConfig) { 226 c.HostConfig.GroupAdd = groups 227 } 228 } 229 230 // WithPrivileged sets privileged mode for the container 231 func WithPrivileged(privileged bool) func(*TestContainerConfig) { 232 return func(c *TestContainerConfig) { 233 if c.HostConfig == nil { 234 c.HostConfig = &container.HostConfig{} 235 } 236 c.HostConfig.Privileged = privileged 237 } 238 } 239 240 // WithCgroupnsMode sets the cgroup namespace mode for the container 241 func WithCgroupnsMode(mode string) func(*TestContainerConfig) { 242 return func(c *TestContainerConfig) { 243 if c.HostConfig == nil { 244 c.HostConfig = &container.HostConfig{} 245 } 246 c.HostConfig.CgroupnsMode = container.CgroupnsMode(mode) 247 } 248 } 249 250 // WithExtraHost sets the user defined IP:Host mappings in the container's 251 // /etc/hosts file 252 func WithExtraHost(extraHost string) func(*TestContainerConfig) { 253 return func(c *TestContainerConfig) { 254 c.HostConfig.ExtraHosts = append(c.HostConfig.ExtraHosts, extraHost) 255 } 256 } 257 258 // WithPlatform specifies the desired platform the image should have. 259 func WithPlatform(p *ocispec.Platform) func(*TestContainerConfig) { 260 return func(c *TestContainerConfig) { 261 c.Platform = p 262 } 263 } 264 265 // WithWindowsDevice specifies a Windows Device, ala `--device` on the CLI 266 func WithWindowsDevice(device string) func(*TestContainerConfig) { 267 return func(c *TestContainerConfig) { 268 c.HostConfig.Devices = append(c.HostConfig.Devices, container.DeviceMapping{PathOnHost: device}) 269 } 270 } 271 272 // WithIsolation specifies the isolation technology to apply to the container 273 func WithIsolation(isolation container.Isolation) func(*TestContainerConfig) { 274 return func(c *TestContainerConfig) { 275 c.HostConfig.Isolation = isolation 276 } 277 } 278 279 // WithConsoleSize sets the initial console size of the container 280 func WithConsoleSize(width, height uint) func(*TestContainerConfig) { 281 return func(c *TestContainerConfig) { 282 c.HostConfig.ConsoleSize = [2]uint{height, width} 283 } 284 } 285 286 // WithRuntime sets the runtime to use to start the container 287 func WithRuntime(name string) func(*TestContainerConfig) { 288 return func(c *TestContainerConfig) { 289 c.HostConfig.Runtime = name 290 } 291 } 292 293 // WithCDIDevices sets the CDI devices to use to start the container 294 func WithCDIDevices(cdiDeviceNames ...string) func(*TestContainerConfig) { 295 return func(c *TestContainerConfig) { 296 request := container.DeviceRequest{ 297 Driver: "cdi", 298 DeviceIDs: cdiDeviceNames, 299 } 300 c.HostConfig.DeviceRequests = append(c.HostConfig.DeviceRequests, request) 301 } 302 } 303 304 func WithCapability(capabilities ...string) func(*TestContainerConfig) { 305 return func(c *TestContainerConfig) { 306 c.HostConfig.CapAdd = append(c.HostConfig.CapAdd, capabilities...) 307 } 308 } 309 310 func WithDropCapability(capabilities ...string) func(*TestContainerConfig) { 311 return func(c *TestContainerConfig) { 312 c.HostConfig.CapDrop = append(c.HostConfig.CapDrop, capabilities...) 313 } 314 } 315 316 func WithSecurityOpt(opt string) func(*TestContainerConfig) { 317 return func(c *TestContainerConfig) { 318 c.HostConfig.SecurityOpt = append(c.HostConfig.SecurityOpt, opt) 319 } 320 } 321 322 // WithPIDMode sets the PID-mode for the container. 323 func WithPIDMode(mode container.PidMode) func(c *TestContainerConfig) { 324 return func(c *TestContainerConfig) { 325 c.HostConfig.PidMode = mode 326 } 327 } 328 329 func WithStopSignal(stopSignal string) func(c *TestContainerConfig) { 330 return func(c *TestContainerConfig) { 331 c.Config.StopSignal = stopSignal 332 } 333 } 334 335 func WithContainerWideMacAddress(address string) func(c *TestContainerConfig) { 336 return func(c *TestContainerConfig) { 337 c.Config.MacAddress = address //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44. 338 } 339 }