What Is Docker?

Updated

Docker is used to package and run applications consistently across different environments. The primary challenge is that there are many operating systems and versions of every dependency. Docker makes it easier to run code written on one device elsewhere, as long as the target can run a compatible container image.

Images and containers

A Docker image is an immutable package containing an application's filesystem, code, libraries, and configuration defaults. A Dockerfile is one common recipe for building an image; the image itself is the result, not the Dockerfile.

A container is a running (or stopped) instance of an image with an isolated process environment and a writable container layer.

Miscellaneous things to note:

  • Layer caching: Reuses unchanged build layers so dependencies do not need to be downloaded every time, which can dramatically speed up builds.
  • .dockerignore: Keeps irrelevant or sensitive local paths out of the build context.

To build an image:

docker build -t example .

Docker Compose

Docker Compose allows you to define and manage multiple containers together. Let's say you're building a web application with a frontend, backend, and database. You could use one huge container for all of that (you probably shouldn't), so instead you use separate services. This is where Docker Compose becomes really useful.

A Docker volume is Docker-managed persistent storage that can be mounted into one or more containers. It is not necessarily one normal file on your machine. A bind mount, by contrast, maps a specific host path into a container.

Also, you can just use docker init (LOL) to generate starter Docker files for supported projects.

Containers vs. virtualization

Both of these approaches abstract software away from some details of the underlying hardware and host environment, but they draw the boundary in different places.

How do VMs work? (The hypervisor)

Virtualization provides virtual hardware so multiple guest operating systems can run on one physical machine.

A hypervisor, or virtual machine monitor, manages the virtual machines and allocates resources among them. A guest VM normally includes its own kernel.

How do containers work?

Containers package application code, libraries, and configuration into an image. At runtime, containers run as isolated processes that share a kernel rather than booting a separate guest kernel for each container. Linux containers use Linux kernel features; Docker Desktop typically provides the required Linux environment through a lightweight VM on macOS and Windows.

Containerization isolates individual applications and their dependencies while letting them share an operating-system kernel.