Merge pull request #9672 from hashicorp/docker_builder_examples

Docker builder examples
This commit is contained in:
Megan Marsh 2020-07-30 15:31:54 -07:00 committed by GitHub
commit e0af769ffb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 190 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,32 @@ provisioners are defined, but it will effectively repackage an image.
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
source "docker" "example" {
image = "ubuntu"
export_path = "image.tar"
}
build {
sources = ["source.docker.example"]
}
```
</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 +84,23 @@ then be more easily tagged, pushed, etc.
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
source "docker" "example" {
image = "ubuntu"
commit = true
}
build {
sources = ["source.docker.example"]
}
```
</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 +112,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 +134,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 +231,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 +293,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 +341,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 +358,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 +367,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 +406,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 +423,34 @@ container.
} }
``` ```
</Tab>
<Tab heading="HCL2">
```hcl
source "docker" "windows" {
image = "ubuntu"
container_dir = "c:/app"
windows_container = true
commit = true
}
build {
sources = ["source.docker.example"]
}
```
</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 +472,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)