Laura Frank – “Containerized Applications with Docker“ | .concat() 2015

hey thank you everybody good afternoon I hope you’re not too sleepy I promise if you would make it through this there will be coffee so my name is Laura I’m from Chicago we traveled quite a long way to get here but it is a wonderful lovely city I’ve never been here before so I’m enjoying my time greatly my friends are all excited because the Sound of Music was shot in Salzburg but it seems no one cares but me so I have been singing to myself this whole time but maybe other people not not so important American movies you can find me on the internet various places my name as we say in English is Rhine wine they’re very pretty sending I used to live in Bonn so that is where my name comes from today I’m gonna talk to you about containerization and what even is it because people are kind of slinging this word around and maybe not using it correctly or maybe having multiple meanings for it I’m gonna hopefully clear that up for you a little bit and then we’ll get into actually using containers in your project you might have heard of containerization or containers before and if you have you’ve probably heard about them in context of this giant blue whale called docker one thing I want to clear up before we get started is that docker is not a container docker does not equal containers docker is a tool containers are the thing that will actually manipulate so docker does two things it manages the code that goes inside of them and then it also executes sand runs the code inside of them so there’s kind of the management and execution part to docker but again it is a tool it’s not a container itself so before we actually talk about docker and how you might use it let’s understand what containers are first and then we can integrate them into some of our projects so a container is a virtualization layer a container is kind of like a virtual machine it’s a self-contained execution environment so if you think about a server in terms of physicality it’s probably maybe a big data center it could be a laptop like this one but there’s some physical hardware though and then on top of that is a virtual layer so not a physical layer and that’s where containers live a container much like a virtual machine can share the kernel of the host system it’s on it’s isolated from other containers so if you have something running in one container can’t touch stuff going on in another container unless you explicitly give it permission and another great thing about containers which we’ll talk about a little bit more is that they have very fast boot times and very low overhead so you could say that they’re very cheap in terms of time and space in containers can work in conjunction or in place of traditional virtual machines that you might already use in your project for example if you use rails you might have your rails application running on one virtual machine and your database running in a different virtual machine you can replace virtual machine with container you might have one virtual machine running two containers so they can work integrated with each other or you can use one or the other and again it is just a virtualization layer people think containers are so scary because they’re kind of low level systems things but here to tell you and persuade you that they’re not really that scary they’re not really that hard to use they’re kind of like a virtual machine however they do have some fundamental differences which many people find super appealing and that’s a big reason why we use containers so let’s look at actually physically and virtually what these differences are so this is a kind of schematic diagram of what a traditional application that has a web component in a database might look like if it’s on a virtual machine so we have hardware which is maybe a data center and then the host OS on that hardware and a hypervisor which is managing and allocating resources for a virtual machine and these light blue blobs at the top if you can imagine each column as a separate virtual machine you’ll see we have three of them running and then the two labeled web that’s our web application so we could say that’s rails we have two instances of those running a little bit of redundancy there’s probably a load balancer somewhere I just didn’t put it on this slide and then over on the other side we have our database running and you notice each of these kind of has its own little stack and call them they have a guest OS and libraries and no one is sharing or learning any of those and you any of those things we learned in kindergarten about being uh good friends with one another but if we introduce containers we can actually get rid of a lot of the junk that virtual machines force us to have namely the hypervisor and then the guest OS on each of the machine so we’ll get rid of that and in its place we can introduce something called a container runtime engine so this is a level where docker comes in so the container runtime engine is basically your point of contact between you deploying your applications and then

