github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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](#dockerfiles).
    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  -   `author` (string) - Set the author (e-mail) of a commit.
    90  
    91  -   `aws_access_key` (string) - The AWS access key used to communicate with AWS.
    92      [Learn how to set this.](/docs/builders/amazon.html#specifying-amazon-credentials)
    93  
    94  -   `aws_secret_key` (string) - The AWS secret key used to communicate with AWS.
    95      [Learn how to set this.](/docs/builders/amazon.html#specifying-amazon-credentials)
    96  
    97  -   `aws_token` (string) - The AWS access token to use. This is different from the
    98      access key and secret key. If you're not sure what this is, then you
    99      probably don't need it. This will also be read from the `AWS_SESSION_TOKEN`
   100      environmental variable.
   101  
   102  -   `changes` (array of strings) - Dockerfile instructions to add to the commit.
   103      Example of instructions are `CMD`, `ENTRYPOINT`, `ENV`, and `EXPOSE`. Example:
   104      `[ "USER ubuntu", "WORKDIR /app", "EXPOSE 8080" ]`
   105  
   106  -   `ecr_login` (boolean) - Defaults to false. If true, the builder will login in
   107      order to pull the image from
   108      [Amazon EC2 Container Registry (ECR)](https://aws.amazon.com/ecr/).
   109      The builder only logs in for the duration of the pull. If true
   110      `login_server` is required and `login`, `login_username`, and
   111      `login_password` will be ignored. For more information see the
   112      [section on ECR](#amazon-ec2-container-registry).
   113  
   114  -   `login` (boolean) - Defaults to false. If true, the builder will login in
   115      order to pull the image. The builder only logs in for the duration of
   116      the pull. It always logs out afterwards. For log into ECR see `ecr_login`.
   117  
   118  -   `login_email` (string) - The email to use to authenticate to login.
   119  
   120  -   `login_username` (string) - The username to use to authenticate to login.
   121  
   122  -   `login_password` (string) - The password to use to authenticate to login.
   123  
   124  -   `login_server` (string) - The server address to login to.
   125  
   126  -   `message` (string) - Set a message for the commit.
   127  
   128  -   `privileged` (boolean) - If true, run the docker container with the
   129      `--privileged` flag. This defaults to false if not set.
   130  
   131  -   `pull` (boolean) - If true, the configured image will be pulled using
   132      `docker pull` prior to use. Otherwise, it is assumed the image already
   133      exists and can be used. This defaults to true if not set.
   134  
   135  -   `run_command` (array of strings) - An array of arguments to pass to
   136      `docker run` in order to run the container. By default this is set to
   137      `["-d", "-i", "-t", "{{.Image}}", "/bin/bash"]`. As you can see, you have a
   138      couple template variables to customize, as well.
   139  
   140  -   `volumes` (map of strings to strings) - A mapping of additional volumes to
   141      mount into this container. The key of the object is the host path, the value
   142      is the container path.
   143  
   144  ## Using the Artifact: Export
   145  
   146  Once the tar artifact has been generated, you will likely want to import, tag,
   147  and push it to a container repository. Packer can do this for you automatically
   148  with the [docker-import](/docs/post-processors/docker-import.html) and
   149  [docker-push](/docs/post-processors/docker-push.html) post-processors.
   150  
   151  **Note:** This section is covering how to use an artifact that has been
   152  *exported*. More specifically, if you set `export_path` in your configuration.
   153  If you set `commit`, see the next section.
   154  
   155  The example below shows a full configuration that would import and push the
   156  created image. This is accomplished using a sequence definition (a collection of
   157  post-processors that are treated as as single pipeline, see
   158  [Post-Processors](/docs/templates/post-processors.html) for more information):
   159  
   160  ``` {.javascript}
   161  {
   162    "post-processors": [
   163      [
   164        {
   165          "type": "docker-import",
   166          "repository": "mitchellh/packer",
   167          "tag": "0.7"
   168        },
   169        "docker-push"
   170      ]
   171    ]
   172  }
   173  ```
   174  
   175  In the above example, the result of each builder is passed through the defined
   176  sequence of post-processors starting first with the `docker-import`
   177  post-processor which will import the artifact as a docker image. The resulting
   178  docker image is then passed on to the `docker-push` post-processor which handles
   179  pushing the image to a container repository.
   180  
   181  If you want to do this manually, however, perhaps from a script, you can import
   182  the image using the process below:
   183  
   184  ``` {.text}
   185  $ docker import - registry.mydomain.com/mycontainer:latest < artifact.tar
   186  ```
   187  
   188  You can then add additional tags and push the image as usual with `docker tag`
   189  and `docker push`, respectively.
   190  
   191  ## Using the Artifact: Committed
   192  
   193  If you committed your container to an image, you probably want to tag, save,
   194  push, etc. Packer can do this automatically for you. An example is shown below
   195  which tags and pushes an image. This is accomplished using a sequence definition
   196  (a collection of post-processors that are treated as as single pipeline, see
   197  [Post-Processors](/docs/templates/post-processors.html) for more information):
   198  
   199  ``` {.javascript}
   200  {
   201    "post-processors": [
   202      [
   203        {
   204          "type": "docker-tag",
   205          "repository": "mitchellh/packer",
   206          "tag": "0.7"
   207        },
   208        "docker-push"
   209      ]
   210    ]
   211  }
   212  ```
   213  
   214  In the above example, the result of each builder is passed through the defined
   215  sequence of post-processors starting first with the `docker-tag` post-processor
   216  which tags the committed image with the supplied repository and tag information.
   217  Once tagged, the resulting artifact is then passed on to the `docker-push`
   218  post-processor which handles pushing the image to a container repository.
   219  
   220  Going a step further, if you wanted to tag and push an image to multiple
   221  container repositories, this could be accomplished by defining two,
   222  nearly-identical sequence definitions, as demonstrated by the example below:
   223  
   224  ``` {.javascript}
   225  {
   226    "post-processors": [
   227      [
   228        {
   229          "type": "docker-tag",
   230          "repository": "mitchellh/packer",
   231          "tag": "0.7"
   232        },
   233        "docker-push"
   234      ],
   235      [
   236        {
   237          "type": "docker-tag",
   238          "repository": "hashicorp/packer",
   239          "tag": "0.7"
   240        },
   241        "docker-push"
   242      ]
   243    ]
   244  }
   245  ```
   246  
   247  <span id="amazon-ec2-container-registry"></span>
   248  
   249  ## Amazon EC2 Container Registry
   250  
   251  Packer can tag and push images for use in
   252  [Amazon EC2 Container Registry](https://aws.amazon.com/ecr/). The post
   253  processors work as described above and example configuration properties are
   254  shown below:
   255  
   256  ``` {.javascript}
   257  {
   258    "post-processors": [
   259      [
   260        {
   261          "type": "docker-tag",
   262          "repository": "12345.dkr.ecr.us-east-1.amazonaws.com/packer",
   263          "tag": "0.7"
   264        },
   265        {
   266          "type": "docker-push",
   267          "ecr_login": true,
   268          "aws_access_key": "YOUR KEY HERE",
   269          "aws_secret_key": "YOUR SECRET KEY HERE",
   270          "login_server": "https://12345.dkr.ecr.us-east-1.amazonaws.com/"
   271        }
   272      ]
   273    ]
   274  }
   275  ```
   276  
   277  [Learn how to set Amazon AWS credentials.](/docs/builders/amazon.html#specifying-amazon-credentials)
   278  
   279  ## Dockerfiles
   280  
   281  This builder allows you to build Docker images *without* Dockerfiles.
   282  
   283  With this builder, you can repeatably create Docker images without the use of a
   284  Dockerfile. You don't need to know the syntax or semantics of Dockerfiles.
   285  Instead, you can just provide shell scripts, Chef recipes, Puppet manifests,
   286  etc. to provision your Docker container just like you would a regular
   287  virtualized or dedicated machine.
   288  
   289  While Docker has many features, Packer views Docker simply as an container
   290  runner. To that end, Packer is able to repeatably build these containers
   291  using portable provisioning scripts.
   292  
   293  Dockerfiles have some additional features that Packer doesn't support which are
   294  able to be worked around. Many of these features will be automated by Packer in
   295  the future:
   296  
   297  -   Dockerfiles will snapshot the container at each step, allowing you to go
   298      back to any step in the history of building. Packer doesn't do this yet, but
   299      inter-step snapshotting is on the way.
   300  
   301  -   Dockerfiles can contain information such as exposed ports, shared volumes,
   302      and other metadata. Packer builds a raw Docker container image that has none
   303      of this metadata. You can pass in much of this metadata at runtime with
   304      `docker run`.