Explain Like I'm Five: Containers and Docker
As I progress through my career, it's impossible to not having heard of Docker. Whether it be in the workplace or at school, talk of Docker is everywhere. That's because it is immensely powerful, and most importantly, easy to use tool that simplifies a lot of the software development process.
Before the days of Docker, developers used Virtual Machines (VMs) to run and test applications. If you've never heard of virtual machines, they're essentially self-isolated operating systems running on a set of hardware the developer doesn't have.
Ever wanted to run an application on Mac only to find out it only runs on Windows? If you really needed to run it, you might have used VMware. VMware would create a virtual machine with the Windows operating system (OS) on your computer. Then you would have used the virtual machine like any other computer.
Since they load an entire OS, running VMs on your computer would take up a lot of resources. The resource requirements would only grow proportionally to the VMs you ran. As such, using VMs limited the ability to transfer applications between clouds and data centres.
Then came containers.
Containers are similar to virtual machines in that they allow you to run applications on a separate system that you own. But rather than create an operating system for each container, only one shared OS is created for all containers. Everything an application needs to run is configured in the container.
Rather than taking gigabytes and minutes to start up a virtual machine, we only take megabytes and seconds to start up a container. Since the process to setup a container is so simple, it's very easy to distribute them. Anyone could configure a container very quickly and have the same environment setup. From a private data centre to a developer's laptop, containers can be deployed in seconds.
If you need a more intuitive explanation, another way to think about it is with restaurants and food trucks. A restaurant would have the necessities of a counter, a stove, an oven, a sink, and a refrigerator. They might also have seating, a walk-in fridge, maybe even a bar. A food truck would have the same necessities as well, but it's unlikely they would have any of the extras.
If all I wanted to do was to cook food and serve it, both would work. But it would be a lot easier for me to serve food on a street of my choice in a food truck than a brick and mortar restaurant.
Containers aren't new. They didn't come into existence at the same time as Docker. Docker also isn't the only tool available. OpenVZ, a technology that allows users to create multiple Linux containers, has been available since 2005. There is also Solaris Zones, FreeBSD Jails, and more.
So why does Docker have such a large community?
While some of its popularity can be attributed to hype, it's also because Docker has simplified the tools used to manage containers. Docker allows you to setup a configured instance of the container (image) using a single file (i.e. the Dockerfile). When you're ready to deploy an application, you just need to push the updated container of that image. Anyone who wants to use it can pull it onto their system.
The command line is also pretty nice.
This downloads the image
hello-world(the first time only), then runs a container using the image
$ docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly.
See all containers that are active on your system with
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 15e3bc8f45e0 hello-world "/hello" 11 seconds ago Exited (0) 10 seconds ago peaceful_murdock
Even though a container is stopped (
STATUS: Exited), it'll still remain on your system until you delete it
$ docker rm 15e3bc8f45e0
If you want to delete all containers in a system, you can run
$ docker rm $(docker ps -a -q)
Run command in a container (In this example, it's an Ubuntu image)
$ docker run ubuntu cat /etc/lsb-release
Run an interactive shell inside the container
$ docker run -it ubuntu bash
Get the status of a container
$ docker ps -a
Run a shell command in a named container
$ docker run --name mycontainer ubuntu cat /etc/lsb-release
Run the container with shared directory
$ docker run -v $(pwd):/shared ubuntu ls /shared
Run the container with the current working path
$ docker run -v $(pwd):/shared -w /shared ubuntu ls
Stop a container
$ docker stop container_name
Stop all containers
$ docker stop $(docker ps -a -q)
Delete all images
$ docker rmi $(docker images -q)
Docker is a great tool to use as a standalone, but another reason why it's so popular is because Docker has put tools together in an accessible and appealing way. Tools like Docker Swarm, Docker Compose, Mesosphere, Helios, Kubernetes all work with Docker to enhance the usage of containers.
For more resources on Docker, check out the official documentation or this tutorial. Prefer a more hands on approach? Want a badge to show off your learning? Check out the Docker Essentials course from Cognitive Class.