11 ms·
Learn Docker by building a Microservice
- Cyph0n 10y agoExcellent tutorial! I'm a newcomer to Docker, and this was quite helpful. I'm currently using Vagrant to run my local dev environment and it's been OK. But I can see how using containers to split components up can make testing much easier. One question I have though is how useful is Docker for deploying production applications? Firstly, can plain Docker be used for the whole setup or is something like Kubernetes required to tie the containers together? Secondly, is there a performance hit when you use multiple Docker containers on the same server? Or should it be a one-to-one relation i.e. one container per as server? If so, why not just use plain servers with Chef or Puppet for server management? These questions are assuming that I'm using my own servers - something like DigitalOcean or AWS. I'm not a huge fan of managed container services; it feels like there's too much "magic" going on behind the scenes.
- jon-wood 10y agoThere are a few options for using Docker in production. At the simplest (in terms of least infrastructure at least) you could just have a VM somewhere running Docker, and use docker-compose or similar to bring everything up as you would in development. In the middle you have Amazon Elastic Beanstalk which can take a Docker image and deploy it with minimal hassle, and at the other end of the spectrum you have Kubernetes/Mesosphere/Amazon ECS to allow distributing containers across a cluster of machines. Except for isolation there's no particular need to run one container per server - while there is a bit of overhead to running in containers my experience is that its offset by being able to run multiple services which don't require a lot of resources alongside each other on one server.
- nzoschke 10y agoI'm working on an OSS project, Convox, that makes a production AWS / VPC / ECS / Docker system trivial. http://github.com/convox/rack http://github.com/convox/rack It's similar to elastic beanstalk in that it gives you a great pre-configured AWS architecture. But it's more transparent and configurable. It also comes with a nice Docker based development environment that mimics production for dev / prod parity. We have hundreds of production clusters and some at really large scale (300+ containers) so I can confidently say that Docker in production is largely solved on AWS.
- sidcool 10y agoI am glad it helped. I am not the creator of the article, but found it worthy to be shared :)
- cromantin 10y agoI highly recommend looking at rancher. It's great in every way.
- TimMeade 10y agoThanks for this tip. Rancher looks well worth further investigation.
- cromantin 10y agoIn my anecdotal experience with 10 machines and 40 services, half of machines dedicated and half in amazon - i've tried: k8s - great but for >>10 machines and you need a devops to watch and configure it + every configuration example on site is broken in one way or another. skydns is working for dns. has it's own overlay network. nomad - great, but doesn't manage dns or network + i didn't get how to save it to file, only upload to nomad. What if it fails? Where cluster config is stored? May be it should be used with Terraform, but i did't like it. Thou for it's purposes it working good. Has concept of long running tasks and can run VM's helios - i don't remember exactly why but i didn't like it. Something with configuration management. No network or dns. maestro-ng - this is as raw as it gets. Basically docker compose. I was using it for a while, but it hit it's limits pretty fast. No network or dns. rancher - after seeing it in action i fell in love. Beautiful UI (you can even view logs and start shell from web ui, and it works great!). Has DNS, manages your overlay network. Store configuration in mysql (i use amazon's rds with MZ). It has it's own perfectly capable scheduler - Cattle. And it can run k8s and mesos if you want it to. I highly recommend OpenVPN package that will connect you to internal overlay network and you will be able to work with services walled from outside world.
- icebraining 10y agoLast time I tried RancherOS is seemed quite heavy, with a 2GB RAM footprint for the initial run (without any nodes attached). What's your experience?
- 10y ago
- deleted 10y ago[deleted]
- haffla 10y agoFor me one of the great things about Docker is that there doesn't necessarily have to be a great difference between dev and production. Both in dev and prod we simply use docker-compose up. Of course with slightly different docker-compose.yml files and Dockerfiles, no HaProxy in dev, no mailcatcher in prod (It's all so wonderfully plugable). Admittedly, I cannot speak for high scale projects with thousands of containers across a cluster of nodes. I guess there you need to have some more elaborate deployment scheme.
- zwischenzug 10y agoI wrote a docker-101-tutorial that's interactive to allow people to get Docker 'under their fingers': http://ianmiell.github.io/docker-101-tutorial/ http://ianmiell.github.io/docker-101-tutorial/
- alistproducer2 10y agoI've been using docker for a couple months but I still learned some new stuff. Awesome tutorial! Docker should be required learning in any CS program. I cannot fathom why anyone would setup environments any other way.
- andrewchambers 10y agoDocker isn't the only way, or always even a good way to do things. A better requirement would be to teach people how to write software that isn't a giant mess of dependencies that can't be recreated without the additional complexity of docker.
- alistproducer2 10y agoI admit my wording was not as good as it could have been. I also agree that good engineering fundamental should be taught; however, Docker is a tool like any other. It can be used for both good and evil.
- pawadu 10y agoDo you think CS programs should teach someframework.js too? Where is the Science in using stuff other people have put together (and then written a detailed instruction on how to use it)?
- eggman 10y agoare there different ways to put boards together so that they all run *nix? or are intel, asus, msi and gigabyte all doing the same thing?
- alistproducer2 10y agoSerious question: have you been through a CS program? In addition to the mathematical and theoretical underpinnings of the discipline, a large part of what is taught is how to engineer software. IMO Docker is a major step forward in software engineering, this is why I believe it should be taught.
- krat0sprakhar 10y agoAwesome tutorial! Love the deep dive into building the entire service and using compose to get it running. If you're interested in how to deploy your docker apps online, I wrote a tutorial which some of you might find useful - http://prakhar.me/docker-curriculum/ http://prakhar.me/docker-curriculum/
- koolba 10y ago> What you are seeing is the bash shell of an isolated container running Ubuntu, on your machine. It's yours to place with - you can install things on it, run software, whatever you want. Nitpicking a bit on security, but root in a container is still root on the host right?
- cmrust 10y agoI don't believe so.
- falcolas 10y agoYes. You're depending on the isolation provided by the cgroups to keep root inside it's cage. It's one reason there was a problem a year or so ago with root users inside a container being able to access the kernel keyring for processes outside the container.
- davexunit 10y agocgroups do not provide isolation, namespaces and chroot do.
- davexunit 10y agoWithout using a user namespace, yes. Does Docker create a user namespace by default now? If not, that is pretty terrible.
- jasonbenne 10y agoI am curious to know the definitive answer to this as well. If root in container == host root can someone provide the steps for breaking out?
- zenlikethat 10y agoIIRC, there are no known breakouts with the latest version of Docker and the default settings are oriented towards preventing them where possible. The concern is largely around having privileges escalated by default if a breakout were attained.
- neo1691 10y agoThis is a pretty amazing tutorial on docker. I was always confused with the difference between docker and virtual machines. This makes more sense now.
- pawadu 10y agoAre there any similar tutorials for LXD?
- benmarks 10y agoIs there some standard architecture illustration tool for Docker? The graphic from this post is quite similar to the one from last October at http://www.teckstory.com/hadoop-infrastructure/docker-tutorial-for-beginners/ http://www.teckstory.com/hadoop-infrastructure/docker-tutori... - http://www.teckstory.com/wp-content/uploads/2015/10/Docker-Architecture.png http://www.teckstory.com/wp-content/uploads/2015/10/Docker-A...
- n_coats 10y agoGreat post! For those learning, make sure you look into removing unused images and containers otherwise you might run into some issues a little farther down the road (eventually our builds started failing). You can run docker rm and docker rmi to remove specific containers and images respectively, but we also found passing the --rm flag with docker run helped a lot as it removes a container after it exits. Happy learning!
- ThunderGorilla 10y agoWhy did this cause the builds to fail?
- roxmon 10y agoI run into this pretty frequently with Docker on Mac OS X. Passing the `--rm` flag helps, but for cleaning up old/unused containers, I usually set these two commands on a cron: # Remove un-tagged or un-named images for im in $(docker images | awk '/<none>/ { print $3 }'); do docker rmi $im; done # Remove stopped containers docker rm $(docker ps -a -q)
- xahrepap 10y agoYou can also do this: docker rmi $(docker images -q -f dangling=true You might consider doing the same with volumes: docker volume rm $(docker volume ls -q -f dangling=true) But be careful with the volume one. I'd read the documentation around volumes first and see if you're deleting what you want to. :)
- mdk754 10y ago> We might not want to run our production database in Docker (perhaps we'll just use Amazon RDS for example), but we can spin up a clean MySQL database in no time as a Docker container for development - leaving our development machine clean and keeping everything we do controlled and repeatable. Curious why you wouldn't want to run it in docker in production? Wouldn't running the same container in dev/prod be helpful?
- yacn 10y agoAFAIK (last I looked into this was ~6ish months ago), scaling databases in docker containers is a hard problem that currently doesn't have a clean solution. Vitess[0] is a solution, but it didn't seem quite ready for production last I looked at it. [0] http://vitess.io/ http://vitess.io/
- Veratyr 10y agoFrom the Vitess overview page: "Vitess has been serving all YouTube database traffic since 2011". Given that, it seems production ready.
- tayo42 10y agoWhat is there to gain by putting your database in a container? A database probably has its own dedicated server so, your probably not deploying databases often and there's the added complexity of making your data persistent.
- mdk754 10y agoYour point stands, and obviously makes more sense. My curiosity was more in whether there were performance (or other) bottlenecks which made it technically infeasible.
- imaginology 10y agoI run my production database in a container, even if it has its own dedicated server. It's nice being able to install, start and initialize the database with a simple docker command or two to get a consistent build, instead of manually setting it up (copying/editing configuration files, deploying schemas, initializing data, setting permissions, etc.) And it makes upgrading the database very easy. Having this process automated and in source control is great. I also have the awesome ability to bundle cron in my database container to schedule regular backups. Persisting data is as simple as mounting a data volume.
- josh_carterPDX 10y agoSo cool. I knew a bit about Docker, but after running through this tutorial I'm already licking my chops at what else I can learn. More please! :)
- WestCoastJustin 10y agoThis is a very good tutorial! Love the end-to-end examples. Great teaching method as it gives an overview of what you want to achieve without looking at things in isolation. Wanted to add one bit of info and give a heads up about the Docker for Mac & Windows beta at https://beta.docker.com/ https://beta.docker.com/. > If you are on Mac or Windows, consider using a Virtual Machine. I use Parallels on Mac OS X to run an Ubuntu machine for most development activities. Being able to take snapshots, break things and then revert back is very handy when experimenting. The beta will make is super easy to use Docker on Mac & Windows without running a heavy Virtual Machine. Here's a little video about it @ https://blog.docker.com/2016/03/docker-for-mac-windows-beta/ https://blog.docker.com/2016/03/docker-for-mac-windows-beta/. Basically, it feels like running Docker natively, has upgrade features build it, so you can choose to always be on the latest release (super nice). I've been using it for a few months and it is awesome to just type "docker run" on my macbook and have it work seamlessly (vs using third party virtual machine software and env bits). The idea is that you can develop on Mac & Windows using the same images you will eventually deploy. It just works (although I'm biased). Disclaimer: Work for Docker.
- 0942v8653 10y agoWhen signing up for the beta, there is a required Company field; what do I put there if I am a student?
- WestCoastJustin 10y agoI'd just put "N/A" or "Student". I'll check about this internally too. Thanks for the heads up.
- GordonS 10y agoas long as you are Windows 10 :/ Are we going to see Windows 7 support?
- WestCoastJustin 10y agoSorry, I'm mostly up on the Mac beta (so I'm not a Windows expert), but did a little digging through the beta docs and forums and found a thread about it. > Unfortunately, Docker for Windows will only run on new versions of Windows 10. This is because Docker for Windows uses Windows Hyper-V and while older Windows versions have Hyper-V, their Hyper-V implementations lack features critical for Docker for Windows to work. [0] I know this isn't what you want to hear but that is the current status. [0] https://forums.docker.com/t/install-client-on-windows-7/13400 https://forums.docker.com/t/install-client-on-windows-7/1340...
- olau 10y agoDoes anyway know if there's an easy to encrypt a docker volume in a dev environment? So basically, I'd start a docker database container, it'd ask for a password and then decrypt and start the container and the database. When I shut down the machine and leave the office, I can then be sure the data in the container isn't readily available if someone breaks in a take the computer.
- Annatar 10y agoWow, I read that article and it just made me cringe every step of the way. What could have been: # imgadm install image-uuid # vmadm create -f vm.json [1][2] ...instead is a whole bunch of hacking on a whole bunch of things... One would think that with as much as SmartOS has been discussed here on Hacker News, people would finally realize there is something simpler out there without all this pain, but apparently not. Crazy! [1] https://wiki.smartos.org/display/DOC/Managing+Images#ManagingImages-ImportingImagesLocally https://wiki.smartos.org/display/DOC/Managing+Images#Managin... [2] https://wiki.smartos.org/display/DOC/Using+vmadm+to+manage+virtual+machines#Usingvmadmtomanagevirtualmachines-CreatinganewVM https://wiki.smartos.org/display/DOC/Using+vmadm+to+manage+v...
- icebraining 10y agoWhich parts of the article do those two commands replace?
- Annatar 10y agoPretty much all of them, including the database setup.
- icebraining 10y agoClearly those two commands can't by themselves know how to set up a database; I'm assuming it's in the JSON file, but then not showing it is somewhat misleading. It would certainly not look as clean if you were to include a big "echo ... > vm.json". Also, the database setup is only the first part, it then goes on to show container linking and etc.
- Annatar 10y agoWith SmartOS, the database image can be preconfigured and published into the catalog, so when the image is pulled down with imgadm(1M), the database is already ready to take data and serve. The complex startup he then continues to hack on is superfluous on SmartOS, as SmartOS has the service management facility for that. For brand new services, one XML file and svcadm import later, the thing is ready to be turned into an image, never to have to be revisited again. I do not understand why people take the harder way to do the same thing (if there is no clear advantage to doing it the harder way). He picked MySQL, a database which is infamous for data corruption and bugs where basic SQL functionality does not work; the substrate, Ubuntu, provides no fault management architecture, no end-to-end data integrity, no post mortem kernel debugging and dynamic tracing facilities, which will eventually result in incidents in the dead of the night. Then there is the choice of Docker, which is really not designed to run a fully functional virtual server, but a single process in a single image (although it can run something akin to a virtual server, with lots and lots of extra system engineering put in). Why choose Docker, when with zones on SmartOS one can have a fully isolated, fully functional UNIX virtual server, but running at the speed of bare metal, and all of that in a single invocation of vmadm -f vm_definition.json (or use the SDC software, which puts a GUI on top of it all)? If something more reliable and easier is available, why insist on doing it the harder way with an unreliable substrate? There appears to be no logic here, and if there is any, it escapes me. Is it lack of awareness of SmartOS, FMA, SMF, ZFS, and zones, or something else?
- azimuth11 10y agoNice tutorial! For those following along, I've noticed a few errors/typos, and maybe others in the thread caught them... * Container ports are exposed like so, HOST_PORT:CONTAINER_PORT, or YOUR_MACHINE:DOCKER_VM. If you already have a mysqld instance running, then try using 3308:3306 * The /search route needs a "next" parameter in users.js: "app.get('/search', (req, res, next) => {"