2014-07-31 16:36:42 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
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-20 13:57:15 -04:00
|
|
|
"github.com/docker/docker/image"
|
2015-11-18 17:20:54 -05:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
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-10-04 08:09:51 -04:00
|
|
|
"github.com/docker/docker/volume"
|
2015-07-16 19:00:55 -04:00
|
|
|
"github.com/opencontainers/runc/libcontainer/label"
|
2014-07-31 16:36:42 -04:00
|
|
|
)
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
// ContainerCreateConfig is the parameter set to ContainerCreate()
|
|
|
|
type ContainerCreateConfig struct {
|
|
|
|
Name string
|
|
|
|
Config *runconfig.Config
|
|
|
|
HostConfig *runconfig.HostConfig
|
|
|
|
AdjustCPUShares bool
|
|
|
|
}
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// ContainerCreate takes configs and creates a container.
|
2015-09-09 22:23:06 -04:00
|
|
|
func (daemon *Daemon) ContainerCreate(params *ContainerCreateConfig) (types.ContainerCreateResponse, error) {
|
|
|
|
if params.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-09 22:23:06 -04:00
|
|
|
warnings, err := daemon.verifyContainerSettings(params.HostConfig, params.Config)
|
2015-04-16 02:31:52 -04:00
|
|
|
if err != nil {
|
2015-11-02 11:28:34 -05:00
|
|
|
return types.ContainerCreateResponse{ID: "", Warnings: warnings}, err
|
2015-01-22 22:29:21 -05:00
|
|
|
}
|
2014-09-25 17:23:59 -04:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
daemon.adaptContainerSettings(params.HostConfig, params.AdjustCPUShares)
|
2015-09-06 23:07:12 -04:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
container, err := daemon.create(params)
|
2014-07-31 16:36:42 -04:00
|
|
|
if err != nil {
|
2015-11-18 17:20:54 -05:00
|
|
|
return types.ContainerCreateResponse{ID: "", Warnings: warnings}, daemon.imageNotExistToErrcode(err)
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2014-12-01 11:44:13 -05:00
|
|
|
|
2015-11-02 11:28:34 -05:00
|
|
|
return types.ContainerCreateResponse{ID: container.ID, Warnings: 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-09 22:23:06 -04:00
|
|
|
func (daemon *Daemon) create(params *ContainerCreateConfig) (retC *Container, retErr error) {
|
2014-07-31 16:36:42 -04:00
|
|
|
var (
|
|
|
|
container *Container
|
2015-07-20 13:57:15 -04:00
|
|
|
img *image.Image
|
2015-11-18 17:20:54 -05:00
|
|
|
imgID image.ID
|
2014-10-28 17:06:23 -04:00
|
|
|
err error
|
2014-07-31 16:36:42 -04:00
|
|
|
)
|
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
if params.Config.Image != "" {
|
2015-11-18 17:20:54 -05:00
|
|
|
img, err = daemon.GetImage(params.Config.Image)
|
2014-10-28 17:06:23 -04:00
|
|
|
if err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
2014-10-28 17:06:23 -04:00
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
imgID = img.ID()
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2014-10-28 17:06:23 -04:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
if err := daemon.mergeAndVerifyConfig(params.Config, img); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2015-08-11 22:27:33 -04:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
if params.HostConfig == nil {
|
|
|
|
params.HostConfig = &runconfig.HostConfig{}
|
2015-01-19 12:09:02 -05:00
|
|
|
}
|
2015-09-09 22:23:06 -04:00
|
|
|
if params.HostConfig.SecurityOpt == nil {
|
|
|
|
params.HostConfig.SecurityOpt, err = daemon.generateSecurityOpt(params.HostConfig.IpcMode, params.HostConfig.PidMode)
|
2014-11-10 16:14:17 -05:00
|
|
|
if err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
2014-11-10 16:14:17 -05:00
|
|
|
}
|
|
|
|
}
|
2015-09-09 22:23:06 -04:00
|
|
|
if container, err = daemon.newContainer(params.Name, params.Config, imgID); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
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 {
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
2014-09-29 20:06:26 -04:00
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
rootUID, rootGID, err := idtools.GetRootUIDGID(daemon.uidMaps, daemon.gidMaps)
|
|
|
|
if err != nil {
|
2015-10-28 09:19:51 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
if err := idtools.MkdirAs(container.root, 0700, rootUID, rootGID); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
|
2015-09-09 22:23:06 -04:00
|
|
|
if err := daemon.setHostConfig(container, params.HostConfig); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
2014-09-25 17:23:59 -04:00
|
|
|
}
|
2015-09-14 13:21:17 -04:00
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
2015-11-03 11:42:08 -05:00
|
|
|
if err := daemon.removeMountPoints(container, true); err != nil {
|
2015-09-14 13:21:17 -04:00
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2015-05-19 16:05:25 -04:00
|
|
|
|
2015-11-03 14:25:22 -05:00
|
|
|
if err := daemon.createContainerPlatformSpecificSettings(container, params.Config, params.HostConfig, img); err != nil {
|
2015-09-06 13:26:40 -04:00
|
|
|
return 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)
|
2015-09-06 13:26:40 -04:00
|
|
|
return nil, err
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2015-11-03 12:33:13 -05:00
|
|
|
daemon.LogContainerEvent(container, "create")
|
2015-09-06 13:26:40 -04:00
|
|
|
return container, nil
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
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
|
|
|
|
}
|
2015-10-04 08:09:51 -04: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-06-12 09:25:32 -04:00
|
|
|
return volumeToAPIType(v), nil
|
|
|
|
}
|