diff --git a/builder/docker/driver.go b/builder/docker/driver.go index ad9bff11f..aaed2fc52 100644 --- a/builder/docker/driver.go +++ b/builder/docker/driver.go @@ -20,6 +20,9 @@ type Driver interface { // Pull should pull down the given image. Pull(image string) error + // Push pushes an image to a Docker index/registry. + Push(name string) error + // StartContainer starts a container and returns the ID for that container, // along with a potential error. StartContainer(*ContainerConfig) (string, error) diff --git a/builder/docker/driver_docker.go b/builder/docker/driver_docker.go index b78fc2a21..5c43e0078 100644 --- a/builder/docker/driver_docker.go +++ b/builder/docker/driver_docker.go @@ -93,6 +93,11 @@ func (d *DockerDriver) Pull(image string) error { return runAndStream(cmd, d.Ui) } +func (d *DockerDriver) Push(name string) error { + cmd := exec.Command("docker", "push", name) + return runAndStream(cmd, d.Ui) +} + func (d *DockerDriver) StartContainer(config *ContainerConfig) (string, error) { // Build up the template data var tplData startContainerTemplate diff --git a/builder/docker/driver_mock.go b/builder/docker/driver_mock.go index a737c3240..a48bb99f8 100644 --- a/builder/docker/driver_mock.go +++ b/builder/docker/driver_mock.go @@ -16,6 +16,10 @@ type MockDriver struct { ImportId string ImportErr error + PushCalled bool + PushName string + PushErr error + ExportReader io.Reader ExportError error PullError error @@ -68,6 +72,12 @@ func (d *MockDriver) Pull(image string) error { return d.PullError } +func (d *MockDriver) Push(name string) error { + d.PushCalled = true + d.PushName = name + return d.PushErr +} + func (d *MockDriver) StartContainer(config *ContainerConfig) (string, error) { d.StartCalled = true d.StartConfig = config diff --git a/website/source/docs/builders/docker.html.markdown b/website/source/docs/builders/docker.html.markdown index fbced5009..af827a14d 100644 --- a/website/source/docs/builders/docker.html.markdown +++ b/website/source/docs/builders/docker.html.markdown @@ -64,15 +64,31 @@ Optional: `["run", "-d", "-i", "-t", "-v", "{{.Volumes}}", "{{.Image}}", "/bin/bash"]`. As you can see, you have a couple template variables to customize, as well. -## Using the generated artifact +## Using the Artifact Once the tar artifact has been generated, you will likely want to import, tag, -and push it to a container repository. Until packer supports management of the -docker image metadata, this process is manual. For example, the following will -import `mycontainer-123456789.tar` to the repository -`registry.mydomain.com/mycontainer`, tagged with `latest`: +and push it to a container repository. Packer can do this for you automatically +with the [docker-import](/docs/post-processors/docker-import.html) and +[docker-push](/docs/post-processors/docker-push.html) post-processors. - sudo docker import - registry.mydomain.com/mycontainer:latest < mycontainer-123456789.tar +The example below shows a full configuration that would import and push +the created image: + +
+{
+    "post-processors": [
+		[
+			{ "type": "docker-import", "repository": "mitchellh/packer", "tag": "0.7" },
+			"docker-push"
+		]
+	]
+}
+
+ +If you want to do this manually, however, perhaps from a script, you can +import the image using the process below: + + docker import - registry.mydomain.com/mycontainer:latest < artifact.tar You can then add additional tags and push the image as usual with `docker tag` and `docker push`, respectively. @@ -103,8 +119,3 @@ by Packer in the future: volumes, and other metadata. Packer builds a raw Docker container image that has none of this metadata. You can pass in much of this metadata at runtime with `docker run`. - -* Images made without dockerfiles are missing critical metadata that - make them easily pushable to the Docker registry. You can work around - this by using a metadata-only Dockerfile with the exported image and - building that. A future Packer version will automatically do this for you. diff --git a/website/source/docs/post-processors/docker-import.html.markdown b/website/source/docs/post-processors/docker-import.html.markdown new file mode 100644 index 000000000..e2ca3c93b --- /dev/null +++ b/website/source/docs/post-processors/docker-import.html.markdown @@ -0,0 +1,44 @@ +--- +layout: "docs" +page_title: "docker-import Post-Processor" +--- + +# Docker Import Post-Processor + +Type: `docker-import` + +The Docker import post-processor takes an artifact from the +[docker builder](/docs/builders/docker.html) and imports it with Docker +locally. This allows you to apply a repository and tag to the image +and lets you use the other Docker post-processors such as +[docker-push](/docs/post-processors/docker-push.html) to push the image +to a registry. + +## Configuration + +The configuration for this post-processor is extremely simple. At least +a repository is required. The tag is optional. + +* `repository` (string) - The repository of the imported image. + +* `tag` (string) - The tag for the imported image. By default this is not + set. + +## Example + +An example is shown below, showing only the post-processor configuration: + +
+{
+  "type": "docker-import",
+  "repository": "mitchellh/packer",
+  "tag": "0.7"
+}
+
+ +This example would take the image created by the Docker builder +and import it into the local Docker process with a name of `mitchellh/packer:0.7`. + +Following this, you can use the +[docker-push](/docs/post-processors/docker-push.html) +post-processor to push it to a registry, if you want. diff --git a/website/source/docs/post-processors/docker-push.html.markdown b/website/source/docs/post-processors/docker-push.html.markdown new file mode 100644 index 000000000..602e126e0 --- /dev/null +++ b/website/source/docs/post-processors/docker-push.html.markdown @@ -0,0 +1,28 @@ +--- +layout: "docs" +page_title: "Docker Push Post-Processor" +--- + +# Docker Push Post-Processor + +Type: `docker-push` + +The Docker push post-processor takes an artifact from the +[docker-import](/docs/post-processors/docker-import.html) post-processor +and pushes it to a Docker registry. + +
+Before you use this, you must manually docker login +to the proper repository. A future version of Packer will automate this +for you, but for now you must manually do this. +
+ +## Configuration + +This post-processor has no configuration! Simply add it to your chain +of post-processors and the image will be uploaded. + +## Example + +For an example of using docker-push, see the section on using +generated artifacts from the [docker builder](/docs/builders/docker.html). diff --git a/website/source/layouts/docs.erb b/website/source/layouts/docs.erb index d8a70dc56..30744b372 100644 --- a/website/source/layouts/docs.erb +++ b/website/source/layouts/docs.erb @@ -54,6 +54,8 @@