github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/website/source/docs/builders/docker.html.md (about) 1 --- 2 description: | 3 The `docker` Packer builder builds Docker images using Docker. The builder 4 starts a Docker container, runs provisioners within this container, then exports 5 the container for reuse or commits the image. 6 layout: docs 7 page_title: Docker Builder 8 ... 9 10 # Docker Builder 11 12 Type: `docker` 13 14 The `docker` Packer builder builds [Docker](https://www.docker.io) images using 15 Docker. The builder starts a Docker container, runs provisioners within this 16 container, then exports the container for reuse or commits the image. 17 18 Packer builds Docker containers *without* the use of 19 [Dockerfiles](https://docs.docker.com/reference/builder/). By not using 20 Dockerfiles, Packer is able to provision containers with portable scripts or 21 configuration management systems that are not tied to Docker in any way. It also 22 has a simpler mental model: you provision containers much the same way you 23 provision a normal virtualized or dedicated server. For more information, read 24 the section on [Dockerfiles](#toc_8). 25 26 The Docker builder must run on a machine that has Docker installed. Therefore 27 the builder only works on machines that support Docker (modern Linux machines). 28 If you want to use Packer to build Docker containers on another platform, use 29 [Vagrant](https://www.vagrantup.com) to start a Linux environment, then run 30 Packer within that environment. 31 32 ## Basic Example: Export 33 34 Below is a fully functioning example. It doesn't do anything useful, since no 35 provisioners are defined, but it will effectively repackage an image. 36 37 ``` {.javascript} 38 { 39 "type": "docker", 40 "image": "ubuntu", 41 "export_path": "image.tar" 42 } 43 ``` 44 45 ## Basic Example: Commit 46 47 Below is another example, the same as above but instead of exporting the running 48 container, this one commits the container to an image. The image can then be 49 more easily tagged, pushed, etc. 50 51 ``` {.javascript} 52 { 53 "type": "docker", 54 "image": "ubuntu", 55 "commit": true 56 } 57 ``` 58 59 ## Configuration Reference 60 61 Configuration options are organized below into two categories: required and 62 optional. Within each category, the available options are alphabetized and 63 described. 64 65 In addition to the options listed here, a 66 [communicator](/docs/templates/communicator.html) can be configured for this 67 builder. 68 69 ### Required: 70 71 You must specify (only) one of `commit`, `discard`, or `export_path`. 72 73 - `commit` (boolean) - If true, the container will be committed to an image 74 rather than exported. 75 76 - `discard` (boolean) - Throw away the container when the build is complete. 77 This is useful for the [artifice 78 post-processor](https://www.packer.io/docs/post-processors/artifice.html). 79 80 - `export_path` (string) - The path where the final container will be exported 81 as a tar file. 82 83 - `image` (string) - The base image for the Docker container that will 84 be started. This image will be pulled from the Docker registry if it doesn't 85 already exist. 86 87 ### Optional: 88 89 - `login` (boolean) - Defaults to false. If true, the builder will login in 90 order to pull the image. The builder only logs in for the duration of 91 the pull. It always logs out afterwards. 92 93 - `login_email` (string) - The email to use to authenticate to login. 94 95 - `login_username` (string) - The username to use to authenticate to login. 96 97 - `login_password` (string) - The password to use to authenticate to login. 98 99 - `login_server` (string) - The server address to login to. 100 101 - `pull` (boolean) - If true, the configured image will be pulled using 102 `docker pull` prior to use. Otherwise, it is assumed the image already 103 exists and can be used. This defaults to true if not set. 104 105 - `run_command` (array of strings) - An array of arguments to pass to 106 `docker run` in order to run the container. By default this is set to 107 `["-d", "-i", "-t", "{{.Image}}", "/bin/bash"]`. As you can see, you have a 108 couple template variables to customize, as well. 109 110 - `volumes` (map of strings to strings) - A mapping of additional volumes to 111 mount into this container. The key of the object is the host path, the value 112 is the container path. 113 114 ## Using the Artifact: Export 115 116 Once the tar artifact has been generated, you will likely want to import, tag, 117 and push it to a container repository. Packer can do this for you automatically 118 with the [docker-import](/docs/post-processors/docker-import.html) and 119 [docker-push](/docs/post-processors/docker-push.html) post-processors. 120 121 **Note:** This section is covering how to use an artifact that has been 122 *exported*. More specifically, if you set `export_path` in your configuration. 123 If you set `commit`, see the next section. 124 125 The example below shows a full configuration that would import and push the 126 created image. This is accomplished using a sequence definition (a collection of 127 post-processors that are treated as as single pipeline, see 128 [Post-Processors](/docs/templates/post-processors.html) for more information): 129 130 ``` {.javascript} 131 { 132 "post-processors": [ 133 [ 134 { 135 "type": "docker-import", 136 "repository": "mitchellh/packer", 137 "tag": "0.7" 138 }, 139 "docker-push" 140 ] 141 ] 142 } 143 ``` 144 145 In the above example, the result of each builder is passed through the defined 146 sequence of post-processors starting first with the `docker-import` 147 post-processor which will import the artifact as a docker image. The resulting 148 docker image is then passed on to the `docker-push` post-processor which handles 149 pushing the image to a container repository. 150 151 If you want to do this manually, however, perhaps from a script, you can import 152 the image using the process below: 153 154 ``` {.text} 155 $ docker import - registry.mydomain.com/mycontainer:latest < artifact.tar 156 ``` 157 158 You can then add additional tags and push the image as usual with `docker tag` 159 and `docker push`, respectively. 160 161 ## Using the Artifact: Committed 162 163 If you committed your container to an image, you probably want to tag, save, 164 push, etc. Packer can do this automatically for you. An example is shown below 165 which tags and pushes an image. This is accomplished using a sequence definition 166 (a collection of post-processors that are treated as as single pipeline, see 167 [Post-Processors](/docs/templates/post-processors.html) for more information): 168 169 ``` {.javascript} 170 { 171 "post-processors": [ 172 [ 173 { 174 "type": "docker-tag", 175 "repository": "mitchellh/packer", 176 "tag": "0.7" 177 }, 178 "docker-push" 179 ] 180 ] 181 } 182 ``` 183 184 In the above example, the result of each builder is passed through the defined 185 sequence of post-processors starting first with the `docker-tag` post-processor 186 which tags the committed image with the supplied repository and tag information. 187 Once tagged, the resulting artifact is then passed on to the `docker-push` 188 post-processor which handles pushing the image to a container repository. 189 190 Going a step further, if you wanted to tag and push an image to multiple 191 container repositories, this could be accomplished by defining two, 192 nearly-identical sequence definitions, as demonstrated by the example below: 193 194 ``` {.javascript} 195 { 196 "post-processors": [ 197 [ 198 { 199 "type": "docker-tag", 200 "repository": "mitchellh/packer", 201 "tag": "0.7" 202 }, 203 "docker-push" 204 ], 205 [ 206 { 207 "type": "docker-tag", 208 "repository": "hashicorp/packer", 209 "tag": "0.7" 210 }, 211 "docker-push" 212 ] 213 ] 214 } 215 ``` 216 217 <span id="amazon-ec2-container-registry"></span> 218 219 ## Amazon EC2 Container Registry 220 221 Packer can tag and push images for use in 222 [Amazon EC2 Container Registry](https://aws.amazon.com/ecr/). The post 223 processors work as described above and example configuration properties are 224 shown below: 225 226 ``` {.javascript} 227 { 228 "post-processors": [ 229 [ 230 { 231 "type": "docker-tag", 232 "repository": "12345.dkr.ecr.us-east-1.amazonaws.com/packer", 233 "tag": "0.7" 234 }, 235 { 236 "type": "docker-push", 237 "login": true, 238 "login_email": "none", 239 "login_username": "AWS", 240 "login_password": "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 241 "login_server": "https://12345.dkr.ecr.us-east-1.amazonaws.com/" 242 } 243 ] 244 ] 245 } 246 ``` 247 248 See the 249 [AWS documentation](https://docs.aws.amazon.com/AmazonECR/latest/userguide/Registries.html) 250 for steps to obtain Amazon ECR registry credentials. 251 252 253 ## Dockerfiles 254 255 This builder allows you to build Docker images *without* Dockerfiles. 256 257 With this builder, you can repeatably create Docker images without the use of a 258 Dockerfile. You don't need to know the syntax or semantics of Dockerfiles. 259 Instead, you can just provide shell scripts, Chef recipes, Puppet manifests, 260 etc. to provision your Docker container just like you would a regular 261 virtualized or dedicated machine. 262 263 While Docker has many features, Packer views Docker simply as an LXC container 264 runner. To that end, Packer is able to repeatably build these LXC containers 265 using portable provisioning scripts. 266 267 Dockerfiles have some additional features that Packer doesn't support which are 268 able to be worked around. Many of these features will be automated by Packer in 269 the future: 270 271 - Dockerfiles will snapshot the container at each step, allowing you to go 272 back to any step in the history of building. Packer doesn't do this yet, but 273 inter-step snapshotting is on the way. 274 275 - Dockerfiles can contain information such as exposed ports, shared volumes, 276 and other metadata. Packer builds a raw Docker container image that has none 277 of this metadata. You can pass in much of this metadata at runtime with 278 `docker run`.