2017-06-25 22:36:15 -04:00
|
|
|
---
|
2020-03-18 18:46:47 -04:00
|
|
|
description: >
|
|
|
|
The docker Packer builder builds Docker images using Docker. The builder
|
|
|
|
starts
|
|
|
|
|
|
|
|
a Docker container, runs provisioners within this container, then exports the
|
|
|
|
|
|
|
|
container for reuse or commits the image.
|
2015-07-22 22:31:00 -04:00
|
|
|
layout: docs
|
2020-03-18 18:46:47 -04:00
|
|
|
page_title: Docker - Builders
|
2020-04-02 19:39:47 -04:00
|
|
|
sidebar_title: Docker
|
2017-03-25 18:13:52 -04:00
|
|
|
---
|
2013-11-09 16:28:00 -05:00
|
|
|
|
|
|
|
# Docker Builder
|
|
|
|
|
|
|
|
Type: `docker`
|
|
|
|
|
2016-01-14 15:31:19 -05:00
|
|
|
The `docker` Packer builder builds [Docker](https://www.docker.io) images using
|
2015-07-22 22:31:00 -04:00
|
|
|
Docker. The builder starts a Docker container, runs provisioners within this
|
|
|
|
container, then exports the container for reuse or commits the image.
|
|
|
|
|
2020-03-18 18:46:47 -04:00
|
|
|
Packer builds Docker containers _without_ the use of
|
2017-06-05 00:32:59 -04:00
|
|
|
[Dockerfiles](https://docs.docker.com/engine/reference/builder/). By not using
|
|
|
|
`Dockerfiles`, Packer is able to provision containers with portable scripts or
|
2018-10-26 20:02:51 -04:00
|
|
|
configuration management systems that are not tied to Docker in any way. It
|
|
|
|
also has a simple mental model: you provision containers much the same way you
|
2015-07-22 22:31:00 -04:00
|
|
|
provision a normal virtualized or dedicated server. For more information, read
|
2016-12-01 04:48:19 -05:00
|
|
|
the section on [Dockerfiles](#dockerfiles).
|
2013-11-09 22:40:38 -05:00
|
|
|
|
2018-03-08 07:56:34 -05:00
|
|
|
The Docker builder must run on a machine that has Docker Engine installed.
|
2020-03-18 18:46:47 -04:00
|
|
|
Therefore the builder only works on machines that support Docker and _does not
|
|
|
|
support running on a Docker remote host_. You can learn about what [platforms
|
2018-10-26 20:02:51 -04:00
|
|
|
Docker supports and how to install onto
|
|
|
|
them](https://docs.docker.com/engine/installation/) in the Docker
|
|
|
|
documentation.
|
2018-03-14 12:05:52 -04:00
|
|
|
|
2020-03-26 20:28:32 -04:00
|
|
|
~> **Please note:** Packer does not yet have support for Windows containers.
|
2018-03-14 12:05:52 -04:00
|
|
|
|
2014-09-05 00:29:29 -04:00
|
|
|
## Basic Example: Export
|
2013-11-09 16:28:00 -05:00
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
Below is a fully functioning example. It doesn't do anything useful, since no
|
|
|
|
provisioners are defined, but it will effectively repackage an image.
|
2013-11-09 16:28:00 -05:00
|
|
|
|
2020-03-12 10:05:08 -04:00
|
|
|
```json
|
2013-11-09 16:28:00 -05:00
|
|
|
{
|
|
|
|
"type": "docker",
|
|
|
|
"image": "ubuntu",
|
|
|
|
"export_path": "image.tar"
|
|
|
|
}
|
2014-10-20 13:55:16 -04:00
|
|
|
```
|
2013-11-09 16:28:00 -05:00
|
|
|
|
2014-09-05 00:29:29 -04:00
|
|
|
## Basic Example: Commit
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
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.
|
2014-09-05 00:29:29 -04:00
|
|
|
|
2020-03-12 10:05:08 -04:00
|
|
|
```json
|
2014-09-05 00:29:29 -04:00
|
|
|
{
|
|
|
|
"type": "docker",
|
|
|
|
"image": "ubuntu",
|
|
|
|
"commit": true
|
|
|
|
}
|
2014-10-20 13:55:16 -04:00
|
|
|
```
|
2014-09-05 00:29:29 -04:00
|
|
|
|
2017-01-23 18:19:56 -05:00
|
|
|
## Basic Example: Changes to Metadata
|
|
|
|
|
2017-06-05 13:17:53 -04:00
|
|
|
Below is an example using the changes argument of the builder. This feature
|
|
|
|
allows the source images metadata to be changed when committed back into the
|
|
|
|
Docker environment. It is derived from the `docker commit --change` command
|
|
|
|
line [option to
|
|
|
|
Docker](https://docs.docker.com/engine/reference/commandline/commit/).
|
2017-01-23 18:19:56 -05:00
|
|
|
|
2017-06-05 13:17:53 -04:00
|
|
|
Example uses of all of the options, assuming one is building an NGINX image
|
|
|
|
from ubuntu as an simple example:
|
2017-01-23 18:19:56 -05:00
|
|
|
|
2020-03-12 10:05:08 -04:00
|
|
|
```json
|
2017-01-23 18:19:56 -05:00
|
|
|
{
|
2017-03-25 18:13:52 -04:00
|
|
|
"type": "docker",
|
|
|
|
"image": "ubuntu",
|
|
|
|
"commit": true,
|
|
|
|
"changes": [
|
|
|
|
"USER www-data",
|
|
|
|
"WORKDIR /var/www",
|
|
|
|
"ENV HOSTNAME www.example.com",
|
|
|
|
"VOLUME /test1 /test2",
|
|
|
|
"EXPOSE 80 443",
|
2017-05-09 18:45:26 -04:00
|
|
|
"LABEL version=1.0",
|
2017-05-09 18:47:04 -04:00
|
|
|
"ONBUILD RUN date",
|
2017-03-25 18:13:52 -04:00
|
|
|
"CMD [\"nginx\", \"-g\", \"daemon off;\"]",
|
|
|
|
"ENTRYPOINT /var/www/start.sh"
|
|
|
|
]
|
2017-01-23 18:19:56 -05:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Allowed metadata fields that can be changed are:
|
|
|
|
|
2020-03-18 18:46:47 -04:00
|
|
|
- CMD
|
|
|
|
- String, supports both array (escaped) and string form
|
|
|
|
- EX: `"CMD [\"nginx\", \"-g\", \"daemon off;\"]"` corresponds to Docker exec form
|
|
|
|
- EX: `"CMD nginx -g daemon off;"` corresponds to Docker shell form, invokes a command shell first
|
|
|
|
- ENTRYPOINT
|
|
|
|
- String, supports both array (escaped) and string form
|
|
|
|
- EX: `"ENTRYPOINT [\"/bin/sh\", \"-c\", \"/var/www/start.sh\"]"` corresponds to Docker exec form
|
|
|
|
- EX: `"ENTRYPOINT /var/www/start.sh"` corresponds to Docker shell form, invokes a command shell first
|
|
|
|
- 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"`
|
2017-01-23 18:19:56 -05:00
|
|
|
|
2013-11-09 16:28:00 -05:00
|
|
|
## Configuration Reference
|
|
|
|
|
2013-12-25 05:17:37 -05:00
|
|
|
Configuration options are organized below into two categories: required and
|
|
|
|
optional. Within each category, the available options are alphabetized and
|
|
|
|
described.
|
2013-11-09 20:22:39 -05:00
|
|
|
|
2020-03-18 18:46:47 -04:00
|
|
|
The Docker builder uses a special Docker communicator _and will not use_ the
|
2020-03-31 17:40:07 -04:00
|
|
|
standard [communicators](/docs/templates/communicator).
|
2015-06-23 17:44:57 -04:00
|
|
|
|
2014-05-04 13:47:40 -04:00
|
|
|
### Required:
|
2013-11-09 16:28:00 -05:00
|
|
|
|
2015-08-18 19:47:58 -04:00
|
|
|
You must specify (only) one of `commit`, `discard`, or `export_path`.
|
|
|
|
|
2020-03-23 20:02:12 -04:00
|
|
|
@include 'builder/docker/Config-required.mdx'
|
2013-11-09 16:28:00 -05:00
|
|
|
|
2014-05-04 13:47:40 -04:00
|
|
|
### Optional:
|
2013-11-09 20:22:39 -05:00
|
|
|
|
2020-03-23 20:02:12 -04:00
|
|
|
@include 'builder/docker/AwsAccessConfig-not-required.mdx'
|
2020-03-24 19:48:37 -04:00
|
|
|
|
2020-03-23 20:02:12 -04:00
|
|
|
@include 'builder/docker/Config-not-required.mdx'
|
2017-07-17 15:38:03 -04:00
|
|
|
|
2014-09-05 00:29:29 -04:00
|
|
|
## Using the Artifact: Export
|
2013-12-24 20:40:13 -05:00
|
|
|
|
2013-12-25 05:17:37 -05:00
|
|
|
Once the tar artifact has been generated, you will likely want to import, tag,
|
2014-01-19 23:42:42 -05:00
|
|
|
and push it to a container repository. Packer can do this for you automatically
|
2020-03-31 17:40:07 -04:00
|
|
|
with the [docker-import](/docs/post-processors/docker-import) and
|
|
|
|
[docker-push](/docs/post-processors/docker-push) post-processors.
|
2013-12-24 20:40:13 -05:00
|
|
|
|
2014-09-05 00:29:29 -04:00
|
|
|
**Note:** This section is covering how to use an artifact that has been
|
2020-03-18 18:46:47 -04:00
|
|
|
_exported_. More specifically, if you set `export_path` in your configuration.
|
2014-09-05 00:29:29 -04:00
|
|
|
If you set `commit`, see the next section.
|
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
The example below shows a full configuration that would import and push the
|
2018-10-26 20:02:51 -04:00
|
|
|
created image. This is accomplished using a sequence definition (a collection
|
|
|
|
of post-processors that are treated as as single pipeline, see
|
2020-03-31 17:40:07 -04:00
|
|
|
[Post-Processors](/docs/templates/post-processors) for more information):
|
2014-01-19 23:42:42 -05:00
|
|
|
|
2020-03-12 10:05:08 -04:00
|
|
|
```json
|
2014-01-19 23:42:42 -05:00
|
|
|
{
|
2014-10-20 13:55:16 -04:00
|
|
|
"post-processors": [
|
2015-07-22 22:31:00 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "docker-import",
|
2017-06-25 22:34:23 -04:00
|
|
|
"repository": "hashicorp/packer",
|
2015-07-22 22:31:00 -04:00
|
|
|
"tag": "0.7"
|
|
|
|
},
|
|
|
|
"docker-push"
|
|
|
|
]
|
|
|
|
]
|
2014-01-19 23:42:42 -05:00
|
|
|
}
|
2014-10-20 13:55:16 -04:00
|
|
|
```
|
2014-01-19 23:42:42 -05:00
|
|
|
|
2015-06-30 08:16:31 -04:00
|
|
|
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
|
2018-10-26 20:02:51 -04:00
|
|
|
docker image is then passed on to the `docker-push` post-processor which
|
|
|
|
handles pushing the image to a container repository.
|
2015-06-30 08:16:31 -04:00
|
|
|
|
2015-07-22 22:31:00 -04:00
|
|
|
If you want to do this manually, however, perhaps from a script, you can import
|
|
|
|
the image using the process below:
|
2014-01-19 23:42:42 -05:00
|
|
|
|
2020-03-18 18:46:47 -04:00
|
|
|
```shell
|
2014-10-20 13:55:16 -04:00
|
|
|
$ docker import - registry.mydomain.com/mycontainer:latest < artifact.tar
|
|
|
|
```
|
2013-12-24 20:40:13 -05:00
|
|
|
|
2013-12-25 05:17:37 -05:00
|
|
|
You can then add additional tags and push the image as usual with `docker tag`
|
|
|
|
and `docker push`, respectively.
|
2013-12-24 20:40:13 -05:00
|
|
|
|
2014-09-05 00:29:29 -04:00
|
|
|
## Using the Artifact: Committed
|
|
|
|
|
2015-06-30 08:16:31 -04:00
|
|
|
If you committed your container to an image, you probably want to tag, save,
|
|
|
|
push, etc. Packer can do this automatically for you. An example is shown below
|
2018-10-26 20:02:51 -04:00
|
|
|
which tags and pushes an image. This is accomplished using a sequence
|
|
|
|
definition (a collection of post-processors that are treated as as single
|
2020-03-31 17:40:07 -04:00
|
|
|
pipeline, see [Post-Processors](/docs/templates/post-processors) for more
|
2018-10-26 20:02:51 -04:00
|
|
|
information):
|
2014-09-05 00:29:29 -04:00
|
|
|
|
2020-03-12 10:05:08 -04:00
|
|
|
```json
|
2014-09-05 00:29:29 -04:00
|
|
|
{
|
2014-10-20 13:55:16 -04:00
|
|
|
"post-processors": [
|
2015-07-22 22:31:00 -04:00
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "docker-tag",
|
2017-06-25 22:34:23 -04:00
|
|
|
"repository": "hashicorp/packer",
|
2015-07-22 22:31:00 -04:00
|
|
|
"tag": "0.7"
|
|
|
|
},
|
|
|
|
"docker-push"
|
|
|
|
]
|
|
|
|
]
|
2014-09-05 00:29:29 -04:00
|
|
|
}
|
2014-10-20 13:55:16 -04:00
|
|
|
```
|
2014-09-05 00:29:29 -04:00
|
|
|
|
2015-06-30 08:16:31 -04:00
|
|
|
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
|
2018-10-26 20:02:51 -04:00
|
|
|
which tags the committed image with the supplied repository and tag
|
|
|
|
information. Once tagged, the resulting artifact is then passed on to the
|
|
|
|
`docker-push` post-processor which handles pushing the image to a container
|
|
|
|
repository.
|
2015-06-30 08:16:31 -04:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
2020-03-12 10:05:08 -04:00
|
|
|
```json
|
2015-06-30 08:16:31 -04:00
|
|
|
{
|
2015-07-22 22:31:00 -04:00
|
|
|
"post-processors": [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "docker-tag",
|
2017-11-14 17:40:06 -05:00
|
|
|
"repository": "hashicorp/packer1",
|
2015-07-22 22:31:00 -04:00
|
|
|
"tag": "0.7"
|
|
|
|
},
|
|
|
|
"docker-push"
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "docker-tag",
|
2017-11-14 17:40:06 -05:00
|
|
|
"repository": "hashicorp/packer2",
|
2015-07-22 22:31:00 -04:00
|
|
|
"tag": "0.7"
|
|
|
|
},
|
|
|
|
"docker-push"
|
|
|
|
]
|
|
|
|
]
|
2015-06-30 08:16:31 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-01-13 11:24:32 -05:00
|
|
|
<span id="amazon-ec2-container-registry"></span>
|
|
|
|
|
2019-03-29 16:39:02 -04:00
|
|
|
## Docker For Windows
|
|
|
|
|
|
|
|
You should be able to run docker builds against both linux and Windows
|
|
|
|
containers. Windows containers use a different communicator than linux
|
|
|
|
containers, because Windows containers cannot use `docker cp`.
|
|
|
|
|
|
|
|
If you are building a Windows container, you must set the template option
|
2020-03-18 18:46:47 -04:00
|
|
|
`"windows_container": true`. Please note that docker cannot export Windows
|
2019-03-29 16:39:02 -04:00
|
|
|
containers, so you must either commit or discard them.
|
|
|
|
|
|
|
|
The following is a fully functional template for building a Windows
|
|
|
|
container.
|
|
|
|
|
2020-03-12 10:05:08 -04:00
|
|
|
```json
|
2019-03-29 16:39:02 -04:00
|
|
|
{
|
2020-03-18 18:46:47 -04:00
|
|
|
"builders": [
|
2019-03-29 16:39:02 -04:00
|
|
|
{
|
|
|
|
"type": "docker",
|
|
|
|
"image": "microsoft/windowsservercore:1709",
|
|
|
|
"container_dir": "c:/app",
|
|
|
|
"windows_container": true,
|
|
|
|
"commit": true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-01-13 11:24:32 -05:00
|
|
|
## Amazon EC2 Container Registry
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
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:
|
2016-01-13 11:24:32 -05:00
|
|
|
|
2020-03-12 10:05:08 -04:00
|
|
|
```json
|
2016-01-13 11:24:32 -05:00
|
|
|
{
|
|
|
|
"post-processors": [
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"type": "docker-tag",
|
|
|
|
"repository": "12345.dkr.ecr.us-east-1.amazonaws.com/packer",
|
|
|
|
"tag": "0.7"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"type": "docker-push",
|
2016-09-03 10:45:52 -04:00
|
|
|
"ecr_login": true,
|
|
|
|
"aws_access_key": "YOUR KEY HERE",
|
|
|
|
"aws_secret_key": "YOUR SECRET KEY HERE",
|
2016-01-13 11:24:32 -05:00
|
|
|
"login_server": "https://12345.dkr.ecr.us-east-1.amazonaws.com/"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2018-10-26 20:02:51 -04:00
|
|
|
[Learn how to set Amazon AWS
|
2020-03-31 17:40:07 -04:00
|
|
|
credentials.](/docs/builders/amazon#specifying-amazon-credentials)
|
2016-01-13 11:24:32 -05:00
|
|
|
|
2013-11-09 16:28:00 -05:00
|
|
|
## Dockerfiles
|
|
|
|
|
2020-03-18 18:46:47 -04:00
|
|
|
This builder allows you to build Docker images _without_ Dockerfiles.
|
2013-11-09 16:59:34 -05:00
|
|
|
|
2018-03-13 22:19:20 -04:00
|
|
|
With this builder, you can repeatedly create Docker images without the use of a
|
2015-07-22 22:31:00 -04:00
|
|
|
Dockerfile. You don't need to know the syntax or semantics of Dockerfiles.
|
2013-11-09 16:59:34 -05:00
|
|
|
Instead, you can just provide shell scripts, Chef recipes, Puppet manifests,
|
2013-11-09 22:40:38 -05:00
|
|
|
etc. to provision your Docker container just like you would a regular
|
|
|
|
virtualized or dedicated machine.
|
|
|
|
|
2020-02-13 09:39:45 -05:00
|
|
|
While Docker has many features, Packer views Docker simply as a container
|
2018-10-26 20:02:51 -04:00
|
|
|
runner. To that end, Packer is able to repeatedly build these containers using
|
|
|
|
portable provisioning scripts.
|
2013-11-09 22:40:38 -05:00
|
|
|
|
2017-02-21 17:50:06 -05:00
|
|
|
## Overriding the host directory
|
|
|
|
|
2019-01-25 15:32:44 -05:00
|
|
|
By default, Packer creates a temporary folder under your home directory, and
|
|
|
|
uses that to stage files for uploading into the container. If you would like to
|
2019-01-18 10:42:04 -05:00
|
|
|
change the path to this temporary folder, you can set the `PACKER_TMP_DIR`.
|
2018-12-12 13:17:39 -05:00
|
|
|
This can be useful, for example, if you have your home directory permissions
|
|
|
|
set up to disallow access from the docker daemon.
|