Docker! That’s a real buzzword today! If you haven’t had a chance to use it yet, then you probably wonder what problems does it solve and if it can come in handy for you? Have you ever struggled with the problem of installing new software just for trying something out? Doing this regularly can lead to chaos in your OS not to mention the extra hassle of configuring more complex or coupled software (Wordpress with LAMP). Maybe you are the one who wants to create easy to deploy software? Among others, these are the reasons to consider Docker. In this post, I’ll give a quick introduction to this tool so you can get a gist of an idea in one evening. Let’s get started!
What is Docker? It is so popular that you probably know more or less what it is - anyway - according to documentation Docker is a platform for developing, shipping and running applications. So what’s the deal? Isolation! Docker uses features like cgroups, namespaces, capabilities and other Linux kernel stuff to lock down your process in a “software cage” by limiting resources like CPU, memory as well as ability to see other things in the system like processes or files. If you’re interested go check those mechanisms, but I think you can imagine how Docker might work under the hood using them.
Before we go further be sure to have Docker installed - the installation process is fairly simple, and you can follow the instructions for Ubuntu here.
Image vs container
The two most important things in Docker’s world are image and container. You can think of an image as some kind of ISO image or CD with your favorite system installation. In particular, Docker image contains all things that are required to create a running thing - container. A container is much like a living thing inside your OS, process, or something. You can start it, stop it, change it, save it.
Hands-on - cowsay on Ubuntu
I’ll show you a quick thing you can do with Docker - install random software and check it out. In our example, it’ll be a popular cowsay in the Ubuntu environment. Let’s get started with running Ubuntu container - remember you’re not installing a virtual machine with Ubuntu, but creating an “isolated box” with the environment just like ubuntu (libraries, tools, binaries, whatever, etc.).
docker run -t -i ubuntu
It’ll take a while at the beginning - why? Docker checks if you have already downloaded Ubuntu images and if you have not then it downloads the image from the repository. If you happen to run ubuntu again later, then it’ll be much quicker.
Anyway, you’ll end up in a prompt within your new ubuntu environment, you can fool around, install something - let’s install cowsay! Execute this command from within your container:
apt-get update
apt-get install cowsay
/usr/games/cowsay 'I like Nerdy Geek!'
Don’t leave your container just yet - open up a new terminal and execute:
docker ps
You’ll see your container running (see a funny name generated for it? 😄 Mine was ‘gigantic-panini’). This command gives you a list of all running containers.
You can now hit Ctrl+C to leave your container. Actually, you’ve just stopped it - execute ps command once again - it’s not there. You can fix it by adding –all option
docker ps --all
There - you should see your container. You probably think - oh my god this sucks! I want my cowsay back! Fear not! The thing is that you ran ubuntu in the interactive mode in which container automatically stops after you leave shell (more or less). Let’s bring back your ubuntu to life. Replace funny-name with the name of your stopped container.
docker start <funny-name>
Ok, your ubuntu should be running in the background right now - check it with ps command. Now if you want to execute cowsay again you can do it easily by connecting to your container.
docker exec -ti <funny-name> bash
/usr/games/cowsay 'Actually I love NerdyGeek!'
When you’re done with your container you can stop it with:
docker stop <funny-name>
That’s it! You’ve just created your first Ubuntu container, customized it and stopped it - you can always start it again and add something more, test something else, etc.
The last thing missing is - what about image? Well, Ubuntu image you’ve downloaded from Docker hub wasn’t changed. Docker is “layer-oriented” as one might say, the image has some layers in it and by running and changing container you’re introducing a brand new layer on top of it, which - let’s say - has a higher priority over the image ones. Every change you make in the container is saved to this new layer. The question here is - can I persist this layer somehow for the future? Can I save my container to be an image and why would I want to do that? Of course, you can - this is so-called “commit” and you can do it like that:
docker commit <funny-name> nerdy-geek-ubuntu
What docker did is that it applied your changes (your new layer) to the image so that when you run the container from newly created image then:
- You already have cowsay installed
- A new layer is created just above your previous one
- You can create many containers from your new images and for example, install fortune on one of them and Postgres (why not?) on the other one
- You can commit those new containers and get 2 new images with cowsay and fortune as well as cowsay and Postgres.
- You can easily share your new super awesome image with others
What more can Docker do?
What I described is just a small part of Docker functionality and I’ll for sure write more about it, but to give you an idea of what can be done you have to be aware of the following:
- You can manage networks - a.k.a. put a few containers in common network, and others in other - isolated network
- You can map ports from the container to your host system (access Postgres running within container from your colleague’s computer)
- Manage data volumes - persistent storage which can be accessed by many containers - for example Postgres data folder.
- Create more advanced images with Dockerfile - i.e. instruct Docker to install cowsay while creating the container so it’ll be up-to-date not the version when you created your image.
- Much more!
Summary
Docker is awesome and I’m pretty sure that you are now hyped about it as well! Go check the official documentation for more awesome things you can do. Soon I’ll write a post about utilizing Docker in a development cycle and an example of running real software like Elasticsearch. Stay tuned and see you soon!
comments powered by Disqus