6 ms·
Docker Development Patterns
- willvarfar 12y agoNice useful article. Accurate title too, but one can't help but think this a dumbing down of HN when tech articles follow the "10 things you never knew were worth listing" route ;)
- bryanlarsen 12y agoYou just caused the mods to remove a useful piece of information from the title. Pluralization in an article like this implies a "few". The word eight tells us that there are more than a few. Eight is also not round, so it tells us that the author didn't stretch to get 10 in the style of those articles you disparage.
- dang 12y agoMods routinely remove arbitrary numbers from titles, because HN's guidelines exclude them as a bait device. This rule has served the site well over the years. Yes, there are borderline cases, but surprisingly few.
- Oculus 12y agoFrom a sideline viewers perspective, persistence in production for Docker containers is a big problem that I've yet to see a good solution for. You end up having to keep DB servers and such outside of your container pool.
- dugmartin 12y agoI haven't used Docker in production but isn't the standard practice for persistence to mount a host directory as a data volume?
- vidarh 12y agoYou don't. That's the point of volumes. You do need to be careful to ensure you mount volumes or everything that needs to persist, but in practice that's not a very onerous limitation. Since volumes are bind-mounted from the outside, you can put the volumes on whatever storage pool you want that you can bring up on the host (as long as it meets your apps requirements, e.g. POSIX semantics, locking etc). E.g. at work we have a private Docker repository that runs on top of GlusterFS volumes that are mounted on thehost and then bind mounted in as a volume in the container. I also run my postgres instances inside Docker, with the data on persistent volumes. We could certainly use better tools to manage it, though. Another pattern I ought to have mentioned (that I've used myself) is to set up "empty" containers whose only purpose is to act as a storage volume for another container. I don't like that as much, mostly since I've not had as much long-term experience with how the layering Docker uses would impact it.
- Rafert 12y agohttps://github.com/clusterhq/flocker https://github.com/clusterhq/flocker seems to be doing some interesting stuff in that direction.
- cpuguy83 12y agoAnd we're working with the folks @ clusterhq to bring some of that into Docker.
- eropple 12y agoHaving persistent containers strikes me as the infrastructural equivalent of a code smell. I don't use in-container storage for anything that needs to be persisted at all, and I'm not sure why you would in a modern environment. Everything can fail and fail hard, and writing meaningful (i.e., volume'd) data to disk seems like asking for trouble. Ephemeral containers just seems to fit the model of Docker's capabilities much better than maintaining state. The exception to this would be, I guess, you could wedge in Docker containers for database server isolation or something, but my databases don't run on multi-tenant instances so there isn't a huge win to it. (I use RDS most of the time, let somebody else manage that problem.)
- deleted 12y ago[deleted]
- cpuguy83 12y agoSo, we're making volumes better! Please see: https://github.com/docker/docker/pull/8484 https://github.com/docker/docker/pull/8484 And that is only the beginning. It may be presumptuous to say the referenced PR will be in 1.4, but that's certainly what I'm pushing for.
- vidarh 12y agoAny plans on making it possible to control what's backing those volumes more directly? For me, when I'm using data volumes, it's generally because I have specific requirements (e.g. want my database on my expensive SSD array's; want my high availability file storage on a Gluster volume or similar) that doesn't make it interesting to have them co-located with the container storage. I don't want to care if the container storage is totally destroyed - I want to just re-create it from our registry on a different host. I keep threatening the devs I work with that I'll wipe the containers regularly, for a reason, and they're specifically and intentionally not backed up. From what I hear, the idea of keeping the containers totally disposable is a key appeal of Docker for many (me included), so I would just not use any volume related functionality that co-mingles the data volumes other than in very special circumstances (e.g. lets say we had static datasets that we'd like to mix with an application in different configurations; but I don't have any actual, real-life use cases for that at the moment; basically I'd only do that if I then also could push those volumes into our registry)
- cpuguy83 12y agoFirst, volumes are 100% separated from containers. If you remove a container, the volumes are not removed unless you explicitly told docker to (docker rm -v <container>). And even then, it won't remove the volume if other containers are using it. That said, it is hard to re-use a volume right now if you removed the last container referencing that volume. The linked PR solves this issue. You can mount these SSD's on the host and then use bind-mounts (docker run -v /host/path:/container/path) to get those into the container. Or you can add the devices to the containers directly ("docker run --device /dev/sdb", for example), but you'd need privilaged access to actually mount the device. The referenced PR itself doesn't really make using things like specialized disks any easier, except you can register them with the volumes subsystem and it will track them form you, ie `docker volumes create --path /path/to/data --name my_speedy_disks`. There was some discussion around being able to deal with devices directly with Docker instead of expecting the admin to handle mounting those devices onto the host so they can be used as volumes. Nothing finalized here. Here is where that discussion happened: https://botbot.me/freenode/docker-dev/2014-10-21/?msg=23911528&page=5 https://botbot.me/freenode/docker-dev/2014-10-21/?msg=239115...
- cpuguy83 12y agoTo be honest, I'm very much against this approach to containerized development. The project should have a single Dockerfile from which dev/test/staging/production work from. There are certaily things you may want to change in these envs (particularly dev and test) but these can be runtime configurations (through env vars or changing the command that gets run in the container). When I do dev, if I need to do so interactively I build from my main dockerfile and do `docker run -v $(pwd):/opt/myapp myapp bash`, which mounts my live code into the container, overwriting the code that was built in. I'll mess around with some things, exit and adjust my Dockerfile if needed based on those changes. To run tests (as an example): `docker run -e RAILS_ENV test -v $(pwd):/opt/myapp myapp rake test` If all is well and I'm ready to push my code up, go ahead and rebuild the image with the changed code, run the tests again, etc. Sometimes it is neccessary to have some tools like gdb, strace, etc, available within the container. For this you can startup the container as you normally would, then (with Docker 1.3) `docker exec -it /bin/bash`, install the tools I need to debug something, and get out. In general it is just going to be bad practice to have a significantly different from the production env, and is indeed why dev handoff to production is often wrought with trouble.
- vidarh 12y ago> The project should have a single Dockerfile from which dev/test/staging/production work from. I don't agree with this largely, I think because we actually substantially agree about the need to test regularly in an environment identical to staging/production during development. I DO agree that there should be a single Dockerfile to be used as the basis, and I DO agree that you should regularly run that Dockerfile in dev/test/staging/production unmodified. But part of the point is that there is no reason to limit yourself to a single dev instance of your app. I appreciate that might have been somewhat obscured because of some of my examples, which uses a separate dev container setup that I use for some projects as the base, including as a base for the production container for my blog. So to reiterate, I absolutely agree with having a Dockerfile that is identical to your live environment to bring up an instance with during development. But that is not a reason to not _also_ have a container that bring it up with a code reloader, or a container that lets you test in different environments. And it certainly is not a reason to avoid isolating dev/build dependencies into yet other containers. I actually much prefer that to the "Rails way" of having separate environments "built into" the app, and that's probably part of the difference. Instead of doing that, I use "FROM" in Dockerfiles to "inherit" from bases to provide me with different "lenses" to work on the project from different viewpoints. Not just dev vs. live, but also with debugging tools, or architecture differences etc. > Sometimes it is neccessary to have some tools like gdb, strace, etc, available within the container. For this you can startup the container as you normally would, then (with Docker 1.3) `docker exec -it /bin/bash`, install the tools I need to debug something, and get out. But there's no reason to keep redoing these steps when they can be encapsulated in a Dockerfile that inherits from your basic container. And you can easily run it in parallel with your "pristine" containers, or bring it up in seconds as needed. > In general it is just going to be bad practice to have a significantly different from the production env, and is indeed why dev handoff to production is often wrought with trouble. We fully agree with this. It's a decade of frustration doing devops that has made me enjoy the opportunities Docker brings to layer situation-specific dependencies on top of a pristine base: It takes away any excuse for letting the basic setup diverge, or for being unable to run full tests on an environment identical to live.
- antocv 12y agoWe are doing something similar at work, specifying exact few dependencies needed on the host and their versions (mostly docker, tar etc), then we make a chroot environment using debootstrap from known few packages and versions, from this we make a docker image (tar cf - chroot | docker import) and use this as base for build environment. We could use pacstrap or whatever too, the initial selection of packages is important. For any package which we need to build, we have many packages with dependencies between them, are specified in a Makefile - because make is good at dependency tracking, but when make needs to (re)-build a package, it does so using the docker build-container-base image, whatever that package does is committed for later analysis under the name of the package built and timestamp, for traceability. For every package built, the resulting docker image, with build-environment-and-package-plus-timestamp name, is run but now, trying to install the package and commit that image as well with 'install' tag. - so we also verify the install step basically. Btw, this is for openstack, many components, packages and dependencies all over the place.
- atbell 12y agoI'm extremely surprised that Packer (http://www.packer.io http://www.packer.io) hasn't received more attention in the Docker arena. Its Docker builder, coupled with any of its provisioners, makes it a pretty attractive alternative to the simplistic layout and limitations of Dockerfiles.
- mtalantikite 12y agoPacker currently doesn't snapshot the build at each step, so you don't get a history that you can checkout or push from. It's a feature that's on the way, but it's a pretty big one that some teams need. We use packer for a lot of other things though and it's great.