Merge pull request #7127 from ladar/master

Added change support to docker-import post processor.
This commit is contained in:
Megan Marsh 2018-12-20 14:44:05 -08:00 committed by GitHub
commit 141f3b29f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 7 deletions

View File

@ -20,7 +20,7 @@ type Driver interface {
Export(id string, dst io.Writer) error
// Import imports a container from a tar file
Import(path, repo string) (string, error)
Import(path string, changes []string, repo string) (string, error)
// IPAddress returns the address of the container that can be used
// for external access.

View File

@ -97,12 +97,23 @@ func (d *DockerDriver) Export(id string, dst io.Writer) error {
return nil
}
func (d *DockerDriver) Import(path string, repo string) (string, error) {
func (d *DockerDriver) Import(path string, changes []string, repo string) (string, error) {
var stdout, stderr bytes.Buffer
cmd := exec.Command("docker", "import", "-", repo)
args := []string{"import"}
for _, change := range changes {
args = append(args, "--change", change)
}
args = append(args, "-")
args = append(args, repo)
cmd := exec.Command("docker", args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
stdin, err := cmd.StdinPipe()
if err != nil {
return "", err
}
@ -114,6 +125,8 @@ func (d *DockerDriver) Import(path string, repo string) (string, error) {
}
defer file.Close()
log.Printf("Importing tarball with args: %v", args)
if err := cmd.Start(); err != nil {
return "", err
}

View File

@ -101,7 +101,7 @@ func (d *MockDriver) Export(id string, dst io.Writer) error {
return d.ExportError
}
func (d *MockDriver) Import(path, repo string) (string, error) {
func (d *MockDriver) Import(path string, changes []string, repo string) (string, error) {
d.ImportCalled = true
d.ImportPath = path
d.ImportRepo = repo

View File

@ -16,8 +16,9 @@ const BuilderId = "packer.post-processor.docker-import"
type Config struct {
common.PackerConfig `mapstructure:",squash"`
Repository string `mapstructure:"repository"`
Tag string `mapstructure:"tag"`
Repository string `mapstructure:"repository"`
Tag string `mapstructure:"tag"`
Changes []string `mapstructure:"changes"`
ctx interpolate.Context
}
@ -62,7 +63,7 @@ func (p *PostProcessor) PostProcess(ui packer.Ui, artifact packer.Artifact) (pac
ui.Message("Importing image: " + artifact.Id())
ui.Message("Repository: " + importRepo)
id, err := driver.Import(artifact.Files()[0], importRepo)
id, err := driver.Import(artifact.Files()[0], p.config.Changes, importRepo)
if err != nil {
return nil, false, err
}

View File

@ -25,11 +25,20 @@ registry.
The configuration for this post-processor only requires a `repository`, a `tag`
is optional.
### Required:
- `repository` (string) - The repository of the imported image.
- `tag` (string) - The tag for the imported image. By default this is not
set.
### Optional:
- `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" ]`
## Example
An example is shown below, showing only the post-processor configuration:
@ -48,3 +57,68 @@ into the local Docker process with a name of `hashicorp/packer:0.7`.
Following this, you can use the
[docker-push](/docs/post-processors/docker-push.html) post-processor to push it
to a registry, if you want.
## Changing Metadata
Below is an example using the changes argument of the post-processor. This
feature allows the tarball metadata to be changed when imported into the
Docker environment. It is derived from the `docker import --change` command
line [option to
Docker](https://docs.docker.com/engine/reference/commandline/import/).
Example uses of all of the options, assuming one is building an NGINX image
from ubuntu as an simple example:
``` json
{
"type": "docker-import",
"repository": "local/centos6",
"tag": "latest",
"changes": [
"USER www-data",
"WORKDIR /var/www",
"ENV HOSTNAME www.example.com",
"VOLUME /test1 /test2",
"EXPOSE 80 443",
"LABEL version=1.0",
"ONBUILD RUN date",
"CMD [\"nginx\", \"-g\", \"daemon off;\"]",
"ENTRYPOINT /var/www/start.sh"
]
}
```
Allowed metadata fields that can be changed are:
- CMD
- String, supports both array (escaped) and string form
- EX: `"CMD [\"nginx\", \"-g\", \"daemon off;\"]"`
- EX: `"CMD nginx -g daemon off;"`
- ENTRYPOINT
- String
- EX: `"ENTRYPOINT /var/www/start.sh"`
- ENV
- String, note there is no equal sign:
- EX: `"ENV HOSTNAME www.example.com"` not
`"ENV HOSTNAME=www.example.com"`
- EXPOSE
- String, space separated ports
- EX: `"EXPOSE 80 443"`
- LABEL
- String, space separated key=value pairs
- EX: `"LABEL version=1.0"`
- ONBUILD
- String
- EX: `"ONBUILD RUN date"`
- MAINTAINER
- String, deprecated in Docker version 1.13.0
- EX: `"MAINTAINER NAME"`
- USER
- String
- EX: `"USER USERNAME"`
- VOLUME
- String
- EX: `"VOLUME FROM TO"`
- WORKDIR
- String
- EX: `"WORKDIR PATH"`