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