2014-08-01 00:36:42 +04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
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-12-18 21:36:17 +03:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2015-11-12 22:55:17 +03:00
|
|
|
"github.com/docker/docker/container"
|
2015-09-17 21:54:14 +03:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-07-20 20:57:15 +03:00
|
|
|
"github.com/docker/docker/image"
|
2015-12-17 01:13:50 +03:00
|
|
|
"github.com/docker/docker/layer"
|
2015-11-19 01:20:54 +03:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2015-06-12 16:25:32 +03:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-10-04 15:09:51 +03:00
|
|
|
"github.com/docker/docker/volume"
|
2015-07-17 02:00:55 +03:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2014-08-01 00:36:42 +04:00
|
|
|
)
|
|
|
|
|
2015-12-16 19:56:49 +03:00
|
|
|
// ContainerCreate creates a container.
|
|
|
|
func (daemon *Daemon) ContainerCreate(params types.ContainerCreateConfig) (types.ContainerCreateResponse, error) {
|
2015-09-10 05:23:06 +03:00
|
|
|
if params.Config == nil {
|
2015-09-23 04:06:09 +03:00
|
|
|
return types.ContainerCreateResponse{}, derr.ErrorCodeEmptyConfig
|
2015-06-06 19:41:42 +03:00
|
|
|
}
|
|
|
|
|
2015-09-10 05:23:06 +03:00
|
|
|
warnings, err := daemon.verifyContainerSettings(params.HostConfig, params.Config)
|
2015-04-16 09:31:52 +03:00
|
|
|
if err != nil {
|
2015-12-16 19:56:49 +03:00
|
|
|
return types.ContainerCreateResponse{Warnings: warnings}, err
|
2015-01-23 06:29:21 +03:00
|
|
|
}
|
2014-09-26 01:23:59 +04:00
|
|
|
|
2015-11-30 08:10:18 +03:00
|
|
|
if params.HostConfig == nil {
|
2015-12-18 21:36:17 +03:00
|
|
|
params.HostConfig = &containertypes.HostConfig{}
|
2015-11-30 08:10:18 +03:00
|
|
|
}
|
|
|
|
err = daemon.adaptContainerSettings(params.HostConfig, params.AdjustCPUShares)
|
|
|
|
if err != nil {
|
2015-12-16 19:56:49 +03:00
|
|
|
return types.ContainerCreateResponse{Warnings: warnings}, err
|
2015-11-30 08:10:18 +03:00
|
|
|
}
|
2015-09-07 06:07:12 +03:00
|
|
|
|
2015-09-10 05:23:06 +03:00
|
|
|
container, err := daemon.create(params)
|
2014-08-01 00:36:42 +04:00
|
|
|
if err != nil {
|
2015-12-16 19:56:49 +03:00
|
|
|
return types.ContainerCreateResponse{Warnings: warnings}, daemon.imageNotExistToErrcode(err)
|
2014-08-01 00:36:42 +04:00
|
|
|
}
|
2014-12-01 19:44:13 +03:00
|
|
|
|
2015-11-02 19:28:34 +03:00
|
|
|
return types.ContainerCreateResponse{ID: container.ID, Warnings: warnings}, nil
|
2014-08-01 00:36:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates a new container from the given configuration with a given name.
|
2015-12-22 03:13:02 +03:00
|
|
|
func (daemon *Daemon) create(params types.ContainerCreateConfig) (retC *container.Container, retErr error) {
|
2014-08-01 00:36:42 +04:00
|
|
|
var (
|
2015-11-12 22:55:17 +03:00
|
|
|
container *container.Container
|
2015-07-20 20:57:15 +03:00
|
|
|
img *image.Image
|
2015-11-19 01:20:54 +03:00
|
|
|
imgID image.ID
|
2014-10-29 00:06:23 +03:00
|
|
|
err error
|
2014-08-01 00:36:42 +04:00
|
|
|
)
|
|
|
|
|
2015-09-10 05:23:06 +03:00
|
|
|
if params.Config.Image != "" {
|
2015-11-19 01:20:54 +03:00
|
|
|
img, err = daemon.GetImage(params.Config.Image)
|
2014-10-29 00:06:23 +03:00
|
|
|
if err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return nil, err
|
2014-10-29 00:06:23 +03:00
|
|
|
}
|
2015-11-19 01:20:54 +03:00
|
|
|
imgID = img.ID()
|
2014-08-01 00:36:42 +04:00
|
|
|
}
|
2014-10-29 00:06:23 +03:00
|
|
|
|
2015-09-10 05:23:06 +03:00
|
|
|
if err := daemon.mergeAndVerifyConfig(params.Config, img); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return nil, err
|
2014-08-01 00:36:42 +04:00
|
|
|
}
|
2015-08-12 05:27:33 +03:00
|
|
|
|
2015-09-10 05:23:06 +03:00
|
|
|
if container, err = daemon.newContainer(params.Name, params.Config, imgID); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return nil, err
|
2014-08-01 00:36:42 +04:00
|
|
|
}
|
2015-08-11 17:21:52 +03:00
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
2015-12-04 23:34:43 +03:00
|
|
|
if err := daemon.ContainerRm(container.ID, &types.ContainerRmConfig{ForceRemove: true}); err != nil {
|
2015-08-11 17:21:52 +03:00
|
|
|
logrus.Errorf("Clean up Error! Cannot destroy container %s: %v", container.ID, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-12-21 22:23:20 +03:00
|
|
|
if err := daemon.setSecurityOptions(container, params.HostConfig); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set RWLayer for container after mount labels have been set
|
|
|
|
if err := daemon.setRWLayer(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:51:40 +03:00
|
|
|
if err := daemon.Register(container); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return nil, err
|
2014-09-30 04:06:26 +04:00
|
|
|
}
|
2015-11-19 01:20:54 +03:00
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(daemon.uidMaps, daemon.gidMaps)
|
|
|
|
if err != nil {
|
2015-10-28 16:19:51 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-12 22:55:17 +03:00
|
|
|
if err := idtools.MkdirAs(container.Root, 0700, rootUID, rootGID); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return nil, err
|
2014-08-01 00:36:42 +04:00
|
|
|
}
|
2015-11-19 01:20:54 +03:00
|
|
|
|
2015-09-10 05:23:06 +03:00
|
|
|
if err := daemon.setHostConfig(container, params.HostConfig); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return nil, err
|
2014-09-26 01:23:59 +04:00
|
|
|
}
|
2015-09-14 20:21:17 +03:00
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
2015-11-03 19:42:08 +03:00
|
|
|
if err := daemon.removeMountPoints(container, true); err != nil {
|
2015-09-14 20:21:17 +03:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-05-19 23:05:25 +03:00
|
|
|
|
2015-11-03 22:25:22 +03:00
|
|
|
if err := daemon.createContainerPlatformSpecificSettings(container, params.Config, params.HostConfig, img); err != nil {
|
2015-09-06 20:26:40 +03:00
|
|
|
return nil, err
|
2014-11-11 19:17:33 +03:00
|
|
|
}
|
2015-07-17 00:14:58 +03:00
|
|
|
|
2015-12-13 10:33:18 +03:00
|
|
|
if err := daemon.updateContainerNetworkSettings(container); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +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)
|
2015-09-06 20:26:40 +03:00
|
|
|
return nil, err
|
2014-08-01 00:36:42 +04:00
|
|
|
}
|
2015-11-03 20:33:13 +03:00
|
|
|
daemon.LogContainerEvent(container, "create")
|
2015-09-06 20:26:40 +03:00
|
|
|
return container, nil
|
2014-08-01 00:36:42 +04:00
|
|
|
}
|
2014-12-01 19:44:13 +03:00
|
|
|
|
2015-12-18 21:36:17 +03:00
|
|
|
func (daemon *Daemon) generateSecurityOpt(ipcMode containertypes.IpcMode, pidMode containertypes.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-12-11 20:39:28 +03:00
|
|
|
c, err := daemon.GetContainer(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
|
|
|
|
2015-12-17 01:13:50 +03:00
|
|
|
func (daemon *Daemon) setRWLayer(container *container.Container) error {
|
|
|
|
var layerID layer.ChainID
|
|
|
|
if container.ImageID != "" {
|
|
|
|
img, err := daemon.imageStore.Get(container.ImageID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
layerID = img.RootFS.ChainID()
|
|
|
|
}
|
|
|
|
rwLayer, err := daemon.layerStore.CreateRWLayer(container.ID, layerID, container.MountLabel, daemon.setupInitLayer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container.RWLayer = rwLayer
|
|
|
|
|
|
|
|
return 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-29 20:51:40 +03:00
|
|
|
func (daemon *Daemon) VolumeCreate(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
|
|
|
|
}
|
2015-10-04 15:09:51 +03:00
|
|
|
|
|
|
|
// keep "docker run -v existing_volume:/foo --volume-driver other_driver" work
|
|
|
|
if (driverName != "" && v.DriverName() != driverName) || (driverName == "" && v.DriverName() != volume.DefaultDriverName) {
|
|
|
|
return nil, derr.ErrorVolumeNameTaken.WithArgs(name, v.DriverName())
|
|
|
|
}
|
2015-12-22 03:45:31 +03:00
|
|
|
|
|
|
|
if driverName == "" {
|
|
|
|
driverName = volume.DefaultDriverName
|
|
|
|
}
|
|
|
|
daemon.LogVolumeEvent(name, "create", map[string]string{"driver": driverName})
|
2015-06-12 16:25:32 +03:00
|
|
|
return volumeToAPIType(v), nil
|
|
|
|
}
|