From b30257ccf96f646c6b008312d39544d6700f5cb1 Mon Sep 17 00:00:00 2001 From: "Daniel, Dao Quang Minh" Date: Wed, 1 Oct 2014 05:35:44 -0400 Subject: [PATCH] support `changes` in commit job In addition to config env, `commit` now will also accepts a `changes` env which is a string contains new-line separated Dockerfile instructions. `commit` will evaluate `changes` into `runconfig.Config` and merge it with `config` env, and then finally commit a new image with the changed config Docker-DCO-1.1-Signed-off-by: Daniel, Dao Quang Minh (github: dqminh) Docker-DCO-1.1-Signed-off-by: Daniel, Dao Quang Minh (github: rhatdan) --- daemon/commit.go | 19 ++++++++++--- integration-cli/docker_cli_commit_test.go | 33 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/daemon/commit.go b/daemon/commit.go index 7c83c60cc4..f253a94d82 100644 --- a/daemon/commit.go +++ b/daemon/commit.go @@ -1,6 +1,9 @@ package daemon import ( + "bytes" + "encoding/json" + "github.com/docker/docker/engine" "github.com/docker/docker/image" "github.com/docker/docker/runconfig" @@ -18,11 +21,21 @@ func (daemon *Daemon) ContainerCommit(job *engine.Job) engine.Status { } var ( - config = container.Config - newConfig runconfig.Config + config = container.Config + stdoutBuffer = bytes.NewBuffer(nil) + newConfig runconfig.Config ) - if err := job.GetenvJson("config", &newConfig); err != nil { + buildConfigJob := daemon.eng.Job("build_config") + buildConfigJob.Stdout.Add(stdoutBuffer) + buildConfigJob.Setenv("changes", job.Getenv("changes")) + // FIXME this should be remove when we remove deprecated config param + buildConfigJob.Setenv("config", job.Getenv("config")) + + if err := buildConfigJob.Run(); err != nil { + return job.Error(err) + } + if err := json.NewDecoder(stdoutBuffer).Decode(&newConfig); err != nil { return job.Error(err) } diff --git a/integration-cli/docker_cli_commit_test.go b/integration-cli/docker_cli_commit_test.go index 18eca54444..95a34b9169 100644 --- a/integration-cli/docker_cli_commit_test.go +++ b/integration-cli/docker_cli_commit_test.go @@ -202,3 +202,36 @@ func TestCommitWithHostBindMount(t *testing.T) { logDone("commit - commit bind mounted file") } + +func TestCommitChange(t *testing.T) { + defer deleteAllContainers() + cmd(t, "run", "--name", "test", "busybox", "true") + + cmd := exec.Command(dockerBinary, "commit", + "--change", "EXPOSE 8080", + "--change", "ENV DEBUG true", + "test", "test-commit") + imageId, _, err := runCommandWithOutput(cmd) + if err != nil { + t.Fatal(imageId, err) + } + imageId = strings.Trim(imageId, "\r\n") + defer deleteImages(imageId) + + expected := map[string]string{ + "Config.ExposedPorts": "map[8080/tcp:map[]]", + "Config.Env": "[DEBUG=true PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]", + } + + for conf, value := range expected { + res, err := inspectField(imageId, conf) + if err != nil { + t.Errorf("failed to get value %s, error: %s", conf, err) + } + if res != value { + t.Errorf("%s('%s'), expected %s", conf, res, value) + } + } + + logDone("commit - commit --change") +}