2014-07-31 16:36:42 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2014-11-10 16:14:17 -05:00
|
|
|
"fmt"
|
2015-05-19 16:05:25 -04:00
|
|
|
"os"
|
2015-04-24 19:15:18 -04:00
|
|
|
"path/filepath"
|
2015-05-19 16:05:25 -04:00
|
|
|
"strings"
|
2014-11-10 16:14:17 -05:00
|
|
|
|
2014-07-31 16:36:42 -04:00
|
|
|
"github.com/docker/docker/graph"
|
2014-10-28 17:06:23 -04:00
|
|
|
"github.com/docker/docker/image"
|
2014-07-31 16:36:42 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers"
|
2015-05-19 16:05:25 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2014-07-31 16:36:42 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
2014-11-10 16:14:17 -05:00
|
|
|
"github.com/docker/libcontainer/label"
|
2014-07-31 16:36:42 -04:00
|
|
|
)
|
|
|
|
|
2015-04-10 20:05:21 -04:00
|
|
|
func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig) (string, []string, error) {
|
2015-04-16 02:31:52 -04:00
|
|
|
warnings, err := daemon.verifyHostConfig(hostConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", warnings, err
|
2015-01-22 22:29:21 -05:00
|
|
|
}
|
2014-09-25 17:23:59 -04:00
|
|
|
|
2015-04-24 19:15:18 -04:00
|
|
|
// The check for a valid workdir path is made on the server rather than in the
|
|
|
|
// client. This is because we don't know the type of path (Linux or Windows)
|
|
|
|
// to validate on the client.
|
|
|
|
if config.WorkingDir != "" && !filepath.IsAbs(config.WorkingDir) {
|
|
|
|
return "", warnings, fmt.Errorf("The working directory '%s' is invalid. It needs to be an absolute path.", config.WorkingDir)
|
|
|
|
}
|
|
|
|
|
2014-09-25 17:23:59 -04:00
|
|
|
container, buildWarnings, err := daemon.Create(config, hostConfig, name)
|
2014-07-31 16:36:42 -04:00
|
|
|
if err != nil {
|
2015-03-27 21:07:20 -04:00
|
|
|
if daemon.Graph().IsNotExist(err, config.Image) {
|
2014-07-31 16:36:42 -04:00
|
|
|
_, tag := parsers.ParseRepositoryTag(config.Image)
|
|
|
|
if tag == "" {
|
|
|
|
tag = graph.DEFAULTTAG
|
|
|
|
}
|
2015-04-09 17:49:22 -04:00
|
|
|
return "", warnings, fmt.Errorf("No such image: %s (tag: %s)", config.Image, tag)
|
2014-07-31 16:36:42 -04:00
|
|
|
}
|
2015-04-09 17:49:22 -04:00
|
|
|
return "", 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-04-09 17:49:22 -04:00
|
|
|
return 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.
|
2014-09-25 17:23:59 -04:00
|
|
|
func (daemon *Daemon) Create(config *runconfig.Config, hostConfig *runconfig.HostConfig, name string) (*Container, []string, error) {
|
2014-07-31 16:36:42 -04:00
|
|
|
var (
|
|
|
|
container *Container
|
|
|
|
warnings []string
|
2014-10-28 17:06:23 -04:00
|
|
|
img *image.Image
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if err = img.CheckDepth(); err != nil {
|
|
|
|
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-04-16 02:31:52 -04:00
|
|
|
if !config.NetworkDisabled && daemon.SystemConfig().IPv4ForwardingDisabled {
|
2015-05-28 05:08:55 -04:00
|
|
|
warnings = append(warnings, "IPv4 forwarding is disabled.")
|
2015-04-16 02:31:52 -04:00
|
|
|
}
|
2015-01-19 12:09:02 -05:00
|
|
|
if hostConfig == nil {
|
|
|
|
hostConfig = &runconfig.HostConfig{}
|
|
|
|
}
|
|
|
|
if hostConfig.SecurityOpt == nil {
|
2014-11-25 15:10:53 -05: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
|
|
|
|
}
|
|
|
|
}
|
2014-10-28 17:06:23 -04:00
|
|
|
if container, err = daemon.newContainer(name, config, imgID); err != nil {
|
2014-07-31 16:36:42 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
2014-09-29 20:06:26 -04:00
|
|
|
if err := daemon.Register(container); err != nil {
|
|
|
|
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-05-19 16:05:25 -04:00
|
|
|
if err := daemon.setHostConfig(container, hostConfig); err != nil {
|
|
|
|
return nil, nil, err
|
2014-09-25 17:23:59 -04:00
|
|
|
}
|
2014-11-11 11:17:33 -05:00
|
|
|
if err := container.Mount(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer container.Unmount()
|
2015-05-19 16:05:25 -04:00
|
|
|
|
|
|
|
for spec := range config.Volumes {
|
|
|
|
var (
|
|
|
|
name, destination string
|
|
|
|
parts = strings.Split(spec, ":")
|
|
|
|
)
|
|
|
|
switch len(parts) {
|
|
|
|
case 2:
|
|
|
|
name, destination = parts[0], filepath.Clean(parts[1])
|
|
|
|
default:
|
|
|
|
name = stringid.GenerateRandomID()
|
|
|
|
destination = filepath.Clean(parts[0])
|
|
|
|
}
|
|
|
|
// Skip volumes for which we already have something mounted on that
|
|
|
|
// destination because of a --volume-from.
|
2015-05-20 17:53:53 -04:00
|
|
|
if container.isDestinationMounted(destination) {
|
2015-05-19 16:05:25 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
path, err := container.GetResourcePath(destination)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-05-22 13:37:00 -04:00
|
|
|
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err == nil && !stat.IsDir() {
|
2015-05-19 16:05:25 -04:00
|
|
|
return nil, nil, fmt.Errorf("cannot mount volume over existing file, file exists %s", path)
|
|
|
|
}
|
2015-05-22 13:37:00 -04:00
|
|
|
|
2015-05-19 16:05:25 -04:00
|
|
|
v, err := createVolume(name, config.VolumeDriver)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-05-27 15:29:49 -04:00
|
|
|
if err := label.Relabel(v.Path(), container.MountLabel, "z"); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-05-22 13:37:00 -04:00
|
|
|
|
|
|
|
if err := container.copyImagePathContent(v, destination); err != nil {
|
2015-05-19 16:05:25 -04:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:53:53 -04:00
|
|
|
container.addMountPointWithVolume(destination, v, true)
|
2014-11-11 11:17:33 -05:00
|
|
|
}
|
2014-07-31 16:36:42 -04:00
|
|
|
if err := container.ToDisk(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2015-05-24 14:19:39 -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
|
|
|
|
2014-11-25 15:10:53 -05:00
|
|
|
func (daemon *Daemon) GenerateSecurityOpt(ipcMode runconfig.IpcMode, pidMode runconfig.PidMode) ([]string, error) {
|
|
|
|
if ipcMode.IsHost() || pidMode.IsHost() {
|
2014-11-10 16:14:17 -05:00
|
|
|
return label.DisableSecOpt(), nil
|
|
|
|
}
|
|
|
|
if ipcContainer := ipcMode.Container(); ipcContainer != "" {
|
2014-12-16 18:06:35 -05:00
|
|
|
c, err := daemon.Get(ipcContainer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-11-10 16:14:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return label.DupSecOpt(c.ProcessLabel), nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|