Skip to content

Commit

Permalink
pass --change changes to the import job
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Dan Walsh <[email protected]> (github: rhatdan)
  • Loading branch information
rhatdan committed Feb 24, 2015
1 parent 5767548 commit 17abfc3
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 25 deletions.
12 changes: 9 additions & 3 deletions api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1156,6 +1156,8 @@ func (cli *DockerCli) CmdKill(args ...string) error {

func (cli *DockerCli) CmdImport(args ...string) error {
cmd := cli.Subcmd("import", "URL|- [REPOSITORY[:TAG]]", "Create an empty filesystem image and import the contents of the\ntarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then\noptionally tag it.", true)
flChanges := opts.NewListOpts(nil)
cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image.")
cmd.Require(flag.Min, 1)

utils.ParseFlags(cmd, args, true)
Expand All @@ -1168,7 +1170,9 @@ func (cli *DockerCli) CmdImport(args ...string) error {

v.Set("fromSrc", src)
v.Set("repo", repository)

for _, change := range flChanges.GetAll() {
v.Add("changes", change)
}
if cmd.NArg() == 3 {
fmt.Fprintf(cli.err, "[DEPRECATED] The format 'URL|- [REPOSITORY [TAG]]' has been deprecated. Please use URL|- [REPOSITORY[:TAG]]\n")
v.Set("tag", cmd.Arg(2))
Expand Down Expand Up @@ -1703,7 +1707,7 @@ func (cli *DockerCli) CmdCommit(args ...string) error {
flComment := cmd.String([]string{"m", "-message"}, "", "Commit message")
flAuthor := cmd.String([]string{"a", "#author", "-author"}, "", "Author (e.g., \"John Hannibal Smith <[email protected]>\")")
flChanges := opts.NewListOpts(nil)
cmd.Var(&flChanges, []string{"c", "-change"}, "Apply a modification before committing the image")
cmd.Var(&flChanges, []string{"c", "-change"}, "Apply Dockerfile instruction to the created image.")
// FIXME: --run is deprecated, it will be replaced with inline Dockerfile commands.
flConfig := cmd.String([]string{"#run", "#-run"}, "", "This option is deprecated and will be removed in a future version in favor of inline Dockerfile-compatible commands")
cmd.Require(flag.Max, 2)
Expand All @@ -1728,7 +1732,9 @@ func (cli *DockerCli) CmdCommit(args ...string) error {
v.Set("tag", tag)
v.Set("comment", *flComment)
v.Set("author", *flAuthor)
v.Set("changes", strings.Join(flChanges.GetAll(), "\n"))
for _, change := range flChanges.GetAll() {
v.Add("changes", change)
}

if *flPause != true {
v.Set("pause", "0")
Expand Down
3 changes: 2 additions & 1 deletion api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ func postCommit(eng *engine.Engine, version version.Version, w http.ResponseWrit
job.Setenv("tag", r.Form.Get("tag"))
job.Setenv("author", r.Form.Get("author"))
job.Setenv("comment", r.Form.Get("comment"))
job.Setenv("changes", r.Form.Get("changes"))
job.SetenvList("changes", r.Form["changes"])
job.SetenvSubEnv("config", &config)

job.Stdout.Add(stdoutBuffer)
Expand Down Expand Up @@ -571,6 +571,7 @@ func postImagesCreate(eng *engine.Engine, version version.Version, w http.Respon
}
job = eng.Job("import", r.Form.Get("fromSrc"), repo, tag)
job.Stdin.Add(r.Body)
job.SetenvList("changes", r.Form["changes"])
}

if version.GreaterThan("1.0") {
Expand Down
7 changes: 4 additions & 3 deletions builder/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"strings"

"github.com/docker/docker/api"
"github.com/docker/docker/builder/parser"
Expand All @@ -21,7 +22,7 @@ import (
"github.com/docker/docker/utils"
)

// whitelist of commands allowed for a commit
// whitelist of commands allowed for a commit/import
var validCommitCommands = map[string]bool{
"entrypoint": true,
"cmd": true,
Expand Down Expand Up @@ -162,15 +163,15 @@ func (b *BuilderJob) CmdBuildConfig(job *engine.Job) engine.Status {
}

var (
changes = job.Getenv("changes")
changes = job.GetenvList("changes")
newConfig runconfig.Config
)

if err := job.GetenvJson("config", &newConfig); err != nil {
return job.Error(err)
}

ast, err := parser.Parse(bytes.NewBufferString(changes))
ast, err := parser.Parse(bytes.NewBufferString(strings.Join(changes, "\n")))
if err != nil {
return job.Error(err)
}
Expand Down
13 changes: 7 additions & 6 deletions docs/man/docker-commit.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Using an existing container's name or ID you can create a new image.
Author (e.g., "John Hannibal Smith <[email protected]>")

**-c** , **--change**=[]
Apply a modification in Dockerfile format before committing the image.
Apply specified Dockerfile instructions while committing the image
Supported Dockerfile instructions: CMD, ENTRYPOINT, ENV, EXPOSE, ONBUILD, USER, VOLUME, WORKDIR

**--help**
Print usage statement
Expand All @@ -42,11 +43,11 @@ create a new image run docker ps to find the container's ID and then run:
# docker commit -m="Added Apache to Fedora base image" \
-a="A D Ministrator" 98bd7fc99854 fedora/fedora_httpd:20

## Modify configuration settings before committing the image
An existing container was created without the necessary environment variable
DEBUG set to "true". To create a new image based on the container with a
correct DEBUG environment variable, run docker ps to find the container's ID
and then run
## Apply specified Dockerfile instructions while committing the image
If an existing container was created without the DEBUG environment
variable set to "true", you can create a new image based on that
container by first getting the container's ID with docker ps and
then running:

# docker commit -c="ENV DEBUG true" 98bd7fc99854 debug-image

Expand Down
11 changes: 11 additions & 0 deletions docs/man/docker-import.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ docker-import - Create an empty filesystem image and import the contents of the

# SYNOPSIS
**docker import**
[**-c**|**--change**[= []**]]
[**--help**]
URL|- [REPOSITORY[:TAG]]

# OPTIONS
**-c**, **--change**=[]
Apply specified Dockerfile instructions while importing the image
Supported Dockerfile instructions: CMD, ENTRYPOINT, ENV, EXPOSE, ONBUILD, USER, VOLUME, WORKDIR

# DESCRIPTION
Create a new filesystem image from the contents of a tarball (`.tar`,
`.tar.gz`, `.tgz`, `.bzip`, `.tar.xz`, `.txz`) into it, then optionally tag it.
Expand Down Expand Up @@ -39,6 +45,11 @@ Import to docker via pipe and stdin:

# tar -c . | docker import - exampleimagedir

## Apply specified Dockerfile instructions while importing the image
This example sets the docker image ENV variable DEBUG to true by default.

# tar -c . | docker import -c="ENV DEBUG true" - exampleimagedir

# HISTORY
April 2014, Originally compiled by William Henry (whenry at redhat dot com)
based on docker.com source material and internal work.
Expand Down
22 changes: 19 additions & 3 deletions docs/sources/reference/commandline/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ you refer to it on the command line.
Create a new image from a container's changes

-a, --author="" Author (e.g., "John Hannibal Smith <[email protected]>")
-c, --change=[] Apply a modification in Dockerfile format before committing the image
-c, --change=[] Apply specified Dockerfile instructions while committing the image
-m, --message="" Commit message
-p, --pause=true Pause container during commit

Expand All @@ -709,7 +709,12 @@ while the image is committed. This reduces the likelihood of
encountering data corruption during the process of creating the commit.
If this behavior is undesired, set the 'p' option to false.

#### Commit an existing container
The `--change` option will apply `Dockerfile` instructions to the image
that is created.
Supported `Dockerfile` instructions: `CMD`, `ENTRYPOINT`, `ENV`, `EXPOSE`,
`ONBUILD`, `USER`, `VOLUME`, `WORKDIR`

#### Commit a container

$ sudo docker ps
ID IMAGE COMMAND CREATED STATUS PORTS
Expand All @@ -721,7 +726,7 @@ If this behavior is undesired, set the 'p' option to false.
REPOSITORY TAG ID CREATED VIRTUAL SIZE
SvenDowideit/testimage version3 f5283438590d 16 seconds ago 335.7 MB

#### Commit an existing container with new configurations
#### Commit a container with new configurations

$ sudo docker ps
ID IMAGE COMMAND CREATED STATUS PORTS
Expand Down Expand Up @@ -1151,11 +1156,18 @@ NOTE: Docker will warn you if any containers exist that are using these untagged
tarball (.tar, .tar.gz, .tgz, .bzip, .tar.xz, .txz) into it, then
optionally tag it.

-c, --change=[] Apply specified Dockerfile instructions while importing the image

URLs must start with `http` and point to a single file archive (.tar,
.tar.gz, .tgz, .bzip, .tar.xz, or .txz) containing a root filesystem. If
you would like to import from a local directory or archive, you can use
the `-` parameter to take the data from `STDIN`.

The `--change` option will apply `Dockerfile` instructions to the image
that is created.
Supported `Dockerfile` instructions: `CMD`, `ENTRYPOINT`, `ENV`, `EXPOSE`,
`ONBUILD`, `USER`, `VOLUME`, `WORKDIR`

#### Examples

**Import from a remote location:**
Expand All @@ -1174,6 +1186,10 @@ Import to docker via pipe and `STDIN`.

$ sudo tar -c . | sudo docker import - exampleimagedir

**Import from a local directory with new configurations:**

$ sudo tar -c . | sudo docker import --change "ENV DEBUG true" - exampleimagedir

Note the `sudo` in this example – you must preserve
the ownership of the files (especially root ownership) during the
archiving with tar. If you are not root (or the sudo command) when you
Expand Down
33 changes: 26 additions & 7 deletions graph/import.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package graph

import (
"bytes"
"encoding/json"
"net/http"
"net/url"

log "github.com/Sirupsen/logrus"
"github.com/docker/docker/engine"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/runconfig"
"github.com/docker/docker/utils"
)

Expand All @@ -15,12 +18,14 @@ func (s *TagStore) CmdImport(job *engine.Job) engine.Status {
return job.Errorf("Usage: %s SRC REPO [TAG]", job.Name)
}
var (
src = job.Args[0]
repo = job.Args[1]
tag string
sf = utils.NewStreamFormatter(job.GetenvBool("json"))
archive archive.ArchiveReader
resp *http.Response
src = job.Args[0]
repo = job.Args[1]
tag string
sf = utils.NewStreamFormatter(job.GetenvBool("json"))
archive archive.ArchiveReader
resp *http.Response
stdoutBuffer = bytes.NewBuffer(nil)
newConfig runconfig.Config
)
if len(job.Args) > 2 {
tag = job.Args[2]
Expand All @@ -47,7 +52,21 @@ func (s *TagStore) CmdImport(job *engine.Job) engine.Status {
defer progressReader.Close()
archive = progressReader
}
img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, nil)

buildConfigJob := job.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)
}

img, err := s.graph.Create(archive, "", "", "Imported from "+src, "", nil, &newConfig)
if err != nil {
return job.Error(err)
}
Expand Down
8 changes: 6 additions & 2 deletions integration-cli/docker_cli_commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,13 @@ func TestCommitWithHostBindMount(t *testing.T) {

func TestCommitChange(t *testing.T) {
defer deleteAllContainers()
cmd(t, "run", "--name", "test", "busybox", "true")

cmd := exec.Command(dockerBinary, "commit",
cmd := exec.Command(dockerBinary, "run", "--name", "test", "busybox", "true")
if _, err := runCommand(cmd); err != nil {
t.Fatal(err)
}

cmd = exec.Command(dockerBinary, "commit",
"--change", "EXPOSE 8080",
"--change", "ENV DEBUG true",
"test", "test-commit")
Expand Down

0 comments on commit 17abfc3

Please sign in to comment.