10 "github.com/docker/distribution/reference"
11 "github.com/docker/docker/api/types/backend"
12 containertypes "github.com/docker/docker/api/types/container"
13 "github.com/docker/docker/builder/dockerfile"
14 "github.com/docker/docker/container"
15 "github.com/docker/docker/image"
16 "github.com/docker/docker/layer"
17 "github.com/docker/docker/pkg/ioutils"
18 "github.com/pkg/errors"
21 // merge merges two Config, the image container configuration (defaults values),
22 // and the user container configuration, either passed by the API or generated
24 // It will mutate the specified user configuration (userConf) with the image
25 // configuration where the user configuration is incomplete.
26 func merge(userConf, imageConf *containertypes.Config) error {
27 if userConf.User == "" {
28 userConf.User = imageConf.User
30 if len(userConf.ExposedPorts) == 0 {
31 userConf.ExposedPorts = imageConf.ExposedPorts
32 } else if imageConf.ExposedPorts != nil {
33 for port := range imageConf.ExposedPorts {
34 if _, exists := userConf.ExposedPorts[port]; !exists {
35 userConf.ExposedPorts[port] = struct{}{}
40 if len(userConf.Env) == 0 {
41 userConf.Env = imageConf.Env
43 for _, imageEnv := range imageConf.Env {
45 imageEnvKey := strings.Split(imageEnv, "=")[0]
46 for _, userEnv := range userConf.Env {
47 userEnvKey := strings.Split(userEnv, "=")[0]
48 if runtime.GOOS == "windows" {
49 // Case insensitive environment variables on Windows
50 imageEnvKey = strings.ToUpper(imageEnvKey)
51 userEnvKey = strings.ToUpper(userEnvKey)
53 if imageEnvKey == userEnvKey {
59 userConf.Env = append(userConf.Env, imageEnv)
64 if userConf.Labels == nil {
65 userConf.Labels = map[string]string{}
67 for l, v := range imageConf.Labels {
68 if _, ok := userConf.Labels[l]; !ok {
69 userConf.Labels[l] = v
73 if len(userConf.Entrypoint) == 0 {
74 if len(userConf.Cmd) == 0 {
75 userConf.Cmd = imageConf.Cmd
76 userConf.ArgsEscaped = imageConf.ArgsEscaped
79 if userConf.Entrypoint == nil {
80 userConf.Entrypoint = imageConf.Entrypoint
83 if imageConf.Healthcheck != nil {
84 if userConf.Healthcheck == nil {
85 userConf.Healthcheck = imageConf.Healthcheck
87 if len(userConf.Healthcheck.Test) == 0 {
88 userConf.Healthcheck.Test = imageConf.Healthcheck.Test
90 if userConf.Healthcheck.Interval == 0 {
91 userConf.Healthcheck.Interval = imageConf.Healthcheck.Interval
93 if userConf.Healthcheck.Timeout == 0 {
94 userConf.Healthcheck.Timeout = imageConf.Healthcheck.Timeout
96 if userConf.Healthcheck.StartPeriod == 0 {
97 userConf.Healthcheck.StartPeriod = imageConf.Healthcheck.StartPeriod
99 if userConf.Healthcheck.Retries == 0 {
100 userConf.Healthcheck.Retries = imageConf.Healthcheck.Retries
105 if userConf.WorkingDir == "" {
106 userConf.WorkingDir = imageConf.WorkingDir
108 if len(userConf.Volumes) == 0 {
109 userConf.Volumes = imageConf.Volumes
111 for k, v := range imageConf.Volumes {
112 userConf.Volumes[k] = v
116 if userConf.StopSignal == "" {
117 userConf.StopSignal = imageConf.StopSignal
122 // Commit creates a new filesystem image from the current state of a container.
123 // The image can optionally be tagged into a repository.
124 func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (string, error) {
126 container, err := daemon.GetContainer(name)
131 // It is not possible to commit a running container on Windows and on Solaris.
132 if (runtime.GOOS == "windows" || runtime.GOOS == "solaris") && container.IsRunning() {
133 return "", errors.Errorf("%+v does not support commit of a running container", runtime.GOOS)
136 if c.Pause && !container.IsPaused() {
137 daemon.containerPause(container)
138 defer daemon.containerUnpause(container)
141 newConfig, err := dockerfile.BuildFromConfig(c.Config, c.Changes)
147 if err := merge(newConfig, container.Config); err != nil {
152 rwTar, err := daemon.exportContainerRw(container)
162 var parent *image.Image
163 if container.ImageID == "" {
164 parent = new(image.Image)
165 parent.RootFS = image.NewRootFS()
167 parent, err = daemon.stores[container.Platform].imageStore.Get(container.ImageID)
173 l, err := daemon.stores[container.Platform].layerStore.Register(rwTar, parent.RootFS.ChainID(), layer.Platform(container.Platform))
177 defer layer.ReleaseAndLog(daemon.stores[container.Platform].layerStore, l)
179 containerConfig := c.ContainerConfig
180 if containerConfig == nil {
181 containerConfig = container.Config
183 cc := image.ChildConfig{
184 ContainerID: container.ID,
187 ContainerConfig: containerConfig,
191 config, err := json.Marshal(image.NewChildImage(parent, cc, container.Platform))
196 id, err := daemon.stores[container.Platform].imageStore.Create(config)
201 if container.ImageID != "" {
202 if err := daemon.stores[container.Platform].imageStore.SetParent(id, container.ImageID); err != nil {
209 newTag, err := reference.ParseNormalizedNamed(c.Repo) // todo: should move this to API layer
213 if !reference.IsNameOnly(newTag) {
214 return "", errors.Errorf("unexpected repository name: %s", c.Repo)
217 if newTag, err = reference.WithTag(newTag, c.Tag); err != nil {
221 if err := daemon.TagImageWithReference(id, container.Platform, newTag); err != nil {
224 imageRef = reference.FamiliarString(newTag)
227 attributes := map[string]string{
228 "comment": c.Comment,
229 "imageID": id.String(),
230 "imageRef": imageRef,
232 daemon.LogContainerEventWithAttributes(container, "commit", attributes)
233 containerActions.WithValues("commit").UpdateSince(start)
234 return id.String(), nil
237 func (daemon *Daemon) exportContainerRw(container *container.Container) (io.ReadCloser, error) {
238 if err := daemon.Mount(container); err != nil {
242 archive, err := container.RWLayer.TarStream()
244 daemon.Unmount(container) // logging is already handled in the `Unmount` function
247 return ioutils.NewReadCloserWrapper(archive, func() error {
249 return container.RWLayer.Unmount()