github.com/mheon/docker@v0.11.2-0.20150922122814-44f47903a831/daemon/create.go (about) 1 package daemon 2 3 import ( 4 "strings" 5 6 "github.com/Sirupsen/logrus" 7 "github.com/docker/docker/api/types" 8 derr "github.com/docker/docker/errors" 9 "github.com/docker/docker/graph/tags" 10 "github.com/docker/docker/image" 11 "github.com/docker/docker/pkg/parsers" 12 "github.com/docker/docker/pkg/stringid" 13 "github.com/docker/docker/runconfig" 14 "github.com/opencontainers/runc/libcontainer/label" 15 ) 16 17 // ContainerCreate takes configs and creates a container. 18 func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig, adjustCPUShares bool) (*Container, []string, error) { 19 if config == nil { 20 return nil, nil, derr.ErrorCodeEmptyConfig 21 } 22 23 warnings, err := daemon.verifyContainerSettings(hostConfig, config) 24 if err != nil { 25 return nil, warnings, err 26 } 27 28 daemon.adaptContainerSettings(hostConfig, adjustCPUShares) 29 30 container, buildWarnings, err := daemon.Create(config, hostConfig, name) 31 if err != nil { 32 if daemon.Graph().IsNotExist(err, config.Image) { 33 if strings.Contains(config.Image, "@") { 34 return nil, warnings, derr.ErrorCodeNoSuchImageHash.WithArgs(config.Image) 35 } 36 img, tag := parsers.ParseRepositoryTag(config.Image) 37 if tag == "" { 38 tag = tags.DefaultTag 39 } 40 return nil, warnings, derr.ErrorCodeNoSuchImageTag.WithArgs(img, tag) 41 } 42 return nil, warnings, err 43 } 44 45 warnings = append(warnings, buildWarnings...) 46 47 return container, warnings, nil 48 } 49 50 // Create creates a new container from the given configuration with a given name. 51 func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.HostConfig, name string) (retC *Container, retS []string, retErr error) { 52 var ( 53 container *Container 54 warnings []string 55 img *image.Image 56 imgID string 57 err error 58 ) 59 60 if config.Image != "" { 61 img, err = daemon.repositories.LookupImage(config.Image) 62 if err != nil { 63 return nil, nil, err 64 } 65 if err = daemon.graph.CheckDepth(img); err != nil { 66 return nil, nil, err 67 } 68 imgID = img.ID 69 } 70 71 if err := daemon.mergeAndVerifyConfig(config, img); err != nil { 72 return nil, nil, err 73 } 74 75 if hostConfig == nil { 76 hostConfig = &runconfig.HostConfig{} 77 } 78 if hostConfig.SecurityOpt == nil { 79 hostConfig.SecurityOpt, err = daemon.generateSecurityOpt(hostConfig.IpcMode, hostConfig.PidMode) 80 if err != nil { 81 return nil, nil, err 82 } 83 } 84 if container, err = daemon.newContainer(name, config, imgID); err != nil { 85 return nil, nil, err 86 } 87 defer func() { 88 if retErr != nil { 89 if err := daemon.rm(container, false); err != nil { 90 logrus.Errorf("Clean up Error! Cannot destroy container %s: %v", container.ID, err) 91 } 92 } 93 }() 94 95 if err := daemon.Register(container); err != nil { 96 return nil, nil, err 97 } 98 if err := daemon.createRootfs(container); err != nil { 99 return nil, nil, err 100 } 101 if err := daemon.setHostConfig(container, hostConfig); err != nil { 102 return nil, nil, err 103 } 104 defer func() { 105 if retErr != nil { 106 if err := container.removeMountPoints(true); err != nil { 107 logrus.Error(err) 108 } 109 } 110 }() 111 if err := container.Mount(); err != nil { 112 return nil, nil, err 113 } 114 defer container.Unmount() 115 116 if err := createContainerPlatformSpecificSettings(container, config, hostConfig, img); err != nil { 117 return nil, nil, err 118 } 119 120 if err := container.toDiskLocking(); err != nil { 121 logrus.Errorf("Error saving new container to disk: %v", err) 122 return nil, nil, err 123 } 124 container.logEvent("create") 125 return container, warnings, nil 126 } 127 128 func (daemon *Daemon) generateSecurityOpt(ipcMode runconfig.IpcMode, pidMode runconfig.PidMode) ([]string, error) { 129 if ipcMode.IsHost() || pidMode.IsHost() { 130 return label.DisableSecOpt(), nil 131 } 132 if ipcContainer := ipcMode.Container(); ipcContainer != "" { 133 c, err := daemon.Get(ipcContainer) 134 if err != nil { 135 return nil, err 136 } 137 138 return label.DupSecOpt(c.ProcessLabel), nil 139 } 140 return nil, nil 141 } 142 143 // VolumeCreate creates a volume with the specified name, driver, and opts 144 // This is called directly from the remote API 145 func (daemon *Daemon) VolumeCreate(name, driverName string, opts map[string]string) (*types.Volume, error) { 146 if name == "" { 147 name = stringid.GenerateNonCryptoID() 148 } 149 150 v, err := daemon.volumes.Create(name, driverName, opts) 151 if err != nil { 152 return nil, err 153 } 154 return volumeToAPIType(v), nil 155 }