MCPcopy Index your code
hub / github.com/docker/cli / createContainer

Function createContainer

cli/command/container/create.go:214–380  ·  view source on GitHub ↗

nolint:gocyclo

(ctx context.Context, dockerCLI command.Cli, containerCfg *containerConfig, options *createOptions)

Source from the content-addressed store, hash-verified

212
213//nolint:gocyclo
214func createContainer(ctx context.Context, dockerCLI command.Cli, containerCfg *containerConfig, options *createOptions) (containerID string, _ error) {
215 config := containerCfg.Config
216 hostConfig := containerCfg.HostConfig
217 networkingConfig := containerCfg.NetworkingConfig
218
219 var namedRef reference.Named
220
221 // TODO(thaJeztah): add a platform option-type / flag-type.
222 if options.platform != "" {
223 if _, err := platforms.Parse(options.platform); err != nil {
224 return "", err
225 }
226 }
227
228 containerIDFile, err := newCIDFile(hostConfig.ContainerIDFile)
229 if err != nil {
230 return "", err
231 }
232 defer func() {
233 _ = containerIDFile.Close()
234 }()
235
236 ref, err := reference.ParseAnyReference(config.Image)
237 if err != nil {
238 return "", err
239 }
240 if named, ok := ref.(reference.Named); ok {
241 namedRef = reference.TagNameOnly(named)
242 }
243
244 const dockerConfigPathInContainer = "/run/secrets/docker/config.json"
245 var apiSocketCreds map[string]types.AuthConfig
246
247 if options.useAPISocket {
248 // We'll create two new mounts to handle this flag:
249 //
250 // 1. Mount the actual docker socket.
251 // 2. A synthesized ~/.docker/config.json with resolved tokens.
252
253 if dockerCLI.ServerInfo().OSType == "windows" {
254 return "", errors.New("flag --use-api-socket can't be used with a Windows Docker Engine")
255 }
256
257 // hard-code engine socket path until https://github.com/moby/moby/pull/43459 gives us a discovery mechanism
258 containerCfg.HostConfig.Mounts = append(containerCfg.HostConfig.Mounts, mount.Mount{
259 Type: mount.TypeBind,
260 Source: "/var/run/docker.sock",
261 Target: "/var/run/docker.sock",
262 BindOptions: &mount.BindOptions{},
263 })
264
265 /*
266
267 Ideally, we'd like to copy the config into a tmpfs but unfortunately,
268 the mounts won't be in place until we start the container. This can
269 leave around the config if the container doesn't get deleted.
270
271 We are using the most compose-secret-compatible approach,

Callers 3

runContainerFunction · 0.85
runCreateFunction · 0.85

Calls 13

newCIDFileFunction · 0.85
readCredentialsFunction · 0.85
pullImageFunction · 0.85
GetTtySizeMethod · 0.80
ContainerCreateMethod · 0.80
invalidParameterFunction · 0.70
ServerInfoMethod · 0.65
OutMethod · 0.65
ClientMethod · 0.65
ErrMethod · 0.65
CloseMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…