2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-07-31 16:57:21 -04:00
|
|
|
|
|
|
|
import (
|
2017-05-19 22:38:45 -04:00
|
|
|
"fmt"
|
2015-11-06 13:05:00 -05:00
|
|
|
"runtime"
|
2015-11-18 17:20:54 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
2015-11-06 13:05:00 -05:00
|
|
|
|
2016-03-16 19:07:41 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 14:18:12 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2016-03-16 19:07:41 -04:00
|
|
|
"github.com/docker/docker/builder/dockerfile"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/pkg/errors"
|
2014-07-31 16:57:21 -04:00
|
|
|
)
|
|
|
|
|
2016-01-05 11:48:09 -05:00
|
|
|
// merge merges two Config, the image container configuration (defaults values),
|
|
|
|
// and the user container configuration, either passed by the API or generated
|
|
|
|
// by the cli.
|
|
|
|
// It will mutate the specified user configuration (userConf) with the image
|
|
|
|
// configuration where the user configuration is incomplete.
|
|
|
|
func merge(userConf, imageConf *containertypes.Config) error {
|
|
|
|
if userConf.User == "" {
|
|
|
|
userConf.User = imageConf.User
|
|
|
|
}
|
|
|
|
if len(userConf.ExposedPorts) == 0 {
|
|
|
|
userConf.ExposedPorts = imageConf.ExposedPorts
|
|
|
|
} else if imageConf.ExposedPorts != nil {
|
|
|
|
for port := range imageConf.ExposedPorts {
|
|
|
|
if _, exists := userConf.ExposedPorts[port]; !exists {
|
|
|
|
userConf.ExposedPorts[port] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(userConf.Env) == 0 {
|
|
|
|
userConf.Env = imageConf.Env
|
|
|
|
} else {
|
|
|
|
for _, imageEnv := range imageConf.Env {
|
|
|
|
found := false
|
|
|
|
imageEnvKey := strings.Split(imageEnv, "=")[0]
|
|
|
|
for _, userEnv := range userConf.Env {
|
|
|
|
userEnvKey := strings.Split(userEnv, "=")[0]
|
2016-11-22 14:26:02 -05:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Case insensitive environment variables on Windows
|
|
|
|
imageEnvKey = strings.ToUpper(imageEnvKey)
|
|
|
|
userEnvKey = strings.ToUpper(userEnvKey)
|
|
|
|
}
|
2016-01-05 11:48:09 -05:00
|
|
|
if imageEnvKey == userEnvKey {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
userConf.Env = append(userConf.Env, imageEnv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if userConf.Labels == nil {
|
|
|
|
userConf.Labels = map[string]string{}
|
|
|
|
}
|
2016-12-19 12:56:20 -05:00
|
|
|
for l, v := range imageConf.Labels {
|
|
|
|
if _, ok := userConf.Labels[l]; !ok {
|
|
|
|
userConf.Labels[l] = v
|
2016-01-05 11:48:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 06:28:37 -05:00
|
|
|
if len(userConf.Entrypoint) == 0 {
|
|
|
|
if len(userConf.Cmd) == 0 {
|
2016-01-05 11:48:09 -05:00
|
|
|
userConf.Cmd = imageConf.Cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
if userConf.Entrypoint == nil {
|
|
|
|
userConf.Entrypoint = imageConf.Entrypoint
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 05:48:13 -04:00
|
|
|
if imageConf.Healthcheck != nil {
|
|
|
|
if userConf.Healthcheck == nil {
|
|
|
|
userConf.Healthcheck = imageConf.Healthcheck
|
|
|
|
} else {
|
|
|
|
if len(userConf.Healthcheck.Test) == 0 {
|
|
|
|
userConf.Healthcheck.Test = imageConf.Healthcheck.Test
|
|
|
|
}
|
|
|
|
if userConf.Healthcheck.Interval == 0 {
|
|
|
|
userConf.Healthcheck.Interval = imageConf.Healthcheck.Interval
|
|
|
|
}
|
|
|
|
if userConf.Healthcheck.Timeout == 0 {
|
|
|
|
userConf.Healthcheck.Timeout = imageConf.Healthcheck.Timeout
|
|
|
|
}
|
2016-11-29 04:58:47 -05:00
|
|
|
if userConf.Healthcheck.StartPeriod == 0 {
|
|
|
|
userConf.Healthcheck.StartPeriod = imageConf.Healthcheck.StartPeriod
|
|
|
|
}
|
2016-04-18 05:48:13 -04:00
|
|
|
if userConf.Healthcheck.Retries == 0 {
|
|
|
|
userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 11:48:09 -05:00
|
|
|
if userConf.WorkingDir == "" {
|
|
|
|
userConf.WorkingDir = imageConf.WorkingDir
|
|
|
|
}
|
|
|
|
if len(userConf.Volumes) == 0 {
|
|
|
|
userConf.Volumes = imageConf.Volumes
|
|
|
|
} else {
|
|
|
|
for k, v := range imageConf.Volumes {
|
|
|
|
userConf.Volumes[k] = v
|
|
|
|
}
|
|
|
|
}
|
2016-02-12 17:56:40 -05:00
|
|
|
|
|
|
|
if userConf.StopSignal == "" {
|
|
|
|
userConf.StopSignal = imageConf.StopSignal
|
|
|
|
}
|
2016-01-05 11:48:09 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-06 13:27:55 -05:00
|
|
|
// CreateImageFromContainer creates a new image from a container. The container
|
|
|
|
// config will be updated by applying the change set to the custom config, then
|
|
|
|
// applying that config over the existing container config.
|
|
|
|
func (daemon *Daemon) CreateImageFromContainer(name string, c *backend.CreateImageConfig) (string, error) {
|
2016-07-20 19:11:28 -04:00
|
|
|
start := time.Now()
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := daemon.GetContainer(name)
|
2015-11-06 13:05:00 -05:00
|
|
|
if err != nil {
|
2015-11-18 17:20:54 -05:00
|
|
|
return "", err
|
2015-11-06 13:05:00 -05:00
|
|
|
}
|
|
|
|
|
2017-10-24 14:32:52 -04:00
|
|
|
// It is not possible to commit a running container on Windows
|
|
|
|
if (runtime.GOOS == "windows") && container.IsRunning() {
|
2017-01-25 19:54:18 -05:00
|
|
|
return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
|
2015-11-06 13:05:00 -05:00
|
|
|
}
|
|
|
|
|
2017-05-19 22:38:45 -04:00
|
|
|
if container.IsDead() {
|
|
|
|
err := fmt.Errorf("You cannot commit container %s which is Dead", container.ID)
|
2017-11-28 23:09:37 -05:00
|
|
|
return "", errdefs.Conflict(err)
|
2017-05-19 22:38:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if container.IsRemovalInProgress() {
|
|
|
|
err := fmt.Errorf("You cannot commit container %s which is being removed", container.ID)
|
2017-11-28 23:09:37 -05:00
|
|
|
return "", errdefs.Conflict(err)
|
2017-05-19 22:38:45 -04:00
|
|
|
}
|
|
|
|
|
2015-11-12 14:55:17 -05:00
|
|
|
if c.Pause && !container.IsPaused() {
|
2015-11-02 18:39:39 -05:00
|
|
|
daemon.containerPause(container)
|
|
|
|
defer daemon.containerUnpause(container)
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
2018-02-06 13:27:55 -05:00
|
|
|
if c.Config == nil {
|
2017-11-22 16:31:26 -05:00
|
|
|
c.Config = container.Config
|
|
|
|
}
|
2017-09-19 15:14:46 -04:00
|
|
|
newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes, container.OS)
|
2016-03-16 19:07:41 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-02-06 13:27:55 -05:00
|
|
|
if err := merge(newConfig, container.Config); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-03-16 19:07:41 -04:00
|
|
|
|
2018-02-02 17:18:46 -05:00
|
|
|
id, err := daemon.imageService.CommitImage(backend.CommitConfig{
|
2018-02-06 13:27:55 -05:00
|
|
|
Author: c.Author,
|
|
|
|
Comment: c.Comment,
|
|
|
|
Config: newConfig,
|
|
|
|
ContainerConfig: container.Config,
|
|
|
|
ContainerID: container.ID,
|
|
|
|
ContainerMountLabel: container.MountLabel,
|
|
|
|
ContainerOS: container.OS,
|
|
|
|
ParentImageID: string(container.ImageID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2015-11-06 13:05:00 -05:00
|
|
|
}
|
|
|
|
|
2018-02-09 18:24:57 -05:00
|
|
|
var imageRef string
|
|
|
|
if c.Repo != "" {
|
2018-02-02 17:18:46 -05:00
|
|
|
imageRef, err = daemon.imageService.TagImage(string(id), c.Repo, c.Tag)
|
2018-02-09 18:24:57 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-02-06 13:27:55 -05:00
|
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(container, "commit", map[string]string{
|
|
|
|
"comment": c.Comment,
|
|
|
|
"imageID": id.String(),
|
|
|
|
"imageRef": imageRef,
|
|
|
|
})
|
|
|
|
containerActions.WithValues("commit").UpdateSince(start)
|
|
|
|
return id.String(), nil
|
|
|
|
}
|