A Brief Introduction to Docker

Dec. 20, 2023, 11:05 p.m.

What are Containers?

Containers are completely isolated environments which contain all the dependencies required to run an application. Containers are isolated from all other processes running on that host machine.

Containers run on top of the Kernel operating system.

The kernel is responsible for interacting with the underlying hardware. Software runs on top of the Kernel. The software is what differentiates different operating systems.

Containers vs virtual machines

Containers run on top of a shared OS where as virtual machines run on top of a hypervisor and have their own OS. Therefor VMs have higher resource utilization and take more space. VMs therefore take longer to boot up. However containers are less isolated because containers share OSs.

Container vs image

An image is a package or a template for creating a container. A container is a running instance of a template image. An image is an isolated file system that contains all the dependencies needed to run an application.

What is Docker?

Docker is platform for containerizing applications.

Docker can run containers based on the same kernel of the underlying OS. eg if docker runs on top of Ubuntu docker can run containers which use Linux OSs, but not a container that uses windows.

Why do you need it?

Docker is great for eliminating Compatibility / Dependency issues in your development workflow. Docker also cuts down on long setup time. A great use case for using Docker is being able to create different dev/test/prod environments quickly and efficiently without having to worry about your application behaving differently.

What can it do?

Docker’s purpose is to containerize applications. Each service that makes up your application is run in a container with its own dependencies. This allows you to develop in isolation, and with confidence that your app can run anywhere.

Run Docker Containers

docker run [options] <image name> run a docker image on the local host if it already exists or will pull it from docker hub.

Create a Docker Image

docker build dockerfile builds the image based on a dockerfile. A dockerfile is a collection of instructions for setting up a service.

The image can then be used locally or pushed to a repository.

Networks in Docker

When you install docker three networks are created automatically:

Bind Mounting

Another way of persisting data in the container layer is through binding a location in the container to a location on the host system. This is what is known as bind mounting. eg:

docker run -v /data/mysql:/var/lib/mysql mysql

Docker Compose

Docker Compose is a tool that helps you define and share multi-container applications. With Compose, you can create a YAML file to define the services and with a single command, you can spin everything up or tear it all down.

You can then spin up the entire application at once using docker compose up and shut it down with docker compose down.

Example compose.yaml file:

services:

  app:

    image: node:18-alpine

    command: sh -c "yarn install && yarn run dev"

    ports:

      - 127.0.0.1:3000:3000

    working_dir: /app

    volumes:

      - ./:/app

    environment:

      MYSQL_HOST: mysql

      MYSQL_USER: root

      MYSQL_PASSWORD: secret

      MYSQL_DB: todos

  mysql:

    image: mysql:8.0

    volumes:

      - todo-mysql-data:/var/lib/mysql

    environment:

      MYSQL_ROOT_PASSWORD: secret

      MYSQL_DATABASE: todos

volumes:

  todo-mysql-data:

Here you define each container under services and then configure all of the options in your set up as key value pairs. For example adding binding or environment variables.

Container Orchestration

Container orchestration services like Kubernetes or Docker Swarm can manage the deployment of your containers across a cluster of computers or virtual machines. They take care of tasks such as health checks, autoscaling, making sure your apps are highly available, etc. This is a whole topic in and of itself, but for now it’s sufficient to say that container orchestration tools help manage containerized applications running on a distributed cluster.

In Closing

I hope this gave the reader a good overview of what docker is and some of it’s main features. Please be advised that this is just scratching the surface of the features and benefits of docker.