diff --git a/builder/docker/config.go b/builder/docker/config.go index 236a3060d..e24280432 100644 --- a/builder/docker/config.go +++ b/builder/docker/config.go @@ -32,6 +32,9 @@ type Config struct { RunCommand []string `mapstructure:"run_command"` Volumes map[string]string Privileged bool `mapstructure:"privileged"` + Author string + Changes []string + Message string // This is used to login to dockerhub to pull a private base container. For // pushing to dockerhub, see the docker post-processors diff --git a/builder/docker/driver.go b/builder/docker/driver.go index 850bd98dd..0eda0a604 100644 --- a/builder/docker/driver.go +++ b/builder/docker/driver.go @@ -11,7 +11,7 @@ import ( // a mock driver can be shimmed in. type Driver interface { // Commit the container to a tag - Commit(id string) (string, error) + Commit(id string, author string, changes []string, message string) (string, error) // Delete an image that is imported into Docker DeleteImage(id string) error diff --git a/builder/docker/driver_docker.go b/builder/docker/driver_docker.go index 172574e78..f603b1892 100644 --- a/builder/docker/driver_docker.go +++ b/builder/docker/driver_docker.go @@ -42,11 +42,24 @@ func (d *DockerDriver) DeleteImage(id string) error { return nil } -func (d *DockerDriver) Commit(id string) (string, error) { +func (d *DockerDriver) Commit(id string, author string, changes []string, message string) (string, error) { var stdout bytes.Buffer var stderr bytes.Buffer - cmd := exec.Command("docker", "commit", id) + args := []string{"commit"} + if author != "" { + args = append(args, "--author", author) + } + for _, change := range changes { + args = append(args, "--change", change) + } + if message != "" { + args = append(args, "--message", message) + } + args = append(args, id) + + log.Printf("Committing container with args: %v", args) + cmd := exec.Command("docker", args...) cmd.Stdout = &stdout cmd.Stderr = &stderr diff --git a/builder/docker/driver_mock.go b/builder/docker/driver_mock.go index b0170f85f..57a02b691 100644 --- a/builder/docker/driver_mock.go +++ b/builder/docker/driver_mock.go @@ -76,7 +76,7 @@ type MockDriver struct { VersionVersion string } -func (d *MockDriver) Commit(id string) (string, error) { +func (d *MockDriver) Commit(id string, author string, changes []string, message string) (string, error) { d.CommitCalled = true d.CommitContainerId = id return d.CommitImageId, d.CommitErr diff --git a/builder/docker/step_commit.go b/builder/docker/step_commit.go index bd1277182..2e0281bd4 100644 --- a/builder/docker/step_commit.go +++ b/builder/docker/step_commit.go @@ -14,10 +14,11 @@ type StepCommit struct { func (s *StepCommit) Run(state multistep.StateBag) multistep.StepAction { driver := state.Get("driver").(Driver) containerId := state.Get("container_id").(string) + config := state.Get("config").(*Config) ui := state.Get("ui").(packer.Ui) ui.Say("Committing the container") - imageId, err := driver.Commit(containerId) + imageId, err := driver.Commit(containerId, config.Author, config.Changes, config.Message) if err != nil { state.Put("error", err) ui.Error(err.Error()) diff --git a/website/source/docs/builders/docker.html.md b/website/source/docs/builders/docker.html.md index 7d2a4f21e..a42d83d58 100644 --- a/website/source/docs/builders/docker.html.md +++ b/website/source/docs/builders/docker.html.md @@ -86,6 +86,8 @@ You must specify (only) one of `commit`, `discard`, or `export_path`. ### Optional: +- `author` (string) - Set the author (e-mail) of a commit. + - `aws_access_key` (string) - The AWS access key used to communicate with AWS. [Learn how to set this.](/docs/builders/amazon.html#specifying-amazon-credentials) @@ -97,6 +99,10 @@ You must specify (only) one of `commit`, `discard`, or `export_path`. probably don't need it. This will also be read from the `AWS_SESSION_TOKEN` environmental variable. +- `changes` (array of strings) - Dockerfile instructions to add to the commit. + Example of instructions are `CMD`, `ENTRYPOINT`, `ENV`, and `EXPOSE`. Example: + `[ "USER ubuntu", "WORKDIR /app", "EXPOSE 8080" ]` + - `ecr_login` (boolean) - Defaults to false. If true, the builder will login in order to pull the image from [Amazon EC2 Container Registry (ECR)](https://aws.amazon.com/ecr/). @@ -116,6 +122,8 @@ You must specify (only) one of `commit`, `discard`, or `export_path`. - `login_server` (string) - The server address to login to. +- `message` (string) - Set a message for the commit. + - `privileged` (boolean) - If true, run the docker container with the `--privileged` flag. This defaults to false if not set.