Published on

Docker: What is it and why should you use it?

Authors
  • avatar
    Name
    Kaur Kadak
    Twitter

1. Introduction

“Docker is an open platform that simplifies application development, delivery, and execution. It achieves this by separating applications from infrastructure, allowing for faster software delivery.” - Docker documentation

Docker allows you to run your application in an environment called a container. Imagine it this way, you want to move your furniture from one house to another. Wouldn’t it be great to have a magic moving box which you could place all of your stuff in to, and as soon as you put that box into your new home your furniture automatically sets up where it needs to be, regardless of what the house looks like?

Well Docker is this magic box - for your code.

You can run a Docker container on your local machine, virtual machine or on the server of a cloud provider. Once you’ve got your application containerised, going live is as easy as pressing upload.

2. Understanding Docker

2.1 Containers, what role do they play?

Docker containers are the most important thing related to docker, it’s what takes your packaged code and gives it an environment to run in. Container are lightweight, standalone, executable packages that contains everything you need to run a piece of software. That includes the code, runtime, system tools, libraries, and settings. Their main benefits are that they provide a consistent and isolated environment for running which makes them highly portable and simple to manage.

Some key benefits that containers bring to the table:

  1. Isolation - Isolation from the host and the system (and from other containers) ensures that a running container won’t interfere with other applications.
  2. Portability - You can create a container on your own machine, such as your laptop, and run it on another machine, like a AWS server, without modification.
  3. Efficiency - Great resource utilisation makes allows the use of multiple containers at a time on a single host machine.
  4. Consistency - The classic “it works on my machine” argument, we’ve all heard it. The difference is that when you’ve dockerized your app (made a container for it), then it will work on any machine that can run docker.
  5. Modularity - The encapsulation of specific components is what Docker is all about, when you have a lot of containers talking to each other then you basically have a micro-service architecture.

2.2 Other important terms

  • Image

    • A blueprint of a container. These read-only files can be considered as the starting point for creating Docker containers. You can think of them as a template that contains all the instructions and files required to create and configure a container. Once an image is built, it can be stored in a Docker registry.
  • Dockerfile

    • Images are typically built using a Dockerfile, which is a text file that specifies the instructions for creating the image
    Image explaining docker docker files
  • Registry

    • A Docker registry is a centralised repository for storing and distributing Docker images. It’s basically GitHub, but instead of code, its docker images. A registry usually comes with many known features you would expect from a repository service: versioning, public and private registries and security and access control to name a few.
    Image explaining docker registries
  • Volume

    • A Docker volume is a mechanism that allows data to be persistently stored and shared between Docker containers and the host system. It provides a way to manage and handle data separately from the container itself, ensuring that data is retained even when containers are stopped, removed, or replaced.
    • Best example here would be if you were running a database docker container. If volumes were not persisted then, anything you saved on the database would be deleted, each time you shut down the docker container.

2.3 The technology underneath

Docker uses several underlying technologies and features from the Linux operating system to achieve containerisation and manage containers effectively.

Some notable examples are:

  • cgroups
  • Linux Namespaces
  • Linux Containers (LXC)* - Docker initially used LXC as its containerization technology. LXC is a Linux feature that provides lightweight process isolation, allowing multiple containers to run on a single Linux host. However, Docker has since transitioned to its own container runtime.
  • Networking - Docker uses Linux's network namespace and bridge networking to isolate network resources for containers.

3. Why Use Docker?

In short: portability. Deployments from development to production look seamless, while utilizing fewer resources compared to traditional virtual machines.

Containers, guarantee independence, security, and reproducibility. Its hugely reduces DevOps and CI/CD pipelines deployment processes, allowing for rapid scaling, version control, collaboration, and efficient collaboration with tools like Kubernetes.

Additionally, Docker benefits from a strong community, providing many resources and integrations for users.

For example imagine you wanted to set up a database on your computer. You could either:

  • Download the database software onto your machine
  • Manage the dependencies
  • Set up custom configuration

And now you got a locally working database, but if you wanted to set it up on another machine you would have to do it all again.

Or …

  • docker pull postgres
  • docker run --name Postgres-container -e POSTGRES_PASSWORD=secretpassword -d postgres

Running a Postgres instance is literally that easy, and you can run it on any machine that support docker. Pretty awesome in my humble opinion.

4. Docker Basics

Below are some fundamental Docker commands and their functionalities:

  1. Pull Image:

    Pulls a Docker image from a registry (e.g., Docker Hub) to your local machine.

    docker pull [IMAGE_NAME]:[TAG]

  2. Run Container:

    Executes a Docker container from a specified image.

    docker run [OPTIONS] [IMAGE_NAME]:[TAG]

  3. List Containers:

    Lists all running containers.

    docker ps

  4. Stop Container

    Halts a running container.

    docker stop [CONTAINER_ID/CONTAINER_NAME]

  5. Remove Container

    Deletes a stopped container.

    docker rm [CONTAINER_ID/CONTAINER_NAME]

  6. List Images

    Lists all available Docker images stored locally.

    docker images

  7. Execute Command in Container

    Runs a command inside a running container.

    docker exec [OPTIONS] [CONTAINER_ID/CONTAINER_NAME] [COMMAND]

5. Additional Resources