hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
15.12k stars 3.33k forks source link

Docker builder RFC #604

Closed mwhooker closed 11 years ago

mwhooker commented 11 years ago

This document is to gather input from the community both on how packer can most effectively benefit from docker, and how packer can provide the best utility to docker users.

Use Cases

Build docker containers. I want to use the provisioner facilities in packer to generate a docker container as the end result.

Replacement for chroot builders. Chroots, as used by the amazon-chroot builder, are ineffectual as a means of namespacing because processes are still scheduled by the host OS. If sysvinit/upstart/etc start any processes as part of provisioning, the chroot won't be unmountable while the processes are still running. I've attempted a few remedies, including managing policyrc.d, but it's become apparent that this path is too complex to work in the general case.

Proposed implementation

I am still spinning up on docker, so please point out any technical errors in the proposal.

Docker Builder

We can start with a docker builder as the base use case.

Docker uses a build file called a Dockerfile. I believe we can create a docker Communicator which will write to the Dockerfile, though I suspect there's an IPC protocol that's usable.

The final artifact will be a filesystem tarball ie from running docker --export.

Provisioning

There should be a way to seed provisioning with a base docker image.

AMI intermediate.

The chroot replacement use case looks a bit simpler once we have the docker tarball. I have to do more research into packer and docker both, but it seems like the steps should be

  1. builder docker image
  2. convert exported container to AMI using similar code in the amazon/chroot builder

I don't know if it would be better to do this step as a post-processor or as a separate builder

Comments

I would greatly appreciate feedback, documentation, links to extant code, etc.

mitchellh commented 11 years ago

There was a fairly lengthy discussion in #24 I assume you've read but if you haven't, you should because we've crossed a lot of these bridges already.

Responding to your points, I think there are a lot more Docker integration points:

In addition to this I realize you want to create AMIs from Docker containers. I actually have no real knowledge here but I imagine by doing the above that process can become a lot more clear.

mwhooker commented 11 years ago

thanks @mitchellh! I admittedly only glanced over the issue. I'll give it some closer attention but I think we're on the same page. If my intentions weren't clear I'll reword it so they are. I want to do everything you mention wrt the Builder using a Communicator and spitting out a container, etc.

The provisioner is also an excellent point. I want to achieve the same thing in my use-case, but with EBS snapshots. I'll add some points about this.

fgrehm commented 11 years ago

I'm not too familiar with AMIs so here's my 2 cents from the lxc side of things after only 9 months of "lxc experience" :)

My understanding based is that the "source" of both Docker / vagrant-lxc / "plain old lxc" containers are basically a rootfs tarball + some configurations and are usually build out of a debootstrap process. To give you a better idea, here's some scripts used for building the those images:

I haven't used Packer enough to know if what I have in mind makes sense but the way I see it working is to have a "lxc rootfs tarball builder" (with a better name of course :P) that creates the container and brings it up with lxc-start so that Packer provisioners can kick in to do their work. After provisioners are run it just lxc-stops it and a tarball of the rootfs gets built.

With that tarball around, the user can easily import it into docker with sudo tar --numeric-owner -c . | docker import ... by hand or we can have a vagrant post processor that does whatever is needed to build a vagrant-lxc box. For those using plain old lxc, I think they'll probably be able to find their way of feeding that tarball back into their environment :)

Does that make sense?

EDIT: I've just came across this message on Docker's mailing list and it seems that I'm not alone:

Hmmmm I'm interested in using Packer from the other end; to make a tarball of an OS that can be imported into Docker as well as being exported to EC2, Virtualbox, and other formats. I think I might need to make a new Packer output plugin for that though.

mitchellh commented 11 years ago

@mwhooker Can you describe to me what the issues with the chroot builder are again? I remember issues with process management. Is it not possible to place the entire chroot somehow under a cgroup and control it using that?

mitchellh commented 11 years ago

@mwhooker Also, I agree, I think we're on the same page! :)

mitchellh commented 11 years ago

@fgrehm Thanks!

mwhooker commented 11 years ago

@mitchellh That's essentially the issue. We're looking to cgroups to solve it, but via docker, which has the added benefit of describing a container format so we can use the produced images in different environments.

mitchellh commented 11 years ago

@mwhooker I'm going to work on the Docker builder that doesn't use Dockerfiles.

mwhooker commented 11 years ago

@mitchellh excellent. I'll be in irc if you want some help.

mitchellh commented 11 years ago

@mwhooker I'm working in this branch: https://github.com/mitchellh/packer/tree/f-docker-builder

I just one hour trying to get the Communicator Start method to work, but Docker is not cooperating. In a terminal if you do echo 'echo foo' | docker attach 1234 then it will execute that command and return output. I can't mimic that behavior with os/exec for some reason. I've tried everything I can think of here.

At this point I'm going to call it a roadblock, will sleep on it.

mwhooker commented 11 years ago

nice job. I'll poke at it

mitchellh commented 11 years ago

Updated: I've worked around every issue so far. We're on our way! Progress screenshot shown below:

hehe

mitchellh commented 11 years ago

I probably should've made a pull request (sorry), but I merged in the docker builder! /cc @mwhooker

https://github.com/mitchellh/packer/commit/8cc09bcd56560db989661d6c33fe731a5e886bd3

Works as you would expect, is fully documented in the website, etc.

mitchellh commented 11 years ago

I'm actually going to close this since it is in. We can make another for the chroot builder or provisioner when the time comes.