2014-07-31 20:57:21 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-11-18 22:20:54 +00:00
|
|
|
"encoding/json"
|
2017-05-20 02:38:45 +00:00
|
|
|
"fmt"
|
2016-10-20 23:40:59 +00:00
|
|
|
"io"
|
2015-11-06 18:05:00 +00:00
|
|
|
"runtime"
|
2015-11-18 22:20:54 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2015-11-06 18:05:00 +00:00
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-03-16 23:07:41 +00:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2016-09-06 18:18:12 +00:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2016-03-16 23:07:41 +00:00
|
|
|
"github.com/docker/docker/builder/dockerfile"
|
2015-11-12 19:55:17 +00:00
|
|
|
"github.com/docker/docker/container"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2015-07-20 17:57:15 +00:00
|
|
|
"github.com/docker/docker/image"
|
2015-11-18 22:20:54 +00:00
|
|
|
"github.com/docker/docker/layer"
|
2015-11-02 22:37:45 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2014-07-31 20:57:21 +00:00
|
|
|
)
|
|
|
|
|
2016-01-05 16:48:09 +00: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 19:26:02 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Case insensitive environment variables on Windows
|
|
|
|
imageEnvKey = strings.ToUpper(imageEnvKey)
|
|
|
|
userEnvKey = strings.ToUpper(userEnvKey)
|
|
|
|
}
|
2016-01-05 16:48:09 +00: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 17:56:20 +00:00
|
|
|
for l, v := range imageConf.Labels {
|
|
|
|
if _, ok := userConf.Labels[l]; !ok {
|
|
|
|
userConf.Labels[l] = v
|
2016-01-05 16:48:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-29 11:28:37 +00:00
|
|
|
if len(userConf.Entrypoint) == 0 {
|
|
|
|
if len(userConf.Cmd) == 0 {
|
2016-01-05 16:48:09 +00:00
|
|
|
userConf.Cmd = imageConf.Cmd
|
2016-05-20 23:05:14 +00:00
|
|
|
userConf.ArgsEscaped = imageConf.ArgsEscaped
|
2016-01-05 16:48:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if userConf.Entrypoint == nil {
|
|
|
|
userConf.Entrypoint = imageConf.Entrypoint
|
|
|
|
}
|
|
|
|
}
|
2016-04-18 09:48:13 +00: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 09:58:47 +00:00
|
|
|
if userConf.Healthcheck.StartPeriod == 0 {
|
|
|
|
userConf.Healthcheck.StartPeriod = imageConf.Healthcheck.StartPeriod
|
|
|
|
}
|
2016-04-18 09:48:13 +00:00
|
|
|
if userConf.Healthcheck.Retries == 0 {
|
|
|
|
userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:48:09 +00: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 22:56:40 +00:00
|
|
|
|
|
|
|
if userConf.StopSignal == "" {
|
|
|
|
userConf.StopSignal = imageConf.StopSignal
|
|
|
|
}
|
2016-01-05 16:48:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 20:57:21 +00:00
|
|
|
// Commit creates a new filesystem image from the current state of a container.
|
2015-07-30 21:01:53 +00:00
|
|
|
// The image can optionally be tagged into a repository.
|
2016-03-16 23:07:41 +00:00
|
|
|
func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (string, error) {
|
2016-07-20 23:11:28 +00:00
|
|
|
start := time.Now()
|
2015-12-11 17:39:28 +00:00
|
|
|
container, err := daemon.GetContainer(name)
|
2015-11-06 18:05:00 +00:00
|
|
|
if err != nil {
|
2015-11-18 22:20:54 +00:00
|
|
|
return "", err
|
2015-11-06 18:05:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 18:32:52 +00:00
|
|
|
// It is not possible to commit a running container on Windows
|
|
|
|
if (runtime.GOOS == "windows") && container.IsRunning() {
|
2017-01-26 00:54:18 +00:00
|
|
|
return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
|
2015-11-06 18:05:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:38:45 +00:00
|
|
|
if container.IsDead() {
|
|
|
|
err := fmt.Errorf("You cannot commit container %s which is Dead", container.ID)
|
2017-11-29 04:09:37 +00:00
|
|
|
return "", errdefs.Conflict(err)
|
2017-05-20 02:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if container.IsRemovalInProgress() {
|
|
|
|
err := fmt.Errorf("You cannot commit container %s which is being removed", container.ID)
|
2017-11-29 04:09:37 +00:00
|
|
|
return "", errdefs.Conflict(err)
|
2017-05-20 02:38:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
if c.Pause && !container.IsPaused() {
|
2015-11-02 23:39:39 +00:00
|
|
|
daemon.containerPause(container)
|
|
|
|
defer daemon.containerUnpause(container)
|
2014-07-31 20:57:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 21:31:26 +00:00
|
|
|
if c.MergeConfigs && c.Config == nil {
|
|
|
|
c.Config = container.Config
|
|
|
|
}
|
|
|
|
|
2016-03-16 23:07:41 +00:00
|
|
|
newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-11-06 18:05:00 +00:00
|
|
|
if c.MergeConfigs {
|
2016-03-16 23:07:41 +00:00
|
|
|
if err := merge(newConfig, container.Config); err != nil {
|
2015-11-18 22:20:54 +00:00
|
|
|
return "", err
|
2015-11-06 18:05:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-02 22:37:45 +00:00
|
|
|
rwTar, err := daemon.exportContainerRw(container)
|
2014-07-31 20:57:21 +00:00
|
|
|
if err != nil {
|
2015-11-18 22:20:54 +00:00
|
|
|
return "", err
|
2014-07-31 20:57:21 +00:00
|
|
|
}
|
2015-04-24 22:03:53 +00:00
|
|
|
defer func() {
|
|
|
|
if rwTar != nil {
|
|
|
|
rwTar.Close()
|
|
|
|
}
|
|
|
|
}()
|
2014-07-31 20:57:21 +00:00
|
|
|
|
2017-05-14 18:18:48 +00:00
|
|
|
var parent *image.Image
|
|
|
|
if container.ImageID == "" {
|
|
|
|
parent = new(image.Image)
|
|
|
|
parent.RootFS = image.NewRootFS()
|
|
|
|
} else {
|
2017-08-08 19:43:48 +00:00
|
|
|
parent, err = daemon.stores[container.OS].imageStore.Get(container.ImageID)
|
2015-11-18 22:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-08 19:43:48 +00:00
|
|
|
l, err := daemon.stores[container.OS].layerStore.Register(rwTar, parent.RootFS.ChainID(), layer.OS(container.OS))
|
2014-07-31 20:57:21 +00:00
|
|
|
if err != nil {
|
2015-11-18 22:20:54 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2017-08-08 19:43:48 +00:00
|
|
|
defer layer.ReleaseAndLog(daemon.stores[container.OS].layerStore, l)
|
2015-11-18 22:20:54 +00:00
|
|
|
|
2017-05-14 18:18:48 +00: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 19:43:48 +00:00
|
|
|
config, err := json.Marshal(image.NewChildImage(parent, cc, container.OS))
|
2015-11-18 22:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-08-08 19:43:48 +00:00
|
|
|
id, err := daemon.stores[container.OS].imageStore.Create(config)
|
2015-11-18 22:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if container.ImageID != "" {
|
2017-08-08 19:43:48 +00:00
|
|
|
if err := daemon.stores[container.OS].imageStore.SetParent(id, container.ImageID); err != nil {
|
2015-11-18 22:20:54 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2014-07-31 20:57:21 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 16:50:46 +00:00
|
|
|
imageRef := ""
|
2015-06-20 10:40:37 +00:00
|
|
|
if c.Repo != "" {
|
2017-01-26 00:54:18 +00:00
|
|
|
newTag, err := reference.ParseNormalizedNamed(c.Repo) // todo: should move this to API layer
|
2015-11-18 22:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-01-26 00:54:18 +00:00
|
|
|
if !reference.IsNameOnly(newTag) {
|
|
|
|
return "", errors.Errorf("unexpected repository name: %s", c.Repo)
|
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
if c.Tag != "" {
|
|
|
|
if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 19:43:48 +00:00
|
|
|
if err := daemon.TagImageWithReference(id, container.OS, newTag); err != nil {
|
2015-11-18 22:20:54 +00:00
|
|
|
return "", err
|
2014-07-31 20:57:21 +00:00
|
|
|
}
|
2017-01-26 00:54:18 +00:00
|
|
|
imageRef = reference.FamiliarString(newTag)
|
2014-07-31 20:57:21 +00:00
|
|
|
}
|
2015-11-03 17:33:13 +00:00
|
|
|
|
2016-01-07 22:14:05 +00:00
|
|
|
attributes := map[string]string{
|
2016-11-17 06:17:40 +00:00
|
|
|
"comment": c.Comment,
|
|
|
|
"imageID": id.String(),
|
2016-11-07 16:50:46 +00:00
|
|
|
"imageRef": imageRef,
|
2016-01-07 22:14:05 +00:00
|
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(container, "commit", attributes)
|
2016-07-20 23:11:28 +00:00
|
|
|
containerActions.WithValues("commit").UpdateSince(start)
|
2015-11-18 22:20:54 +00:00
|
|
|
return id.String(), nil
|
2014-07-31 20:57:21 +00:00
|
|
|
}
|
2015-11-02 22:37:45 +00:00
|
|
|
|
2017-05-20 02:38:45 +00:00
|
|
|
func (daemon *Daemon) exportContainerRw(container *container.Container) (arch io.ReadCloser, err error) {
|
2017-08-08 19:43:48 +00:00
|
|
|
rwlayer, err := daemon.stores[container.OS].layerStore.GetRWLayer(container.ID)
|
2017-05-20 02:38:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2017-08-08 19:43:48 +00:00
|
|
|
daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
|
2017-05-20 02:38:45 +00: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 22:20:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:38:45 +00:00
|
|
|
archive, err := rwlayer.TarStream()
|
2015-11-02 22:37:45 +00:00
|
|
|
if err != nil {
|
2017-05-20 02:38:45 +00:00
|
|
|
rwlayer.Unmount()
|
2015-11-02 22:37:45 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutils.NewReadCloserWrapper(archive, func() error {
|
2015-11-26 00:39:54 +00:00
|
|
|
archive.Close()
|
2017-05-20 02:38:45 +00:00
|
|
|
err = rwlayer.Unmount()
|
2017-08-08 19:43:48 +00:00
|
|
|
daemon.stores[container.OS].layerStore.ReleaseRWLayer(rwlayer)
|
2017-05-20 02:38:45 +00:00
|
|
|
return err
|
2015-11-02 22:37:45 +00:00
|
|
|
}),
|
|
|
|
nil
|
|
|
|
}
|