Create creates a new container from the given configuration with a given name.
(ctx context.Context, daemonCfg *config.Config, opts createOpts)
| 169 | |
| 170 | // Create creates a new container from the given configuration with a given name. |
| 171 | func (daemon *Daemon) create(ctx context.Context, daemonCfg *config.Config, opts createOpts) (retC *container.Container, retErr error) { |
| 172 | var ( |
| 173 | ctr *container.Container |
| 174 | img *image.Image |
| 175 | imgManifest *ocispec.Descriptor |
| 176 | imgID image.ID |
| 177 | err error |
| 178 | platform = platforms.DefaultSpec() |
| 179 | ) |
| 180 | |
| 181 | if opts.params.Config.Image != "" { |
| 182 | img, err = daemon.imageService.GetImage(ctx, opts.params.Config.Image, imagebackend.GetImageOpts{Platform: opts.params.Platform}) |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | if img.Details != nil { |
| 187 | imgManifest = img.Details.ManifestDescriptor |
| 188 | } |
| 189 | platform = img.Platform() |
| 190 | imgID = img.ID() |
| 191 | } else if isWindows { |
| 192 | platform.OS = "linux" // 'scratch' case. |
| 193 | } |
| 194 | |
| 195 | // On WCOW, if are not being invoked by the builder to create this container (where |
| 196 | // ignoreImagesArgEscaped will be true) - if the image already has its arguments escaped, |
| 197 | // ensure that this is replicated across to the created container to avoid double-escaping |
| 198 | // of the arguments/command line when the runtime attempts to run the container. |
| 199 | if platform.OS == "windows" && !opts.ignoreImagesArgsEscaped && img != nil && img.RunConfig().ArgsEscaped { |
| 200 | opts.params.Config.ArgsEscaped = true |
| 201 | } |
| 202 | |
| 203 | if err := daemon.mergeAndVerifyConfig(opts.params.Config, img); err != nil { |
| 204 | return nil, errdefs.InvalidParameter(err) |
| 205 | } |
| 206 | |
| 207 | if err := daemon.mergeAndVerifyLogConfig(&opts.params.HostConfig.LogConfig); err != nil { |
| 208 | return nil, errdefs.InvalidParameter(err) |
| 209 | } |
| 210 | |
| 211 | if ctr, err = daemon.newContainer(opts.params.Name, platform, opts.params.Config, opts.params.HostConfig, imgID, opts.managed); err != nil { |
| 212 | return nil, err |
| 213 | } |
| 214 | defer func() { |
| 215 | if retErr != nil { |
| 216 | err = daemon.cleanupContainer(ctr, backend.ContainerRmConfig{ |
| 217 | ForceRemove: true, |
| 218 | RemoveVolume: true, |
| 219 | }) |
| 220 | if err != nil { |
| 221 | log.G(ctx).WithFields(log.Fields{ |
| 222 | "error": err, |
| 223 | "container": ctr.ID, |
| 224 | }).Errorf("failed to cleanup container on create error") |
| 225 | } |
| 226 | } |
| 227 | }() |
| 228 |
no test coverage detected