Build and deploy your distributed applications easily to the cloud with Docker
Written and developed by Prakhar Srivastav and ADI.
Wikipedia defines Docker as
an open-source project that automates the deployment of software applications inside containers by providing an additional layer of abstraction and automation of OS-level virtualization on Linux.
Wow! That's a mouthful. In simpler words, Docker is a tool that allows developers, sys-admins etc. to easily deploy their applications in a sandbox (called containers) to run on the host operating system i.e. Linux. The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development. Unlike virtual machines, containers do not have the high overhead and hence enable more efficient usage of the underlying system and resources.
The industry standard today is to use Virtual Machines (VMs) to run software applications. VMs run applications inside a guest Operating System, which runs on virtual hardware powered by the server’s host OS.
VMs are great at providing full process isolation for applications: there are very few ways a problem in the host operating system can affect the software running in the guest operating system, and vice-versa. But this isolation comes at great cost — the computational overhead spent virtualizing hardware for a guest OS to use is substantial.
Containers take a different approach: by leveraging the low-level mechanics of the host operating system, containers provide most of the isolation of virtual machines at a fraction of the computing power.
As developers, we are cognizant of the power we hold in our hands - the ability to build apps to share with the entire world is very empowering. However, one key aspect of that realization involves deploying our application either on the cloud, a datacenter or even a single server. Given that apps today involve so many components - an application server, a database, a caching layer etc. deploying and managing these architectures is extremely complex.
By standardizing an application and its dependencies into a container, Docker enables us to build, test and release our apps quickly and easily. Due to its benefits of efficiency and portability, Docker has been gaining mindshare rapidly is now leading the Containerization movement. As a developer going out into the world, it is important that we understand this trend and see how we can benefit from it.
This tutorial will give you a hands-on experience with deploying a docker container on Amazon Web Services (AWS). It will demystify the docker landscape, clarify the differences between images and containers and finally, deploy a python application on the cloud.
This document contains a series of several sections, each of which explains a particular aspect of Docker. In each section, we will be typing commands (or writing code). All the code used in the tutorial is available in the Github repo.
There are no specific skills needed for this tutorial beyond a basic comfort with the command line and using a text editor. Prior experience in developing web applications would be helpful but not required. As we proceed further along the tutorial, we'll make use of a few services. If you're interested in following along, please create an account on each of these websites -
Getting all the tooling setup on your computer can be a daunting task, but thankfully as Docker has become stable getting it up and running on your favorite OS has become very easy. At first, we'll install docker.
Until a few releases ago, running docker on OSX and Windows was quite a hassle. Lately, however, docker has invested significantly into improving the on-boarding experience for its users on these OSes and hence running Docker now is a cakewalk. The getting started guide on Docker has detailed instructions for setting up Docker on Mac, Linux and Windows.
Once you are done installing docker, test your docker installation by running the following
$ docker run hello-world
Hello from Docker.
This message shows that your installation appears to be working correctly.
...
Python comes pre-installed on OSX and (most) Linux distributions. We'll also be using pip for installing packages for our application.
If don't have pip installed, please download it for your system.
$ python --version
python --version
Python 2.7.10
$ pip --version
pip 7.1.2 from /Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg (python 2.7)
The app that we'll be developing will be using Elasticsearch for storage and search. In order to run elasticsearch locally, make sure you Java installed. If Java is installed, typing java -version
in your terminal should give you an output similar to the one below.
$ java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)
Now that we have everything setup, it's time to get our hands dirty. In this section, we are going to run a Busybox (a lightweight linux distribution) container on our system and get a taste of the docker run
command.
To get started, let's run the following in our terminal
$ docker pull busybox
Note: Depending on how you've installed docker on your system, you might see a
permission denied
error on running the above command. If you're on a Mac, make sure docker engine is running. If you're on Linux, then prefix yourdocker
commands withsudo
. Alternatively you can create a docker group to get rid of this issue.
What the pull
command does is it fetches the busybox image from the docker registry and saves it in our system. You can use the docker images
command to see a list of all images on your system.
$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
busybox latest c51f86c28340 4 weeks ago 1.109 MB
Great! Lets now lets run a docker container based on this image. To do that we are going to use the almighty docker run
command.
$ docker run busybox
$
Wait, nothing happened! Is that a bug? Well, no. Behind the scenes, a lot of stuff happened. When you call run
, the docker client finds the image (busybox in this case) loads up the container and then runs a command in that container. When we run docker run busybox
, we didn't provide a command so the container booted up, ran an empty command and then exited.
Well, yeah - kind of a bummer. Let's try something more exciting.
$ docker run busybox echo "hello from busybox"
hello from busybox
Nice - finally we see some output. In this case, the docker client dutifully ran the echo
command in our busybox container and then exited it. If you've noticed, all of that happened pretty quickly. Imagine booting up a virtual machine, running a command and then killing it. Now you know why they say containers are fast! Ok, now it's time to see the docker ps
command. The docker ps
command tells you what all containers are currently running.
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Since no containers are running, we see a blank line. Let's try a more useful variant of docker ps
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
305297d7a235 busybox "uptime" 11 minutes ago Exited (0) 11 minutes ago distracted_goldstine
ff0a5c3750b9 busybox "sh" 12 minutes ago Exited (0) 12 minutes ago elated_ramanujan
So what we see above is a list of all containers that we ran. Do notice that the STATUS
column shows that these containers exited a few minutes ago. You're probably wondering that if there's a way to run more than just one command in a container. Let's try that now
$ docker run -it busybox sh
/ # ls
bin dev etc home proc root sys tmp usr var
/ # uptime
05:45:21 up 5:58, 0 users, load average: 0.00, 0.01, 0.04
Running the run
command with the -it
flags attaches us to an interactive tty in the container. Now we can run as many commands in the container as we want. Take some time to run your favorite commands.
Danger Zone: If you're feeling particularly adventureous you can try
rm -rf bin
in the container. Make sure you run this command in the container and not in your laptop. Doing this will not make any other commands likels
,echo
work. Once everything stop working you can exit the container and then run it back up again with thedocker run -it busybox sh
command. Since docker creates a new container everytime, everything should start working back again.
The docker run
command is going to be command you'll be using the most so it makes sense to explore it in more detail.