2018-02-05 21:05:59 +00:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-07-31 20:57:21 +00:00
|
|
|
|
|
|
|
import (
|
2022-10-26 16:13:17 +00:00
|
|
|
"context"
|
2017-05-20 02:38:45 +00:00
|
|
|
"fmt"
|
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
|
|
|
|
2023-08-30 16:31:46 +00:00
|
|
|
"github.com/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"
|
2023-08-26 13:24:46 +00:00
|
|
|
"github.com/docker/docker/api/types/events"
|
2016-03-16 23:07:41 +00:00
|
|
|
"github.com/docker/docker/builder/dockerfile"
|
2018-01-11 19:53:06 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
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
|
2022-11-01 11:52:44 +00:00
|
|
|
imageEnvKey, _, _ := strings.Cut(imageEnv, "=")
|
2016-01-05 16:48:09 +00:00
|
|
|
for _, userEnv := range userConf.Env {
|
2022-11-01 11:52:44 +00:00
|
|
|
userEnvKey, _, _ := strings.Cut(userEnv, "=")
|
2019-10-13 00:29:21 +00:00
|
|
|
if isWindows {
|
2016-11-22 19:26:02 +00:00
|
|
|
// Case insensitive environment variables on Windows
|
2022-11-01 11:52:44 +00:00
|
|
|
found = strings.EqualFold(imageEnvKey, userEnvKey)
|
|
|
|
} else {
|
|
|
|
found = imageEnvKey == userEnvKey
|
2016-11-22 19:26:02 +00:00
|
|
|
}
|
2022-11-01 11:52:44 +00:00
|
|
|
if found {
|
2016-01-05 16:48:09 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-05-02 21:06:18 +00:00
|
|
|
if userConf.Healthcheck.StartInterval == 0 {
|
|
|
|
userConf.Healthcheck.StartInterval = imageConf.Healthcheck.StartInterval
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2018-02-06 18:27:55 +00: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.
|
2022-10-26 16:13:17 +00:00
|
|
|
func (daemon *Daemon) CreateImageFromContainer(ctx context.Context, name string, c *backend.CreateImageConfig) (string, error) {
|
2016-07-20 23:11:28 +00:00
|
|
|
start := time.Now()
|
2023-01-10 13:29:40 +00:00
|
|
|
|
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
|
2019-10-13 00:29:21 +00:00
|
|
|
if isWindows && 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() {
|
2023-04-29 00:59:54 +00:00
|
|
|
return "", errdefs.Conflict(fmt.Errorf("You cannot commit container %s which is Dead", container.ID))
|
2017-05-20 02:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if container.IsRemovalInProgress() {
|
2023-04-29 00:59:54 +00:00
|
|
|
return "", errdefs.Conflict(fmt.Errorf("You cannot commit container %s which is being removed", container.ID))
|
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
|
|
|
}
|
|
|
|
|
2018-02-06 18:27:55 +00:00
|
|
|
if c.Config == nil {
|
2017-11-22 21:31:26 +00:00
|
|
|
c.Config = container.Config
|
|
|
|
}
|
2022-10-26 16:13:17 +00:00
|
|
|
newConfig, err := dockerfile.BuildFromConfig(ctx, c.Config, c.Changes, container.OS)
|
2016-03-16 23:07:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-02-06 18:27:55 +00:00
|
|
|
if err := merge(newConfig, container.Config); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-03-16 23:07:41 +00:00
|
|
|
|
2023-01-12 14:14:12 +00:00
|
|
|
id, err := daemon.imageService.CommitImage(ctx, backend.CommitConfig{
|
2018-02-06 18:27:55 +00: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 18:05:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 13:29:40 +00:00
|
|
|
imageRef := ""
|
2023-01-31 09:27:09 +00:00
|
|
|
if c.Tag != nil {
|
|
|
|
err = daemon.imageService.TagImage(ctx, id, c.Tag)
|
2018-02-09 23:24:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-01-31 09:27:09 +00:00
|
|
|
imageRef = reference.FamiliarString(c.Tag)
|
2018-02-06 18:27:55 +00:00
|
|
|
}
|
2023-08-26 13:24:46 +00:00
|
|
|
daemon.LogContainerEventWithAttributes(container, events.ActionCommit, map[string]string{
|
2018-02-06 18:27:55 +00:00
|
|
|
"comment": c.Comment,
|
|
|
|
"imageID": id.String(),
|
|
|
|
"imageRef": imageRef,
|
|
|
|
})
|
|
|
|
containerActions.WithValues("commit").UpdateSince(start)
|
|
|
|
return id.String(), nil
|
|
|
|
}
|