the kernel or the host system you notice on top we have still web web database still three applications running separately in their own little containers but one big difference is that the libraries are shared now so instead of having to duplicate copies of libraries needed for our web component they can just share the same one I think it’s silly to use the same thing over again if it’s just identical so with docker and with containers you can give permissions to let processes that are isolated still share the same resources if you want them to so that’s what’s happening here and of course this is not to scale in fact this environment will boot up in probably a very small fraction of the time if you’re using traditional traditional virtual machines so one of the drawbacks and maybe some criticisms of containers is that they are slightly more complex that little diagram doesn’t come for free there some tooling and orchestration around it but it doesn’t reduce the amount of time and space that you need to run the same application in a container versus a virtual machine there’s a couple big benefits of containerization really quickly fast cheap because you’re using less resources it boots faster less resources means less money means less people maintaining Hardware always great portable is really exciting about containers you have a pretty standard kind of easy to duplicate testing environment everything that is in your container is run from one image so if you run it on a production server on your own laptop in a development environment it’s all the same it’s going to be the same all the time you can give it to someone else in your organization you can put it on github people can pull it down will be absolutely identical there’s no manual configuration really as long as you can at least give instructions and automate some of the stuff in that in the container the last thing that’s really great about containers is that they are safe the worst thing that will ever happen because the containers are so isolated in terms of networking in terms of processes if something goes terribly wrong the worst thing you’ll have to do is reboot your container it will not hose your entire environment you will not have to reboot a virtual machine you’ll not have to reboot your computer you might just have to say docker remove this thing and boot it up again in a matter of milliseconds so containers are super great but it doesn’t do us much good if we can’t practically use them in an application in order to do this of course we need to install docker um there are other things you can use there’s Linux containers there’s Rocket docker I I prefer because it wraps a nice front-end experience on this whole containerization process to install docker you need a couple things you need a computer so that can be your laptop that can be virtual something in a data center etc you need application code so whatever application you’re trying to run inside a container that needs to be done tested and will package it up I’ll show you how to do that in just a second also I find coffee is very helpful this can be a little frustrating and it might take a little bit longer than you might hope the first time so grab some coffee alright so a computer you need a computer to put code on of course this is something that we all know if you’re using Linux already super great you can just install the official packages you really don’t have to do anything docker was first developed with Linux containers Aleksey docker runs super natively on Linux it’s kind of mint it’s kind of it’s it’s its home and it feels nice and warm and fuzzy on in a Linux environment so you can just download official packages and it’s not a big problem at all but if you’re using anything else specifically if you’re using a Mac you need to have some kind of virtual machine running I know I just said virtual machines are gross and big and bloated but the point here is that you can run lots and lots dozens of containers on one virtual machine versus running one application one virtual machine machine so the density is much bigger having and VM on your laptop is not going to totally ruin the performance and ruin your development experience docker has this thing called boot to docker which is super easy it actually links all your folders and does some happy configuration for you behind the scenes so it almost feels like you’re using docker natively when I demo for you in just a bit I’m gonna be using boot to docker don’t be fooled I’m not using docker on my Mac I am actually using a virtual machine it’s running Linux it looks like I’m using it on my Mac but totally not so don’t be fooled so installing docker is you know it might take you a little bit but once you have it up and running you can use docker for two things again you can use it to manage code to go inside of your container and then once we have kind of the application code packaged up or managed then we can think about running the application code so the first thing to know about containers is that every container is based on an image if you think of an image is the class in a container as an instantiation of that class that’s a good analogy to kind of help yourself understand the relationship between image and container you can’t have a container unless you

have an image the application code that you’re gonna run needs to be packaged into an image in order to be run inside of a container just like you would package it up to deploy it inside a virtual machine kind of the same the same process here every image is controlled by a thing called a docker file a docker file is just a set of instructions that tells docker how to package your code for you there’s two ways that you can get these images one is actually you don’t have to interact with the docker file directly you’ll interact with something called the docker hub which is basically like github for docker images so you can interact with it push pull docker pull docker push etc kind of just like you would with github it’s very similar feeling additionally one tip if you just want to skip the poll if you do docker run docker will try to find the image that you’re trying to run on the hub first and if it can’t find it it will tell you so you could simply say docker run rails it will go to the public hub try to find the image pull it down and run it in a container for you you can additionally build it from your own docker file you can say docker build – t4 tag fubar that’s the name of the image food would be my github username and then bar would be the name of my image and then the directory that has the dockerfile usually it’s the one that you’re in so you just say period so docker has this thing called a registry which is what we’re calling docker hub docker registry kind of they’re used interchangeably that’s where images live and you can pull them down and interact with them directly you can also build your own images and in fact the hub and the community part is what people love about docker so much it gets a gold star people love it because you don’t have to write your own code if you don’t feel like it if you want to run WordPress and Postgres together you just pull them down get them to talk to each other you don’t have to code anything it’s a really easy way to get started with docker especially if you’re using boot to docker and this is what the registry looks like it’s actually pretty similar to github you can integrate github and your docker hub account notice that these official repositories are there so this is what I’m saying you don’t have to code it all if you just want to pull down to use in your project just say docker run you can just use it you don’t have to do anything you can pull down note all of these things are already pre managed pre made into images for you you don’t have to do anything so it’s a really nice easy way to use containerized components within your application so again this is the public registry you’ll find two types of images services is kind of what we have seen node these are services that are prepackaged be careful to look out for these project-based images these are meant for active development which we’re not really going to talk about today I’m happy to talk about afterward but it’s a little bit more in-depth and I figured we just covered the basics to get you up and running with docker today there are two different kinds so just make sure you’re pulling down one that’s already ready for you to to run in a container it will say so in the documentation and again those are official images they are maintained by other people so if you are lazy like I am it is great I cannot tell you how wonderful it is to not have to worry about my sequel dependency I can just pull it down directly most of these repositories have excellent documentation so if you want to bootstrap something it has instructions for bootstrapping already in the documentation and again they can be base images which are images that you’ll use actively in development or actually run services that’s kind of what we’re looking for when we’re first starting out we want things that are gonna just you know we can say docker run and our service is gonna start additionally if you work at a company I used to work for HP we had a lot of locks on our code we didn’t want people to see it you can have a private registry just know that this is another option for you you don’t have to rely on the docker public hub you can also host one yourself if you have kind of a proprietary information or if you just don’t want to expose your learning curve to the world sometimes we don’t want to do that you can find instructions for running your own docker registry on the public docker registry so you can pull down a registry from the registry to run your registry which is really straightforward and probably really simple right so again the registry is the basis that’s where your images are managed so either publicly or privately you can pull down images that are pre-made and interact with it just like the docker hub it’s great because you don’t have to write your own code here’s a little visual example of what it might look like to run an application where you pull things down from the docker public hub I can say docker run WordPress and docker run my sequel I do some magic which I’ll get to later to get WordPress to know that it has a dependency and suddenly I have a WordPress installation and I didn’t have to write anything these are running

