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