2014-07-31 16:36:42 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-08-29 22:43:33 -04:00
|
|
|
"strings"
|
2014-11-10 16:14:17 -05:00
|
|
|
|
2015-07-02 06:24:35 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-06-12 09:25:32 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-09-17 14:54:14 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-07-29 19:45:47 -04:00
|
|
|
"github.com/docker/docker/graph/tags"
|
2015-07-20 13:57:15 -04:00
|
|
|
"github.com/docker/docker/image"
|
2014-07-31 16:36:42 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2015-06-12 09:25:32 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2014-07-31 16:36:42 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
2015-07-16 19:00:55 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2014-07-31 16:36:42 -04:00
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// ContainerCreate takes configs and creates a container.
|
2015-09-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig, adjustCPUShares bool) (types.ContainerCreateResponse, error) {
|
2015-06-06 12:41:42 -04:00
|
|
|
if config == nil {
|
2015-09-22 21:06:09 -04:00
|
|
|
return types.ContainerCreateResponse{}, derr.ErrorCodeEmptyConfig
|
2015-06-06 12:41:42 -04:00
|
|
|
}
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
warnings, err := daemon.verifyContainerSettings(hostConfig, config)
|
2015-04-16 02:31:52 -04:00
|
|
|
if err != nil {
|
2015-09-22 21:06:09 -04:00
|
|
|
return types.ContainerCreateResponse{"", warnings}, err
|
2015-01-22 22:29:21 -05:00
|
|
|
}
|
2014-09-25 17:23:59 -04:00
|
|
|
|
2015-09-06 23:07:12 -04:00
|
|
|
daemon.adaptContainerSettings(hostConfig, adjustCPUShares)
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
container, buildWarnings, err := daemon.Create(config, hostConfig, name)
|
2014-07-31 16:36:42 -04:00
|
|
|
if err != nil {
|
2015-09-29 13:51:40 -04:00
|
|
|
if daemon.Graph().IsNotExist(err, config.Image) {
|
2015-08-29 22:43:33 -04:00
|
|
|
if strings.Contains(config.Image, "@") {
|
2015-09-22 21:06:09 -04:00
|
|
|
return types.ContainerCreateResponse{"", warnings}, derr.ErrorCodeNoSuchImageHash.WithArgs(config.Image)
|
2015-08-29 22:43:33 -04:00
|
|
|
}
|
|
|
|
img, tag := parsers.ParseRepositoryTag(config.Image)
|
2014-07-31 16:36:42 -04:00
|
|
|
if tag == "" {
|
2015-07-29 19:45:47 -04:00
|
|
|
tag = tags.DefaultTag
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2015-09-22 21:06:09 -04:00
|
|
|
return types.ContainerCreateResponse{"", warnings}, derr.ErrorCodeNoSuchImageTag.WithArgs(img, tag)
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2015-09-22 21:06:09 -04:00
|
|
|
return types.ContainerCreateResponse{"", warnings}, err
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2014-12-01 11:44:13 -05:00
|
|
|
|
2015-04-09 17:49:22 -04:00
|
|
|
warnings = append(warnings, buildWarnings...)
|
2014-03-10 09:11:23 -04:00
|
|
|
|
2015-09-22 21:06:09 -04:00
|
|
|
return types.ContainerCreateResponse{container.ID, warnings}, nil
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new container from the given configuration with a given name.
|
2015-09-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.HostConfig, name string) (retC *Container, retS []string, retErr error) {
|
2014-07-31 16:36:42 -04:00
|
|
|
var (
|
|
|
|
container *Container
|
|
|
|
warnings []string
|
2015-07-20 13:57:15 -04:00
|
|
|
img *image.Image
|
2014-10-28 17:06:23 -04:00
|
|
|
imgID string
|
|
|
|
err error
|
2014-07-31 16:36:42 -04:00
|
|
|
)
|
|
|
|
|
2014-10-28 17:06:23 -04:00
|
|
|
if config.Image != "" {
|
|
|
|
img, err = daemon.repositories.LookupImage(config.Image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-06-05 18:31:10 -04:00
|
|
|
if err = daemon.graph.CheckDepth(img); err != nil {
|
2014-10-28 17:06:23 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
imgID = img.ID
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2014-10-28 17:06:23 -04:00
|
|
|
|
2015-05-24 09:17:29 -04:00
|
|
|
if err := daemon.mergeAndVerifyConfig(config, img); err != nil {
|
2014-07-31 16:36:42 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-08-11 22:27:33 -04:00
|
|
|
|
2015-01-19 12:09:02 -05:00
|
|
|
if hostConfig == nil {
|
|
|
|
hostConfig = &runconfig.HostConfig{}
|
|
|
|
}
|
|
|
|
if hostConfig.SecurityOpt == nil {
|
2015-09-29 13:51:40 -04:00
|
|
|
hostConfig.SecurityOpt, err = daemon.generateSecurityOpt(hostConfig.IpcMode, hostConfig.PidMode)
|
2014-11-10 16:14:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-09-29 13:51:40 -04:00
|
|
|
if container, err = daemon.newContainer(name, config, imgID); err != nil {
|
2014-07-31 16:36:42 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-08-11 10:21:52 -04:00
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := daemon.rm(container, false); err != nil {
|
2015-08-11 10:21:52 -04:00
|
|
|
logrus.Errorf("Clean up Error! Cannot destroy container %s: %v", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := daemon.Register(container); err != nil {
|
2014-09-29 20:06:26 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2014-10-28 17:06:23 -04:00
|
|
|
if err := daemon.createRootfs(container); err != nil {
|
2014-07-31 16:36:42 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := daemon.setHostConfig(container, hostConfig); err != nil {
|
2015-05-19 16:05:25 -04:00
|
|
|
return nil, nil, err
|
2014-09-25 17:23:59 -04:00
|
|
|
}
|
2015-09-14 13:21:17 -04:00
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
|
|
|
if err := container.removeMountPoints(true); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-09-29 13:51:40 -04:00
|
|
|
if err := container.Mount(); err != nil {
|
2014-11-11 11:17:33 -05:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-09-29 13:51:40 -04:00
|
|
|
defer container.Unmount()
|
2015-05-19 16:05:25 -04:00
|
|
|
|
2015-08-24 13:57:39 -04:00
|
|
|
if err := createContainerPlatformSpecificSettings(container, config, hostConfig, img); err != nil {
|
2015-07-16 17:14:58 -04:00
|
|
|
return nil, nil, err
|
2014-11-11 11:17:33 -05:00
|
|
|
}
|
2015-07-16 17:14:58 -04:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
if err := container.toDiskLocking(); err != nil {
|
2015-07-02 06:24:35 -04:00
|
|
|
logrus.Errorf("Error saving new container to disk: %v", err)
|
2014-07-31 16:36:42 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-09-29 13:51:40 -04:00
|
|
|
container.logEvent("create")
|
2014-07-31 16:36:42 -04:00
|
|
|
return container, warnings, nil
|
|
|
|
}
|
2014-12-01 11:44:13 -05:00
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) generateSecurityOpt(ipcMode runconfig.IpcMode, pidMode runconfig.PidMode) ([]string, error) {
|
2014-11-25 15:10:53 -05:00
|
|
|
if ipcMode.IsHost() || pidMode.IsHost() {
|
2014-11-10 16:14:17 -05:00
|
|
|
return label.DisableSecOpt(), nil
|
|
|
|
}
|
|
|
|
if ipcContainer := ipcMode.Container(); ipcContainer != "" {
|
2015-09-29 13:51:40 -04:00
|
|
|
c, err := daemon.Get(ipcContainer)
|
2014-12-16 18:06:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-11-10 16:14:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return label.DupSecOpt(c.ProcessLabel), nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
|
|
|
|
// VolumeCreate creates a volume with the specified name, driver, and opts
|
|
|
|
// This is called directly from the remote API
|
2015-09-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) VolumeCreate(name, driverName string, opts map[string]string) (*types.Volume, error) {
|
2015-06-12 09:25:32 -04:00
|
|
|
if name == "" {
|
|
|
|
name = stringid.GenerateNonCryptoID()
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := daemon.volumes.Create(name, driverName, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return volumeToAPIType(v), nil
|
|
|
|
}
|