again either on a virtual machine like butta docker or any other core OS Linux is a few other operating systems that support docker this could just be running on your own Linux like bare metal server if you if you have that additionally you can also package your own code and run them in containers so all of that the example before was pulling it down from the docker hub right now I’m going to show you the way that if you have your own rails application you have your own rail or application that you want to run in a container you can be that image instead of pulling it down and then run it on your own system or distribute it somewhere else to be run and that’s just one extra step instead of just saying docker run we’re gonna docker build t4 tag my image the image name and a period then we’ll run it again with a little bit of magic link it magically to our to our database and then run that as well so this is the same exact architecture and the same kind of setup as before when we were pulling it down directly from the public hub except in this example we’ve built the image ourselves so this is actually taking code from our own computer when it’s ready for packaging and ready kind of for the world to consume you could package it up into an image and then run the container again from that image so let’s remember that images are built for docker built from docker files really quickly before we build our image together and I’m gonna do a little bit of live coding which is definitely not stressful at all I just want to show you and explain to you a little bit about what you might see in a docker file I’ll admit that docker does come kind of with a pretty steep learning curve and I’m sorry if this talk seems very front loaded with me babbling to you before I get to the good demo part but I feel like I would be doing you a disservice if I didn’t cover these kind of important bits and just let you loose to wreak havoc on containers it wouldn’t really end happily for either of us I don’t think so this is a docker file this is a very short one this runs a Sinatra application there’s a couple parts in here again that it’s run top to bottom not choose-your-own-adventure so thanks Ramon for that tip from is a base image so in this case I’m basing it on a ruby image CenturyLink is what I work for I work for CenturyLink labs so we’ve made a bunch of these base images we have a couple Ruby ones so you can just pick and choose whatever Ruby you want the next bits are the important parts so I’m running a command with run I’m gonna make a directory and then with add then with add I’m going to add everything from the directory I’m current in into the directory that I just made so I’m taking code from my computer and putting it inside the image that will later go in a container this is sort of how things get moved from your local environment into a container eventually note this is static once I do this I can’t change the code it’s not great for development it’s great for deployment though and also for dependencies so from there now that I have my code I’m gonna run a bundle install working directory just sets the context that says hey run bundle install in this directory that I just put my docker or my gem file in so I’ll run a bundle install and then I’m actually gonna start the container with this command Ruby hello world Darby and that’s it this is pretty much the barest bone simplest docker container I’m actually gonna run it for you right now and hopefully it will demystify some of the concerns about okay is that too maybe too big my aspect ratio weird here I want it I hopefully you guys in the back can still see but so I’m gonna just check to see what docker things I already have going on again I’m using booted docker this is totally not this is not my Mac this is a virtual machine sorry if this is kind of jumbled I want to make sure the people in the back can see you can see I have two images here created hello world and then there’s one the Ruby base so the Ruby base we saw is our base image that from command and then hello world was from my previous build I can build it now again let’s call it hello cat and a period to signify that’s exactly I’m in the directory so this is just gonna go through right now we’re just in the bundle install step that we just saw so this might take just a second to finish be more than just a second alright well

