2014-07-31 16:57:21 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-11-18 17:20:54 -05:00
|
|
|
"encoding/json"
|
2017-05-19 22:38:45 -04:00
|
|
|
"fmt"
|
2016-10-20 19:40:59 -04:00
|
|
|
"io"
|
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
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
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"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
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/layer"
|
2015-11-02 17:37:45 -05:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
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
|
2016-05-20 19:05:14 -04:00
|
|
|
userConf.ArgsEscaped = imageConf.ArgsEscaped
|
2016-01-05 11:48:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-07-31 16:57:21 -04:00
|
|
|
// Commit creates a new filesystem image from the current state of a container.
|
2015-07-30 17:01:53 -04:00
|
|
|
// The image can optionally be tagged into a repository.
|
2016-03-16 19:07:41 -04:00
|
|
|
func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (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
|
|
|
}
|
|
|
|
|
2017-11-22 16:31:26 -05:00
|
|
|
if c.MergeConfigs && c.Config == nil {
|
|
|
|
c.Config = container.Config
|
|
|
|
}
|
|
|
|
|
2016-03-16 19:07:41 -04:00
|
|
|
newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-11-06 13:05:00 -05:00
|
|
|
if c.MergeConfigs {
|
2016-03-16 19:07:41 -04:00
|
|
|
if err := merge(newConfig, container.Config); err != nil {
|
2015-11-18 17:20:54 -05:00
|
|
|
return "", err
|
2015-11-06 13:05:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 17:37:45 -05:00
|
|
|
rwTar, err := daemon.exportContainerRw(container)
|
2014-07-31 16:57:21 -04:00
|
|
|
if err != nil {
|
2015-11-18 17:20:54 -05:00
|
|
|
return "", err
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
2015-04-24 18:03:53 -04:00
|
|
|
defer func() {
|
|
|
|
if rwTar != nil {
|
|
|
|
rwTar.Close()
|
|
|
|
}
|
|
|
|
}()
|
2014-07-31 16:57:21 -04:00
|
|
|
|
2017-05-14 14:18:48 -04:00
|
|
|
var parent *image.Image
|
|
|
|
if container.ImageID == "" {
|
|
|
|
parent = new(image.Image)
|
|
|
|
parent.RootFS = image.NewRootFS()
|
|
|
|
} else {
|
2017-08-08 15:43:48 -04:00
|
|
|
parent, err = daemon.stores[container.OS].imageStore.Get(container.ImageID)
|
2015-11-18 17:20:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:43:48 -04:00
|
|
|
l, err := daemon.stores[container.OS].layerStore.Register(rwTar, parent.RootFS.ChainID(), layer.OS(container.OS))
|
2014-07-31 16:57:21 -04:00
|
|
|
if err != nil {
|
2015-11-18 17:20:54 -05:00
|
|
|
return "", err
|
|
|
|
}
|
2017-08-08 15:43:48 -04:00
|
|
|
defer layer.ReleaseAndLog(daemon.stores[container.OS].layerStore, l)
|
2015-11-18 17:20:54 -05:00
|
|
|
|
2017-05-14 14:18:48 -04:00
|
|
|
containerConfig := c.ContainerConfig
|
|
|
|
if containerConfig == nil {
|
|
|
|
containerConfig = container.Config
|
|
|
|
}
|
|
|
|
cc := image.ChildConfig{
|
|
|
|
ContainerID: container.ID,
|
|
|
|
Author: c.Author,
|
|
|
|
Comment: c.Comment,
|
|
|
|
ContainerConfig: containerConfig,
|
|
|
|
Config: newConfig,
|
|
|
|
DiffID: l.DiffID(),
|
|
|
|
}
|
2017-08-08 15:43:48 -04:00
|
|
|
config, err := json.Marshal(image.NewChildImage(parent, cc, container.OS))
|
2015-11-18 17:20:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-08-08 15:43:48 -04:00
|
|
|
id, err := daemon.stores[container.OS].imageStore.Create(config)
|
2015-11-18 17:20:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.ImageID != "" {
|
2017-08-08 15:43:48 -04:00
|
|
|
if err := daemon.stores[container.OS].imageStore.SetParent(id, container.ImageID); err != nil {
|
2015-11-18 17:20:54 -05:00
|
|
|
return "", err
|
|
|
|
}
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
|
|
|
|
2016-11-07 11:50:46 -05:00
|
|
|
imageRef := ""
|
2015-06-20 06:40:37 -04:00
|
|
|
if c.Repo != "" {
|
2017-01-25 19:54:18 -05:00
|
|
|
newTag, err := reference.ParseNormalizedNamed(c.Repo) // todo: should move this to API layer
|
2015-11-18 17:20:54 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
if !reference.IsNameOnly(newTag) {
|
|
|
|
return "", errors.Errorf("unexpected repository name: %s", c.Repo)
|
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
if c.Tag != "" {
|
|
|
|
if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 15:43:48 -04:00
|
|
|
if err := daemon.TagImageWithReference(id, container.OS, newTag); err != nil {
|
2015-11-18 17:20:54 -05:00
|
|
|
return "", err
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
2017-01-25 19:54:18 -05:00
|
|
|
imageRef = reference.FamiliarString(newTag)
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
2015-11-03 12:33:13 -05:00
|
|
|
|
2016-01-07 17:14:05 -05:00
|
|
|
attributes := map[string]string{
|
2016-11-17 01:17:40 -05:00
|
|
|
"comment": c.Comment,
|
|
|
|
"imageID": id.String(),
|
2016-11-07 11:50:46 -05:00
|
|
|
"imageRef": imageRef,
|
2016-01-07 17:14:05 -05:00
|
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(container, "commit", attributes)
|
2016-07-20 19:11:28 -04:00
|
|
|
containerActions.WithValues("commit").UpdateSince(start)
|
2015-11-18 17:20:54 -05:00
|
|
|
return id.String(), nil
|
2014-07-31 16:57:21 -04:00
|
|
|
}
|
2015-11-02 17:37:45 -05:00
|
|
|
|
2017-05-19 22:38:45 -04:00
|
|
|
func (daemon *Daemon) exportContainerRw(container *container.Container) (arch io.ReadCloser, err error) {
|
2017-08-08 15:43:48 -04:00
|
|
|
rwlayer, err := daemon.stores[container.OS].layerStore.GetRWLayer(container.ID)
|
2017-05-19 22:38:45 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2017-08-08 15:43:48 -04:00
|
|
|
daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
|
2017-05-19 22:38:45 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// TODO: this mount call is not necessary as we assume that TarStream() should
|
|
|
|
// mount the layer if needed. But the Diff() function for windows requests that
|
|
|
|
// the layer should be mounted when calling it. So we reserve this mount call
|
|
|
|
// until windows driver can implement Diff() interface correctly.
|
|
|
|
_, err = rwlayer.Mount(container.GetMountLabel())
|
|
|
|
if err != nil {
|
2015-11-18 17:20:54 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-19 22:38:45 -04:00
|
|
|
archive, err := rwlayer.TarStream()
|
2015-11-02 17:37:45 -05:00
|
|
|
if err != nil {
|
2017-05-19 22:38:45 -04:00
|
|
|
rwlayer.Unmount()
|
2015-11-02 17:37:45 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutils.NewReadCloserWrapper(archive, func() error {
|
2015-11-25 19:39:54 -05:00
|
|
|
archive.Close()
|
2017-05-19 22:38:45 -04:00
|
|
|
err = rwlayer.Unmount()
|
2017-08-08 15:43:48 -04:00
|
|
|
daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
|
2017-05-19 22:38:45 -04:00
|
|
|
return err
|
2015-11-02 17:37:45 -05:00
|
|
|
}),
|
|
|
|
nil
|
|
|
|
}
|