This post dives into the world of Docker with the goal of giving you an overview of what Docker is and how it’s used in the real world. I absolutely love Docker, so I really hope I can explain it in a way that makes you love it too. I’ll start with the formal definition and then I’ll try to make it seem less daunting.
According to Wikipedia, Docker is:
Docker is a computer program that performs operating-system-level virtualization. It was first released in 2013 and is developed by Docker, Inc.
Docker is used to run software packages called containers. Containers are isolated from each other and bundle their own application tools, libraries and configuration files; they can communicate with each other through well-defined channels. All containers are run by a single operating-system kernel and are thus more lightweight than virtual machines. Containers are created from images that specify their precise contents. Images are often created by combining and modifying standard images downloaded from public repositories.
My simple definition is: Docker is: Magic!
Actually, Docker and more specifically the Docker Engine (which is installed with Docker Desktop), is an application that Software Developers, IT gurus and DevOps ninjas use to create docker images and Docker containers.
Below is a screenshot of Docker Desktop and you can see it has the Docker Engine installed.
What is the Docker Engine?
So the real question becomes what is the Docker Engine and what are Docker Containers and Docker Images?
We’ll start at a higher level and work our way down. Docker Desktop installs the engine and tools a developer needs (e.g. Docker Machine). The Docker engine is the ‘thing’ you interact with typing
docker in a terminal.
Example – Running a docker image:
docker run <image_name_or_id>
Example – Listing your current active containers:
What is a Docker Container?
The next thing is (again working backward) is a Docker Container, which is a running instance of a Docker Image. You can think of a container a virtual environment (an operating system like Windows or Linux), similar to a virtual machine but without all of the bloat that comes with a virtual machine. That’s your barebones container definition.
What is a Docker Image?
A docker image is essentially a snapshot of the OS and tools you want to use. In some cases, it’s just the OS, while in others it’s an OS with a database, or an OS with specific application code installed. For example your custom e-commerce site, or project management system.
Import point about the OS I mention above: It resembles a virtual machine but without all the extra bloat.
In reality, you get to piggyback off of the hosts OS and the container only needs the bare minimums to actually run. This is how it’s super lightweight compared to a full-blown virtual machine.
The actual order of things:
Now that we have all the minutiae out of the way, we can see how simple it is to use Docker:
- Define a Docker Image (or pick an existing one – see next step)
- Run an Image (this becomes a container)
docker run <insert_image_name_here>
- Stop the container when you are finished
Let’s start here and see how we can use Docker by going through various scenarios.
Docker as a learning tool.
Some people use a barebones container with just the underlying OS infrastructure, which can be used as a learning tool. For example, you want to learn Ubuntu but you don’t want to actually install Ubuntu on your laptop. With Docker, you can pull an Ubuntu Image run it, mess around with it and then simply dump it when you’re done.
Docker as a development tool.
In my day-to-day, I use Docker to keep my laptop free of extra installations. I don’t install Oracle, MySQL, RabbitMQ, Elastic Search, Kibana, etc. Instead, I simply load them via a container. It keeps the overhead down. Plus as my development team grows, they aren’t required to install all the tools & services. As long as they have Docker installed, the can simply issue a couple of commands and they instantly have an environment like mine on their local machine.
Here’s a quick example. Assuming you have Docker Desktop installed. You can issue a command like this to get MySQL up and running on your machine:
docker run --name mySqlGeekCafe -e MYSQL_ROOT_PASSWORD=p@ssW0rd -p 3306:3306 -d mysql
Docker as a controlled production environment
The next evolutionary step is to use Docker containers as your test, qa, production, etc environments. Just as you can “Dockerize” (make images and containers of developer tools), you can do the same with your deployed application.
Here’s an example of a production environment architecture. This uses AWS but you can easily do the same thing on another platform
- AWS ECS
- A Docker Container for our web site built in Angular
- A Docker Container for the web services (restApi calls), which runs on dotnet core
- A Docker Container for some background jobs (executes jobs from the restApi or a message queue)
- Rabbit Message Queue as a service on AWS
- MySQL as a service on AWS (RDS)
The system above is designed to scale. If ECS sees certain containers under a heavy load, it will spawn up more. When the load dissipates it will start reducing them.
The same system above matches our test environment. Through our CI/CD (continuous integrations / controlled deployment) process, changes in our develop branch automatically create and deploy containers into our
develop environment for testing. Changes in our release branch automatically build and run unit tests but do not auto deploy, we have a manually single push to accept button built into the AWS code pipeline.
I hope this intro to Docker has been helpful. I know from experience that Docker can be confusing at first but once you get the hang of it, you begin to see its full potential.
Until next time, happy Dockerizing!