um here we go okay cool so we can see that it successfully built this image and it has this ID if I say docker images again I can see that I just created this image here and then to run will say docker run this is the magic I talked about we have to do some port binding because of course we need to let it know that it’s running on a port and we need to have some communications between the container and then the host port being the virtual machine I’m gonna choose four five six seven and then I’m just gonna tell it the name of the image that I want to run in a container I will press enter you can see that Sinatra has started and then again since I’m on a virtual machine this is my host my docker host for my boot to docker machine oh oh did I have a typo oh for here thank you five okay there we go okay cool so really that happened in like not even a second I don’t even think it took like fractions it’s just so fast so imagine how many times it’s so great because imagine how many times your environment you do something dumb and like it breaks and you have to like restart everything for me that happens a lot because I make a lot of stupid mistakes as you should so instead of having to take five minutes to restart my virtual machine reconfigure everything literally in less than a second I could have my environment back up and running so if anything hopefully this will ease some pain in your development process huh so quickly kind of just back to you a couple things so we saw how a container works again the management part deals with registries the execution part let me just back up one executing and running code is the docker CLI that you just saw me interact just as a reminder this is where docker is living its living kind of in between you and the hardware so that is your point of contact and in fact you’ll probably just interact with docker from the CLI like you saw me doing there’s really only a couple commands that you need to know to get started docker run there’s a couple flags like – peace on my port binding there’s environment variables that you can pass in you can docker pull again this is interacting with the registry docker images you saw me use that’s to list all of the images I have downloaded and then finally docker PS which is all the processes that are running if this seems like a lot of work and typing on the command line is not your favorite thing that’s great because I work on an open source project that helps with application template Inge so to conclude I’m going to kind of talk to you a little bit about the web interfaces and other tools you can use along with docker if you’re trying to kind of get started quickly or you just want to poke around in the containerization world application template is pretty much exactly what it sounds like you can make a template for your application you can specify all of your configure configuration options beforehand and instead of having to run each container by itself you just run your application and it will automate all of the processes of starting up configuring port binding environment variables etc of your containers for you and you’ll just end up with a nice happy application there’s a couple available for you docker has one called docker compose that used to be called fig they kind of bought fig and absorbed it and turned it around and now it’s called docker compose which is great but I like to fig about her because it is shorter so it is text only so if you’re not super comfortable with the command line or super comfortable and yeah Mille maybe not a great first choice for you it does kind of work the same way as other tools you put everything in the ammo file and then you run your application with docker compose up you can check out it on github dock github.com slash docker slash compose has all the documentation and all that good stuff there for you lastly Panamax is the tool that I work on it’s an open source project it’s a Ruby rails go lots of JavaScript and angular lots of cool stuff if you feel like contributing to something it’s very fun in the core contributors I’ve heard are very lovely people so it is very similar to duck or composed we did that on purpose you can kind of go back and forth if you start with docker compose and you want to switch to Panamax or if you start on Panamax and you want to switch duck compose totally fine we made it to be pretty compatible the biggest thing is that it has a UI so you can instead of just typing everything you can drag-and-drop and press buttons and kind of illustrate it it’ll illustrate

for you the architecture of your application and you can manage it in a in a much easier visual way it also supports remote deployments if you want to deploy to Amazon or Google or whatever your digitalocean whatever your choices you can actually deploy your containerized application directly to it just by pressing a button you don’t have to do any any magic other than just get a token you can check that out a Panamax to IO slash got Panamax here is a sample application you can see it’s pretty standard yeah mol we have some images we can specify links some ports description etc but do it once and that’s all you have to do quickly this is what the UI looks like so this search goes directly to the docker public registry you can also integrate it with private registries so even if you have already a docker workflow setup or you don’t want to bother writing your own code you can use Panamax to interact just directly with the docker public registry and then you don’t really have to type anything you can kind of just click around and get a containerized application up and running pretty quickly lastly some resources on your containerized journey i do have a tutorial up on my github account it’s just this hello world container demo that I did for you I have a couple different ways of implementing it and there’s some different strategies and styles of working with containers there’s a lot of documentation and all the source code is in that repository check out the docker hub again boot to docker is really great if you’re just getting started Panamax and then of course rails girls Summer of Code is super great and you should all consider donating or just tweeting and getting the word out so more participants can be sponsored and work on cool open-source projects that’s all I’ll be here through the end of the day so feel free to ask me any questions if you see me drinking coffee by myself