Docker

Docker fundamentals including images, containers, registries, and common commands.

Published January 15, 2025 ET

Basic idea

Docker is a platform for doing 3 things:

  1. developing apps
  2. shipping apps
  3. running apps

Main vocab

Images, Containers, Registries, Volumes, Dockerfiles, Top Commands

1. Images & Dockerfiles

A docker image is a template for a container. You run an image to get a container. An image is a read-only template with instructions for creating a container. These instructions live in something called a dockerfile. An image can be something like instructions for spinning up a container running Ubuntu 19.10. The dockerfile is read and used to build an image.

Dockerfiles have individual lines of instructions, each of which corresponds to a "layer" of the image. If the dockerfile is changed, only the layers corresponding to the lines that were actually changed are regenerated.

2. Containers

Containers are runnable instances of images. You can create, start, stop, or move a container.

Apps are developed in containers, where a container is just a loosely isolated environment (file system). Multiple containers (file system instances) can run on the same host machine (your laptop, a server, or a virtual machine running somewhere on one of those).

A container, once spun up, gives you something like what a virtual machine (VM) gives you -- a private file system -- but unlike with a VM, there is no hypervisor to share kernel resources between the different containers. Instead, docker itself manages your machine or virtual machine's resources between the containers.

Containers can be attached to one or more networks and storage instances. If there are multiple containers on the same host, the extent to which those containers share networks, storage, and other subsystems can be specified.

3. Registries

A registry is just a collection of Docker Images.

When docker is installed on a computer, it has a local registry, and can be configured to connect to a remote registry.

4. Top Commands

docker container create [OPTIONS] IMAGE [COMMAND] [ARG...]

ex: docker container create ubuntu

  • this creates a docker container from the "ubuntu" image
docker pull IMAGE

ex: docker pull ubuntu

  • this looks in your remotely configured registry for the "ubuntu" image
docker build IMAGE

ex: docker build -t thisissomedockerfile

  • this builds and image from a dockerfile
docker run -i -t ubuntu /bin/bash
  • looks for the "ubuntu" image locally, and if it can't find it then it looks for it at the remote configured registry
  • creates a container based on the "ubuntu" image
  • allocates a read-write file system to the container
  • creates a network interface with the default network, including assigning an IP address
  • the -i -t flags tell docker to interactively attach the terminal to the container
  • /bin/bash is the location of the program to execute once the container has been created, in this case bash
  • executing exit will terminate the /bin/bash command and stop the container, but will not remove the container.
docker login && docker push elephantjacques/cheers2019
  • logs into registry and pushes to it
docker ps
  • lists all the existing docker containers and shows the ID for each one
docker stop SOMEDOCKERID
  • stops the container with an ID of SOMEDOCKERID
docker system prune
  • removes all the stopped containers, dangling images, etc.

Related

  1. /bin/bash -> What exactly is a shell?
  2. How is a normal operating system generated on a machine?