r/programming • u/[deleted] • Apr 18 '15
What is Docker and When To Use It
http://www.centurylinklabs.com/what-is-docker-and-when-to-use-it/104
u/5-4-3-2-1-bang Apr 18 '15
Honestly I still have no fucking idea what docker is and why I would use it.
Here's what it isn't: thing #1 I don't use, thing #2 I don't use, thing #3 I've never heard of.
Here's what it's good at: it's good at using docker to docker! it's a manager for infrastructure! (it can build bridges and sandwiches!) You can dock your dockers in our central dock so that when you dock you can dock!
About the only thing I understood in the whole write up is that it uses btrfs for... something. Probably disk images since that's a filesystem. (Why is it managing a file system at all? Does it need to live on an underlying btrfs filesystem, or is it simply a sparse file that can live anywhere that is treated as a btrfs volume by docker?)
This is probably a great article if you already know what docker is and what it should be used for. If you don't have the basic framework already, it adds zero.
(...bring on the downvotes, whatever.)
32
u/MindStalker Apr 18 '15
No, I agree, this article is terrible, and I know what docker is. Ok first thing, you have linux containers. Linux containers have been around for many years. Essentially they allow you to run a process in a walled off environment, much like java or apps on your phone. Each container is contained to only do what it is allowed to do. Docker came around and realized that using containers was difficult. So Docker helps you by making creating then easy, configuring their network stack as easy as any other vm by declaring NAT ports, and managing their filesystem by setting what of the underlying os shines through, then all changes are managed as git changes with nothing being pushed down to the os. You can easily snapshot this system and share the snapshot via git repos.
2
u/Fumigator Apr 18 '15
So docker is just UML?
12
u/aseipp Apr 18 '15 edited Apr 18 '15
User-mode-Linux is actually an 'architecture variant' of the Linux kernel, like x86 or MIPS. Except it builds Linux to run as an ELF executable on Linux, rather than boot from bare metal. In other words - you are compiling the Linux kernel as an actual application on top of POSIX/glibc and running it as an application.
Linux namespaces are a different concept entirely. They allow the Linux kernel to 'namespace' applications in concrete ways. For example, if two processes A and B are put into a different 'PID namespace', then they see a completely different set of PIDs if they query the environment. In fact, both A and B may think they have 'pid 2' for example. Outside the container (for example in your user environment), the two processes have real pids that are different. Inside the namespace, they are isolated.
There are also:
networking namespaces (so the
eth0seen by A is private, and not the sameeth0B sees, thus private - but perhaps routed to the hostseth0as a bridge with separate IPs)user namespaces (so an application can think it has
uid = 0, but outside the namespace does not)filesystem namespaces, like a super-chroot (so that A and B can have separate mounts - for example, each can have a private
/tmpdirectory untouchable by other processes, and each can even have their own view of the actual filesystem. For example, you could make/homecompletely inaccessible so that it doesn't even look like it exists).and one or two others. Linux containers - including Docker - are essentially built by:
Taking a filesystem image of some OS in a directory (for example, like if you
debootstrapinto a directory to populate a Debian image, tochrootinto it)Making a lot of namespaces
Executing
/bin/initor/bin/bashor whatever inside of the filesystem namespace, conceptually like a chroot, to kick off the 'userspace'.You're in a shell.
Except your chroot may have private networking interfaces and new cool stuff.
Because namespaces simply isolate processes on a host system, obviously, all of those applications share the same kernel (although they might use, say, a different libc - just like if you
chrootsomwhere).Despite what the OP says, namespaces aren't really a 'restriction' technology - they are an isolation technology. Sure, you can't touch the same
/tmpdirectory, but that's not really a restriction. You need things like Linux capabilities (man 7 capabilities), cgroups, orseccomp-bpfor something to meaningfully restrict what processes can actually do, for example, by limiting their IO, system calls, or actual privileges like being able to bind to restricted ports.Docker basically takes namespaces, and uses union-filesystem technologies like OverlayFS, AUFS or even BtrFS, and adds concepts of 'images' to manage Linux distribution images (again, ones like the one
debootstrapcreates that you would use withchroot). These images start off with the base installations, but can be checkpointed, which adds a 'snapshot' of the changes (like a COW filesystem) to the image file. Then you use AUFS/UnionFS/BtrFS to layer the changes onto the base image, revert them, etc, and start your container. It's a lot like a shared, persistent data structure in this regard. The base image never changes; but you install mysql and 'commit' a new layer to the image, which is stored separately, and can be independently reverted.There are other approaches to container/namespace management too, like OpenVZ or
systemd-nspawn(systemd can also easily manage your cgroups to isolate container processes, and it also has useful options likePrivateTmp=yeswhich can automatically give any application/service unit a private/tmpmount via filesystem namespaces).2
u/Fumigator Apr 18 '15
Thank you for the very complete explanation! A little of it is beyond me, but only right now. It sounds quite fascinating and something I'll have to look into more in case something comes down the road that I might be able to use it on.
2
u/aseipp Apr 18 '15
LWN has a very complete series called "Namespaces in Operation" that has a lot of good code samples demonstrating these features: https://lwn.net/Articles/531114/
1
u/MindStalker Apr 19 '15
"Despite what the OP says, namespaces aren't really a 'restriction' technology - they are an isolation technology. Sure, you can't touch the same /tmp directory, but that's not really a restriction. You need things like Linux capabilities (man 7 capabilities), cgroups, or seccomp-bpf or something to meaningfully restrict what processes can actually do, for example, by limiting their IO, system calls, or actual privileges like being able to bind to restricted ports."
I've heard that SELinux can be used to secure docker containers? https://major.io/2014/04/21/launch-secure-lxc-containers-on-fedora-20-using-selinux-and-svirt/ Have you tried this? Do you know anything about its security?
1
u/postmodest Apr 18 '15
So is it fair to say that a "docker image" is just:
a .tgz snapshot of a chroot directory
that runs in an LXC container that only init's enough services to run one single program.
LXC has dummy network interfaces that you configure NAT-style rules for from your host network
LXC lets you define quotas for the whole "containerized" system
Docker is a program and a set of scripts to configure the container, the chroot filesystem, and deploy said elsewhere
Have I got that mostly right?
How is Docker different to Vagrant?
→ More replies (1)4
u/NotUniqueOrSpecial Apr 18 '15
Mostly right, yes.
Docker : Linux Containers :: Vagrant : Virtual Machines (broadly).
3
u/MindStalker Apr 18 '15 edited Apr 18 '15
Partially, 1 it always shares the underlying kernel, this makes it more lightweight, but also, you can't experiment with kernels like you can with UML. 2, its heavily tied in with git for managing its virtual filesystem and pushing pulling repos of its images.
BTW, one of the major advantages of not running your own kernel is speed. On a decent system you can take a non running image and start it and get a bash shell in literally 1 second.
4
Apr 18 '15
Docker does not use git at all.
→ More replies (4)2
u/MindStalker Apr 18 '15
Well having used it it uses the same identical commands as git. Reading the documentation it says its very similar to git. I had assumed they assembled some of the internal code as git. Maybe they didn't, but it seems to use the git protocol.
3
u/Novex Apr 18 '15
It borrows from the concept of pushing/pulling prebuilt images from a remote repository, but that's about where the similarity ends.
The underlying tech is just tar files, which is (impressive|unimpressive) compared to git.
→ More replies (1)7
u/andrewcooke Apr 18 '15 edited Apr 19 '15
it's really useful if you want to play with a database. if someone at work asks me how to write some sql to solve a problem i get them to send me some test data and a description of what they want as a result.
then i start up a docker instance running postgres, load the data, and play around until i have the right query or whatever.
the big advantages are that it's a new, completely "virgin" database, you have no risk of messing up any other database on the system, you can choose the version, and it's quick and easy to start and quick and easy to throw away when you're done.
details at http://www.acooke.org/cute/Postgresin0.html
tl;dr - it's a bit like a vm, but simpler, which makes it a nice fit for something like running an isolated database to play with. obviously there are many other uses too, related to virtualisation and packaging processes.
4
u/Lothrazar Apr 18 '15
the big advantages are that it's a new, completely "virgin" database
But its already a backup of the live db. where does docker affect anything
2
u/andrewcooke Apr 18 '15 edited Apr 20 '15
i don't understand what you're saying and i think you've misunderstood me.
there is no "live" database. i just want a one-off database to test some sql on. the alternative is using the db installed locally on my dev machine, which slowly gets messy with a bunch of databases / tablespaces created for random questions that i never deleted because i was in a hurry.
the equivalent in software development is using a separate vm for each project (i no longer develop projects directly on my development machine, only in vms. that way each project is separate, runs in the appropriate os version, has no confusion over installed libraries etc etc). but creating an entire vm just to run a database is a lot of work. easier to open a docker instance.
→ More replies (1)1
u/flukus Apr 20 '15
Won't you just replace that with a bunch of docker images you never delete because your in a hurry?
the equivalent in software development is using a separate vm for each project (i no longer develop projects directly on my development machine, only in vms. that way each project is separate, no confusion over installed libraries etc etc)
That sounds like you don't have proper build/source/dependency management system in place. You've come up with a complicated solution to a simple problem.
3
u/Houndie Apr 18 '15
My use case for docker is when I wanted to have a VM on a server that I had root but not bios access too, and it didn't have virtualization enabled. So, I went with virtualization-lite (docker) instead.
2
u/Tylfin Apr 18 '15
Ha this sounds like someone I work with. I started using docker to create code-jailed environments without internet access but when my co-workers found out that dockers have sudo, I created instances to let them ssh into.
8
28
u/YoungOldMan Apr 18 '15
That link wasn't all that helpful.
It started with what Docker isn't, and what it's good at, and I lost interest.
I went to the Docker homepage (https://www.docker.com) and found this answer to what docker is:
Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications. Consisting of Docker Engine, a portable, lightweight runtime and packaging tool, and Docker Hub, a cloud service for sharing applications and automating workflows, Docker enables apps to be quickly assembled from components and eliminates the friction between development, QA, and production environments. As a result, IT can ship faster and run the same app, unchanged, on laptops, data center VMs, and any cloud.
That page has more answers, more clearly, as well. IMO.
Also, go to the use cases for Docker page and you'll see that some big names are using Docker as a PaaS (Baidu and Yandex).
9
u/jewdai Apr 18 '15
sounds totally "Webscale" to me
9
u/SirClueless Apr 18 '15
Yeah, as a software developer, that page was clearly not written for me. The main problem with going to docker.com is that the real answer I need to "What is Docker?" is "It's basically just a tool for managing Linux containers (LXC)." But docker.com can't actually say that on its home page if it wants business types to pony up big bucks.
Tellingly, the "What is Docker?" page doesn't even contain the word "Linux," let alone "Linux containers."
0
Apr 18 '15
[deleted]
3
Apr 18 '15
Would you agree docket is more traditional process isolation taken another step than it is a VM?
Since there is no virtual machine involved as far as I know, it seems like referring to it as process isolation makes sense.
1
u/danielkza Apr 18 '15
You're looking it from the wrong side. Containers are much closer to "very isolated processes" than "lightweight VMs". Calling Docker a hypervisor is a misnomer, since it does not interpose any new code between the container and the host, it merely sets up isolation features provided by the host' kernel itself.
→ More replies (1)1
u/ActualContent Apr 18 '15
Your last description made the most sense out of anything I've read so far. Thanks for taking the time to write it!
1
u/tetroxid Apr 19 '15
It's written by hipsters sitting in a cafe on a mac using sublime in Ruby, bro. It's as webscale as it gets.
44
u/Darkmoth Apr 18 '15
Docker and Monads;
Two things that only make sense to those who already understand them.
13
u/eyal0 Apr 18 '15
Don't bother trying to understand monads because you probably already use moands without knowing it. Or if I had you try to solve a problem that is best solved with moands, you would re-invent a monad without even knowing it.
I still don't know Docker.
7
u/Darkmoth Apr 18 '15
Yeah, I finally got monoids under my belt thanks to this:
http://fsharpforfunandprofit.com/posts/monoids-without-tears/
...but a man's got to know his limitations.
2
2
u/multivector Apr 18 '15
What makes them seem hard is they don't really have a real world metaphor that works. With OOP you start by telling people that objects are like shapes, and some shapes are triangles and that's inheritance. But with monads you just have to use them for a bit to build up intuition.
1
3
0
8
u/JeefyPants Apr 18 '15
Good to note this article is a year old
4
u/satayboy Apr 18 '15
Yes, and parts of the article are outdated. For example, Docker does have their own container technology now that competes with LXC.
4
3
u/kkirsche Apr 18 '15
Sorry but this never actually says what Docker is and how it is different from other tools on the market.
3
u/RainbowNowOpen Apr 18 '15
The fact that any Docker image can run on any machine running Docker is amazing.
Is it? Has the author used virtualization/hypervisors before? I know Docker is not exactly those things, but the author's delight at "any X-Image can run on any machine running X-Server" seems naïve.
3
u/reneruiz Apr 18 '15
Okay so it sounds like, instead of carving out space on my local hard drive for my projects and running a dev environment off it—I would instead run a Docker image on my machine as I dev. And then I commit changes to the image as I commit. So that a server on the other end, can pull in my changes. Not unlike Capistrano.
So let's say I want to use Docker with something stupidly-simple like static HTML. How should that fit into workflow? Do you scrap git and rely on Docker commits instead? Or am I just thinking about it all wrong?
33
u/hlprmnky Apr 18 '15
You wouldn't scrap git - your HTML, CSS, and static assets you would still keep under source control, just as you do today.
Just to be sure I'm making this clear at the outset, a static HTML site isn't a particularly good use case for Docker; I guess what you'd do there is have nginx or whatever httpd in the container, but that doesn't scale super well if your organization is running more than the one site off one domain, for example. You certainly could do that, but you wouldn't get much benefit over just previewing the site in a browser locally and sending the tagged git commit information to your build system or Ops gal or what have you.
Now, a more interesting case (and a better match for Docker) arises when you need to have, say, a Rails app. Without Docker, you're doing a bunch of work locally to set up a Rails environment, download gems at a given version, run tests, etc. - then when you're done you hand off just the source commit to QA, who does all that infrastructure setup again (and goes back and forth with you a few times trying to figure out why they got the wrong version of Jinja installed, say), and then test. The source then commit goes off to be deployed into stage or production and there's a third and/or fourth time you have to set up Rails, run bundle install, and now you're in an environment where there might be multiple versions of different libraries in use, so gemsets become a requirement instead of a convenience, your code might break if the production server gets restored from backup and 'bundle install' doesn't get run for your app, etc. Capistrano is one way to mitigate this situation - put another layer of abstraction on top to weld all the parts together. Docker is the opposite approach, almost - weld everything together at a level below all this stuff so it travels together without and manual coordination.
With Docker, you still keep your application source in git, but you don't install Ruby, rails, whatever gems etc. on your dev machine. You stand up a Docker container and install all the ancillary stuff your app needs into the container, and copy your app into the container (in fact, you can even symlink your "live" git checkout into the container, edit files locally and see the changes in the running container).
When you have your app ready, you check your source into git, and you create an image of the Docker container and push it. The image has your Ruby version, your gemset, all the ancillary stuff in it - and QA can just pull the image, run it in their Docker host, and test your app. After QA is happy, Ops can also just pull and run the same image in staging and production. No version conflicts, no hassles with QA's environment, no badness when someone working to put out a production fire runs a migration on your app with the wrong rvm selected.
It's not a silver bullet for every lifecycle problem, but it is pretty damn nice once it's set up.
5
u/Darkmoth Apr 18 '15
Wow, thank you. This is an excellent description of the use case. I now see how valuable it would be.
edit: The funny thing was I already do this with Virtualbox VMs (on windows), and it has been revolutionary for our workflow. Maybe Docker will keep me from having to ship 2gb VHDs around.
2
u/0xE2 Apr 18 '15
Maybe Docker will keep me from having to ship 2gb VHDs around.
At the very least, absolutely.
1
1
u/GraceGallis Apr 22 '15
I've heard a little bit about Docker from one of the Web app devs at my company, but I work on embedded systems. What would Docker bring to the table for embedded systems? We typically have a closed environment via emulators for software-in-loop testing as it is, to mimic our closed hardware environment.
2
u/hlprmnky Apr 22 '15
I'm by no means an expert, just a developer with some experience, but I am not sure that Docker would do very much for you at all in the embedded space.
Docker is, as I understand it, using LXC containers (by default, I know it supports other formats but don't know much about them) to "freeze" a Linux process state into an image, which images can then be composed to build the state of the container.
Unless you're in a position to run a kernel with LXC support on your target device, I'm not sure how you would leverage this to give you anything you don't already get from your simulation environment.
0
5
u/coderjewel Apr 18 '15
Docker is more like a virtual machine that you use as your dev environment; you run your database, app server, message queue, whatever you need for your dev environment inside of it. So if you want to start coding on a different machine, you just copy the Docker config, and you'll have your same dev environment on that machine. This would be especially useful on non-nix environments like Windows. Although I guess that Vagrant is more closer to my description than Docker.
1
u/AwfulAltIsAwful Apr 18 '15
This is exactly what my .net team recently began doing with VirtualBox and .vdi images. It's a great way to develop because you can set rollback points and then really sandbox all facets of the application from config files to registry entries to hardware drivers. If you fuck everything up you can simply rollback to the previous state from the host OS. It also makes sharing Dev environments/scenarios with the rest of the team a breeze.
1
u/theonlycosmonaut Apr 18 '15
I think that you shouldn't be using Docker to version control your source, but to version control your deployment. You might make a bunch of changes to your site (in a branch maybe) per deployment, which is when you rebuild the Docker image (adding more 'commits' to it) and deploy it. You could also, for example, roll your deployment back to a previous deployment.
However I think that's not the main focus of how Docker should be used. The main benefit for me is simply that I can easily run the same containers in dev and production.
1
u/mykevelli Apr 18 '15
Forgive me if this is a stupid question but does Docker make and sense in a Windows/.NET environment? I've looked into Docker before but still don't feel confident that I understand the answer to this question.
It seemed to me that it is a heavily *nix based technology and although there is (will be?) support for Windows it essentially does so my running an internal Linux VM environment. If that's the case, that seems to defeat the whole light-weight aspect as well as making it very difficult to monitor if it goes to production.
Can someone clarify for me?
2
Apr 18 '15
AFAIK they've made rumblings about it. There are already things in the windows world that do what docker does (not at the OS level but at the application level - AFAIK there is no windows equivalent to Linux containers). Those software packages are not cheap.
2
u/Novex Apr 18 '15
At the moment it's entirely Linux based, so probably not all that useful for .net. You're right - as soon as you need to run a Linux vm, "light weight" goes out the window.
That being said, Microsoft is developing containerization in the next windows server and it's meant to be compatible with docker. The docker philosophy of having one self-contained unit for an application+dependencies+server configuration will be a lot more useful then.
0
u/whoopdedo Apr 18 '15
An OS running on bare metal is like a single-tasking program. Virtualization is multitasking for OSes. A container is OS multithreading. The relative benefits and drawbacks are the same.
0
0
202
u/BadGoyWithAGun Apr 18 '15
Thanks for the giant registration popup that darkens the entire page. It really reassured me that this isn't an article worth reading and prevented me from wasting any more time.