← All writing
blog

Docker Primer

When we think of virtualization today, we may think of Virtual Box, which abstracts away the system processes, and lets you run a complete system from another. Think of Docker as Virtual Box, but extremely lightweight (in terms of resource consumption). Obviously I’m over simplifying the explanation a little, and a whole lot of things are getting lost in simplification. But for now, this will do.

Installation

Install docker:

Terminal window
apt remove docker docker-engine docker.io
apt update && apt -y upgrade
apt install -y linux-image-extra-$(uname -r) linux-image-extra-virtual
apt update && apt -y upgrade
apt install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt update && apt -y upgrade
apt install -y docker-ce

Install docker-compose:

Terminal window
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

Post Installation

Allow docker usage without sudo access:

Terminal window
sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

Terminology

  • Images: Read-only templates uploaded to Docker Hub; analogous to classes in OOP
  • Containers: Runtime instances spawned from images; similar to object instances
  • Tags: Version identifiers for images
  • Container ID or Image ID: Unique identifiers assigned to each container or image

Usage

A Dockerfile can be used to build docker images.

List all images available on your system:

Terminal window
docker images

List all containers:

Terminal window
docker ps -a

Pull images from the docker hub:

Terminal window
docker pull ubuntu

By default the latest tag is pulled. To pull a specific tag:

Terminal window
docker pull ubuntu:12.04

Delete an image:

Terminal window
docker rmi ubuntu:latest

or

Terminal window
docker rmi IMAGE_ID

Create a container from an image and run it:

Terminal window
docker run -it ID /bin/bash
  • i stands for interactive
  • t stands for pseudo tty
  • ID can be an IMAGE-ID or CONTAINER-ID

Note: Every time you use docker run with an IMAGE-ID, a new container is created based on the image. Using --rm flag automatically deletes the container after exiting.

Exit a container:

Terminal window
exit

Start / stop a container:

Terminal window
docker start CONTAINER_ID
docker stop CONTAINER_ID

Delete a container:

Terminal window
docker rm CONTAINER_ID

Build your own docker image using a Dockerfile:

Terminal window
docker build -t username/image_name:image_tag .

Delete dangling images:

Terminal window
docker rmi $(docker images -f dangling=true -q)

Save an existing docker container as an image:

Terminal window
docker commit CONTAINER_ID IMAGE_NAME:IMAGE_TAG

Save an image as a tar file:

Terminal window
docker save IMAGE_ID -o file_name.tar