Intro to Docker 🚢

Sid Arcidiacono
6 min readJul 22, 2021

--

Docker is a containerization tool that’s becoming more and more widely adopted in industry because of the ecosystem of services it’s compatible with, its relative ease of use, and its availability of support. But what is containerization, and why do we use it?

In the wise words of Wikipedia, Docker is:

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.

Let’s clear this up a bit.

Why Containers? 📦

Using a service like Docker to put applications and programs into containers has the primary purpose of maintaining a consistent machine environment for an application or piece of software, no matter what server it lives on — on-premise, cloud, what OS the server has, and so on. There are a few other adjacent benefits, but for now, let’s keep it simple: Docker packages our code and dependencies, so that we can maintain a consistent machine environment for our software.

You might be asking (and you’d be right to ask) — what’s the difference between Docker and a Virtual Machine, then? Why wouldn’t I just spin up a VM for my code, and call it a day?

While a VM achieves the goal of sandboxing software and its dependencies inside an isolated environment, it does so at a cost. The computational cost of virtualizing an entire machine is pretty high. The Docker curriculum expresses how Docker is different (better than I can):

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.

For conceptual purposes, you can think of a Docker container like a compact version of a VM — a sandbox environment that only uses what it needs of a virtual OS to run your software.

Containers make deployment easy, reproducible, and standardized. This is why we like them.

Taking a Step Back 👣

Now that we understand what containers are, what Docker is, and why we care, let’s take a step back and set some expectations. In this article, we’ll:

  • Go over some important terminology you’ll encounter when working with Docker; like containers (check! ✅), images, and registries
  • Walk through installing Docker on Linux
  • Go through basic Docker commands to familiarize ourselves with those we’ll use most often

Wait, What’s an Image? 🖼

Let’s use the analogy of a container ship on the ocean to get the hang of our terminology. One container ship holds many different containers from a variety of companies, and each container is full of goods. Let’s zoom in on one container. This container holds a specific amount of specific kinds of goods, and they didn’t get there on accident. Someone at the company shipping this container laid out for workers what should go inside it.

An image is the person who tells the workers what to pack in the container. In other words, it’s like a blueprint for containers to be built off of. If you’re familiar with data terms, an image has a one-to-many relationship with all containers based off of it — many containers can be spun up off of a single image.

What About a Registry? 🗂

Well, I know this is a lot of terms, but registries (also called image registries, container registries, or image repositories depending on the cloud services you use) are actually pretty straightforward. If we have a lot of software, we’re likely going to need somewhere to store all of the blueprints.

So, if our images are our blueprints, our containers are our running sandboxed applications, our registries are kind of like a bookshelf or a filing cabinet that hold our images. This allows us to pull our own images, or, if your projects are open source, for others to pull your images and be able to spin up an instance of your application on their own machine without needing to worry about installing any dependencies or configuring their environment at all (aside from just having Docker installed).

Before we move on, take a look at the diagram below to get familiar with the terms we’ve just talked about.

Image courtesy of source.

Setting up Docker ⚓️

While Windows and Mac allow a desktop download, most Ubuntu distros don’t have a GUI. This tutorial will walk you through setting up Docker on Ubuntu, which will be useful for deployment within Linux VMs. If you have a Windows or Mac machine, check out the links below to get Docker set up, then catch back up with us in the next section.

For Windows, use this resource.

For Mac, use this resource.

Pull up terminal on your Linux machine, and run update before installing anything:

$ sudo apt update

You can also run upgrade after, to be sure you’re up to date with installed packages:

$ sudo apt upgrade

If the kernel doesn’t upgrade, you’re good to move on. If it does, reboot with:

$ sudo reboot

Afterwards, we can install Docker:

$ sudo apt install docker.io

If you have any issues installing the docker.io package, check out the below resources to troubleshoot:

  1. Docker docs
  2. Linux.com

You can check that Docker was successfully installed with:

$ sudo docker --version

But why do we have to use sudo?

Docker out of the box requires admin permissions. For security reasons, you don’t always want to use Docker as the root user or always have to use sudo. The following steps take you through setting up a user to use with Docker, and then rebooting.

$ sudo usermod -a -G docker $USER

Once you’ve run this, log out and back in and try running:

$ docker --version

to check that permissions have changed. If they haven’t you’ll receive an error.

Now, try running Docker’s “Hello World” image to verify your install:

$ docker run hello-world

Nice! If that was successful, we’re ready to chat about some Docker commands.

Docker Commands 📃

There are a few Docker commands you’ll use often and want to know. For a comprehensive list of all commands and links to more details on them, check out the Docker documentation.

  • run
  • build
  • exec
  • logs
  • ps
  • images

Let’s start with build. The syntax for build is:

$ docker build [OPTIONS] PATH | URL | -

build builds a docker image from a Dockerfile and what’s referred to as a context. A context can be your project directory, a GitHub repository, and more, depending on what options are specified and if you provide a URL.

Docker run runs a container based off of a built image (there’s a little more to it than that, but you don’t have to worry about that for now. Check the docs if you’re interested). It’s syntax is as follows:

$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

Some options commonly provided to run are port mappings, volumes, detach, and so on.

Docker ps allows you to list the containers on your machine. Providing the -a flag allows you to see both running and stopped containers.

$ docker ps [OPTIONS]

Similarly, Docker images allows you to list the images on your machine:

$ docker images

Docker logs provides you with logging output for running containers, which is valuable for debugging and troubleshooting. The syntax is as follows:

$ docker logs [OPTIONS] CONTAINER

Here, CONTAINER can either specify a container name or ID, both provided in the output of docker ps.

A common use for docker logs is:

$ docker logs --follow my-container

The --follow option tells the logs to stay up and auto-update with new data as the container application is running.

Finally, Docker exec runs a new command in a running container.

$ docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Wrapping Up

Hopefully now you feel like you have a better understanding of what Docker is, why we use it, and how to get things set up and running on your own machine. We’ve covered:

  • Important terminology you’ll encounter when working with Docker; like containers, images, and registries ✅
  • Installing Docker on Linux ✅
  • Basic Docker commands to familiarize ourselves with those we’ll use most often ✅

--

--

Sid Arcidiacono
Sid Arcidiacono

Written by Sid Arcidiacono

Writer | Editor | Ex-techie | Artist | Passionate about innovation, sustainability, and ethics

No responses yet