github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/website/source/docs/builders/docker.html.markdown (about)

     1  ---
     2  layout: "docs"
     3  page_title: "Docker Builder"
     4  ---
     5  
     6  # Docker Builder
     7  
     8  Type: `docker`
     9  
    10  The Docker builder builds [Docker](http://www.docker.io) images using
    11  Docker. The builder starts a Docker container, runs provisioners within
    12  this container, then exports the container for reuse or commits the image.
    13  
    14  Packer builds Docker containers _without_ the use of
    15  [Dockerfiles](http://docs.docker.io/en/latest/use/builder/).
    16  By not using Dockerfiles, Packer is able to provision
    17  containers with portable scripts or configuration management systems
    18  that are not tied to Docker in any way. It also has a simpler mental model:
    19  you provision containers much the same way you provision a normal virtualized
    20  or dedicated server. For more information, read the section on
    21  [Dockerfiles](#toc_4).
    22  
    23  The Docker builder must run on a machine that has Docker installed. Therefore
    24  the builder only works on machines that support Docker (modern Linux machines).
    25  If you want to use Packer to build Docker containers on another platform,
    26  use [Vagrant](http://www.vagrantup.com) to start a Linux environment, then
    27  run Packer within that environment.
    28  
    29  ## Basic Example: Export
    30  
    31  Below is a fully functioning example. It doesn't do anything useful, since
    32  no provisioners are defined, but it will effectively repackage an image.
    33  
    34  <pre class="prettyprint">
    35  {
    36    "type": "docker",
    37    "image": "ubuntu",
    38    "export_path": "image.tar"
    39  }
    40  </pre>
    41  
    42  ## Basic Example: Commit
    43  
    44  Below is another example, the same as above but instead of exporting the
    45  running container, this one commits the container to an image. The image
    46  can then be more easily tagged, pushed, etc.
    47  
    48  <pre class="prettyprint">
    49  {
    50    "type": "docker",
    51    "image": "ubuntu",
    52    "commit": true
    53  }
    54  </pre>
    55  
    56  
    57  ## Configuration Reference
    58  
    59  Configuration options are organized below into two categories: required and
    60  optional. Within each category, the available options are alphabetized and
    61  described.
    62  
    63  ### Required:
    64  
    65  * `commit` (boolean) - If true, the container will be committed to an
    66    image rather than exported. This cannot be set if `export_path` is set.
    67  
    68  * `export_path` (string) - The path where the final container will be exported
    69    as a tar file. This cannot be set if `commit` is set to true.
    70  
    71  * `image` (string) - The base image for the Docker container that will
    72    be started. This image will be pulled from the Docker registry if it
    73    doesn't already exist.
    74  
    75  ### Optional:
    76  
    77  * `login` (boolean) - Defaults to false. If true, the builder will
    78      login in order to pull the image. The builder only logs in for the
    79      duration of the pull. It always logs out afterwards.
    80  
    81  * `login_email` (string) - The email to use to authenticate to login.
    82  
    83  * `login_username` (string) - The username to use to authenticate to login.
    84  
    85  * `login_password` (string) - The password to use to authenticate to login.
    86  
    87  * `login_server` (string) - The server address to login to.
    88  
    89  * `pull` (boolean) - If true, the configured image will be pulled using
    90    `docker pull` prior to use. Otherwise, it is assumed the image already
    91    exists and can be used. This defaults to true if not set.
    92  
    93  * `run_command` (array of strings) - An array of arguments to pass to
    94    `docker run` in order to run the container. By default this is set to
    95    `["-d", "-i", "-t", "{{.Image}}", "/bin/bash"]`.
    96    As you can see, you have a couple template variables to customize, as well.
    97  
    98  * `volumes` (map of strings to strings) - A mapping of additional volumes
    99     to mount into this container. The key of the object is the host path,
   100     the value is the container path.
   101  
   102  ## Using the Artifact: Export
   103  
   104  Once the tar artifact has been generated, you will likely want to import, tag,
   105  and push it to a container repository. Packer can do this for you automatically
   106  with the [docker-import](/docs/post-processors/docker-import.html) and
   107  [docker-push](/docs/post-processors/docker-push.html) post-processors.
   108  
   109  **Note:** This section is covering how to use an artifact that has been
   110  _exported_. More specifically, if you set `export_path` in your configuration.
   111  If you set `commit`, see the next section.
   112  
   113  The example below shows a full configuration that would import and push
   114  the created image:
   115  
   116  <pre class="prettyprint">
   117  {
   118      "post-processors": [
   119  		[
   120  			{
   121  				"type": "docker-import",
   122  				"repository": "mitchellh/packer",
   123  				"tag": "0.7"
   124  			},
   125  			"docker-push"
   126  		]
   127  	]
   128  }
   129  </pre>
   130  
   131  If you want to do this manually, however, perhaps from a script, you can
   132  import the image using the process below:
   133  
   134      docker import - registry.mydomain.com/mycontainer:latest < artifact.tar
   135  
   136  You can then add additional tags and push the image as usual with `docker tag`
   137  and `docker push`, respectively.
   138  
   139  ## Using the Artifact: Committed
   140  
   141  If you committed your container to an image, you probably want to tag,
   142  save, push, etc. Packer can do this automatically for you. An example is
   143  shown below which tags and pushes the image:
   144  
   145  <pre class="prettyprint">
   146  {
   147      "post-processors": [
   148  		[
   149  			{
   150  				"type": "docker-tag",
   151  				"repository": "mitchellh/packer",
   152  				"tag": "0.7"
   153  			},
   154  			"docker-push"
   155  		]
   156  	]
   157  }
   158  </pre>
   159  
   160  ## Dockerfiles
   161  
   162  This builder allows you to build Docker images _without_ Dockerfiles.
   163  
   164  With this builder, you can repeatably create Docker images without the use of
   165  a Dockerfile. You don't need to know the syntax or semantics of Dockerfiles.
   166  Instead, you can just provide shell scripts, Chef recipes, Puppet manifests,
   167  etc. to provision your Docker container just like you would a regular
   168  virtualized or dedicated machine.
   169  
   170  While Docker has many features, Packer views Docker simply as an LXC
   171  container runner. To that end, Packer is able to repeatably build these
   172  LXC containers using portable provisioning scripts.
   173  
   174  Dockerfiles have some additional features that Packer doesn't support
   175  which are able to be worked around. Many of these features will be automated
   176  by Packer in the future:
   177  
   178  * Dockerfiles will snapshot the container at each step, allowing you to
   179    go back to any step in the history of building. Packer doesn't do this yet,
   180    but inter-step snapshotting is on the way.
   181  
   182  * Dockerfiles can contain information such as exposed ports, shared
   183    volumes, and other metadata. Packer builds a raw Docker container image
   184    that has none of this metadata. You can pass in much of this metadata
   185    at runtime with `docker run`.