From 6927a9b10d82fc436509ead6603af599f3ecf857 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 29 Jul 2020 15:23:45 -0700 Subject: [PATCH 1/3] add hcl2 examples for docker builder docs --- website/pages/docs/builders/docker.mdx | 184 ++++++++++++++++++++++++- 1 file changed, 178 insertions(+), 6 deletions(-) diff --git a/website/pages/docs/builders/docker.mdx b/website/pages/docs/builders/docker.mdx index bc0cae85c..67f0fdb4d 100644 --- a/website/pages/docs/builders/docker.mdx +++ b/website/pages/docs/builders/docker.mdx @@ -39,6 +39,9 @@ documentation. Below is a fully functioning example. It doesn't do anything useful, since no provisioners are defined, but it will effectively repackage an image. + + + ```json { "type": "docker", @@ -47,12 +50,28 @@ provisioners are defined, but it will effectively repackage an image. } ``` + + + +```hcl +source "docker" "example" { + image = "ubuntu" + export_path = "image.tar" +} +``` + + + + ## Basic Example: Commit Below is another example, the same as above but instead of exporting the running container, this one commits the container to an image. The image can then be more easily tagged, pushed, etc. + + + ```json { "type": "docker", @@ -61,6 +80,19 @@ then be more easily tagged, pushed, etc. } ``` + + + +```hcl +source "docker" "example" { + image = "ubuntu" + commit = true +} +``` + + + + ## Basic Example: Changes to Metadata Below is an example using the changes argument of the builder. This feature @@ -72,6 +104,9 @@ Docker](https://docs.docker.com/engine/reference/commandline/commit/). Example uses of all of the options, assuming one is building an NGINX image from ubuntu as an simple example: + + + ```json { "type": "docker", @@ -91,6 +126,30 @@ from ubuntu as an simple example: } ``` + + + +```hcl +source "docker" "example" { + image = "ubuntu" + commit = true + 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 @@ -164,21 +223,43 @@ created image. This is accomplished using a sequence definition (a collection of post-processors that are treated as as single pipeline, see [Post-Processors](/docs/templates/post-processors) for more information): + + + ```json { "post-processors": [ [ { "type": "docker-import", - "repository": "hashicorp/packer", + "repository": "myrepo/myimage", "tag": "0.7" }, - "docker-push" + { + "type": "docker-push" + } ] ] } ``` + + + +```hcl + post-processors { + post-processor "docker-import" { + repository = "myrepo/myimage" + tag = "0.7" + } + post-processor "docker-push" {} + } +} +``` + + + + In the above example, the result of each builder is passed through the defined sequence of post-processors starting first with the `docker-import` post-processor which will import the artifact as a docker image. The resulting @@ -204,21 +285,43 @@ definition (a collection of post-processors that are treated as as single pipeline, see [Post-Processors](/docs/templates/post-processors) for more information): + + + ```json { "post-processors": [ [ { "type": "docker-tag", - "repository": "hashicorp/packer", + "repository": "myrepo/myimage", "tag": "0.7" }, - "docker-push" + { + "type": "docker-push" + } ] ] } ``` + + + +```hcl + post-processors { + post-processor "docker-tag" { + repository = "myrepo/myimage" + tag = "0.7" + } + post-processor "docker-push" {} + } +} +``` + + + + In the above example, the result of each builder is passed through the defined sequence of post-processors starting first with the `docker-tag` post-processor which tags the committed image with the supplied repository and tag @@ -230,13 +333,16 @@ Going a step further, if you wanted to tag and push an image to multiple container repositories, this could be accomplished by defining two, nearly-identical sequence definitions, as demonstrated by the example below: + + + ```json { "post-processors": [ [ { "type": "docker-tag", - "repository": "hashicorp/packer1", + "repository": "myrepo/myimage1", "tag": "0.7" }, "docker-push" @@ -244,7 +350,7 @@ nearly-identical sequence definitions, as demonstrated by the example below: [ { "type": "docker-tag", - "repository": "hashicorp/packer2", + "repository": "myrepo/myimage2", "tag": "0.7" }, "docker-push" @@ -253,6 +359,30 @@ nearly-identical sequence definitions, as demonstrated by the example below: } ``` + + + +```hcl + post-processors { + post-processor "docker-tag" { + repository = "myrepo/myimage1" + tag = "0.7" + } + post-processor "docker-push" {} + } + post-processors { + post-processor "docker-tag" { + repository = "myrepo/myimage2" + tag = "0.7" + } + post-processor "docker-push" {} + } +} +``` + + + + ## Docker For Windows @@ -268,6 +398,9 @@ containers, so you must either commit or discard them. The following is a fully functional template for building a Windows container. + + + ```json { "builders": [ @@ -282,12 +415,30 @@ container. } ``` + + + +```hcl +source "docker" "windows" { + image = "ubuntu" + container_dir = "c:/app" + windows_container = true + commit = true +} +``` + + + + ## Amazon EC2 Container Registry Packer can tag and push images for use in [Amazon EC2 Container Registry](https://aws.amazon.com/ecr/). The post processors work as described above and example configuration properties are shown below: + + + ```json { "post-processors": [ @@ -309,6 +460,27 @@ above and example configuration properties are shown below: } ``` + + + +```hcl +post-processors { + post-processor "docker-tag" { + repository = "12345.dkr.ecr.us-east-1.amazonaws.com/packer" + tag = "0.7" + } + post-processor "docker-push" { + ecr_login = true + aws_access_key = "YOUR KEY HERE" + aws_secret_key = "YOUR SECRET KEY HERE" + login_server = "https://12345.dkr.ecr.us-east-1.amazonaws.com/" + } +} +``` + + + + [Learn how to set Amazon AWS credentials.](/docs/builders/amazon#specifying-amazon-credentials) From 6248da58c56059b8db57f50d345de1fc8186ef18 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 30 Jul 2020 15:05:21 -0700 Subject: [PATCH 2/3] Update website/pages/docs/builders/docker.mdx Co-authored-by: Wilken Rivera --- website/pages/docs/builders/docker.mdx | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/website/pages/docs/builders/docker.mdx b/website/pages/docs/builders/docker.mdx index 67f0fdb4d..afbe237b2 100644 --- a/website/pages/docs/builders/docker.mdx +++ b/website/pages/docs/builders/docker.mdx @@ -58,6 +58,10 @@ source "docker" "example" { image = "ubuntu" export_path = "image.tar" } + +build { + sources = ["source.docker.example"] + } ``` From 37f21dde5ccc1291ddd4bc9e437426d64d4bcb73 Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Thu, 30 Jul 2020 15:07:15 -0700 Subject: [PATCH 3/3] add build sources to hcl examples --- website/pages/docs/builders/docker.mdx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/website/pages/docs/builders/docker.mdx b/website/pages/docs/builders/docker.mdx index afbe237b2..2c33dff3b 100644 --- a/website/pages/docs/builders/docker.mdx +++ b/website/pages/docs/builders/docker.mdx @@ -61,7 +61,7 @@ source "docker" "example" { build { sources = ["source.docker.example"] - } +} ``` @@ -92,6 +92,10 @@ source "docker" "example" { image = "ubuntu" commit = true } + +build { + sources = ["source.docker.example"] +} ``` @@ -429,6 +433,10 @@ source "docker" "windows" { windows_container = true commit = true } + +build { + sources = ["source.docker.example"] +} ```