add hcl2 examples for docker builder docs

This commit is contained in:
Megan Marsh 2020-07-29 15:23:45 -07:00
parent 6940cc6d38
commit 6927a9b10d
1 changed files with 178 additions and 6 deletions

View File

@ -39,6 +39,9 @@ documentation.
Below is a fully functioning example. It doesn't do anything useful, since no Below is a fully functioning example. It doesn't do anything useful, since no
provisioners are defined, but it will effectively repackage an image. provisioners are defined, but it will effectively repackage an image.
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "docker", "type": "docker",
@ -47,12 +50,28 @@ provisioners are defined, but it will effectively repackage an image.
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
source "docker" "example" {
image = "ubuntu"
export_path = "image.tar"
}
```
</Tab>
</Tabs>
## Basic Example: Commit ## Basic Example: Commit
Below is another example, the same as above but instead of exporting the 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 running container, this one commits the container to an image. The image can
then be more easily tagged, pushed, etc. then be more easily tagged, pushed, etc.
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "docker", "type": "docker",
@ -61,6 +80,19 @@ then be more easily tagged, pushed, etc.
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
source "docker" "example" {
image = "ubuntu"
commit = true
}
```
</Tab>
</Tabs>
## Basic Example: Changes to Metadata ## Basic Example: Changes to Metadata
Below is an example using the changes argument of the builder. This feature 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 Example uses of all of the options, assuming one is building an NGINX image
from ubuntu as an simple example: from ubuntu as an simple example:
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"type": "docker", "type": "docker",
@ -91,6 +126,30 @@ from ubuntu as an simple example:
} }
``` ```
</Tab>
<Tab heading="HCL2">
```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"
]
}
```
</Tab>
</Tabs>
Allowed metadata fields that can be changed are: Allowed metadata fields that can be changed are:
- CMD - 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 of post-processors that are treated as as single pipeline, see
[Post-Processors](/docs/templates/post-processors) for more information): [Post-Processors](/docs/templates/post-processors) for more information):
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"post-processors": [ "post-processors": [
[ [
{ {
"type": "docker-import", "type": "docker-import",
"repository": "hashicorp/packer", "repository": "myrepo/myimage",
"tag": "0.7" "tag": "0.7"
}, },
"docker-push" {
"type": "docker-push"
}
] ]
] ]
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processors {
post-processor "docker-import" {
repository = "myrepo/myimage"
tag = "0.7"
}
post-processor "docker-push" {}
}
}
```
</Tab>
</Tabs>
In the above example, the result of each builder is passed through the defined In the above example, the result of each builder is passed through the defined
sequence of post-processors starting first with the `docker-import` sequence of post-processors starting first with the `docker-import`
post-processor which will import the artifact as a docker image. The resulting 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 pipeline, see [Post-Processors](/docs/templates/post-processors) for more
information): information):
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"post-processors": [ "post-processors": [
[ [
{ {
"type": "docker-tag", "type": "docker-tag",
"repository": "hashicorp/packer", "repository": "myrepo/myimage",
"tag": "0.7" "tag": "0.7"
}, },
"docker-push" {
"type": "docker-push"
}
] ]
] ]
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
post-processors {
post-processor "docker-tag" {
repository = "myrepo/myimage"
tag = "0.7"
}
post-processor "docker-push" {}
}
}
```
</Tab>
</Tabs>
In the above example, the result of each builder is passed through the defined 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 sequence of post-processors starting first with the `docker-tag` post-processor
which tags the committed image with the supplied repository and tag 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, container repositories, this could be accomplished by defining two,
nearly-identical sequence definitions, as demonstrated by the example below: nearly-identical sequence definitions, as demonstrated by the example below:
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"post-processors": [ "post-processors": [
[ [
{ {
"type": "docker-tag", "type": "docker-tag",
"repository": "hashicorp/packer1", "repository": "myrepo/myimage1",
"tag": "0.7" "tag": "0.7"
}, },
"docker-push" "docker-push"
@ -244,7 +350,7 @@ nearly-identical sequence definitions, as demonstrated by the example below:
[ [
{ {
"type": "docker-tag", "type": "docker-tag",
"repository": "hashicorp/packer2", "repository": "myrepo/myimage2",
"tag": "0.7" "tag": "0.7"
}, },
"docker-push" "docker-push"
@ -253,6 +359,30 @@ nearly-identical sequence definitions, as demonstrated by the example below:
} }
``` ```
</Tab>
<Tab heading="HCL2">
```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" {}
}
}
```
</Tab>
</Tabs>
<span id="amazon-ec2-container-registry"></span> <span id="amazon-ec2-container-registry"></span>
## Docker For Windows ## 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 The following is a fully functional template for building a Windows
container. container.
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"builders": [ "builders": [
@ -282,12 +415,30 @@ container.
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
source "docker" "windows" {
image = "ubuntu"
container_dir = "c:/app"
windows_container = true
commit = true
}
```
</Tab>
</Tabs>
## Amazon EC2 Container Registry ## Amazon EC2 Container Registry
Packer can tag and push images for use in [Amazon EC2 Container Packer can tag and push images for use in [Amazon EC2 Container
Registry](https://aws.amazon.com/ecr/). The post processors work as described Registry](https://aws.amazon.com/ecr/). The post processors work as described
above and example configuration properties are shown below: above and example configuration properties are shown below:
<Tabs>
<Tab heading="JSON">
```json ```json
{ {
"post-processors": [ "post-processors": [
@ -309,6 +460,27 @@ above and example configuration properties are shown below:
} }
``` ```
</Tab>
<Tab heading="HCL2">
```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/"
}
}
```
</Tab>
</Tabs>
[Learn how to set Amazon AWS [Learn how to set Amazon AWS
credentials.](/docs/builders/amazon#specifying-amazon-credentials) credentials.](/docs/builders/amazon#specifying-amazon-credentials)