Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

Docker cheatsheet

📅 2017-Oct-19 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cheatsheet, docker ⬩ 📚 Archive

Build

You need to create a Dockerfile that describes the plan to build an image. Here is a simple Dockerfile named Dockerfile that creates an image based off busybox Linux image into which we are copying a local file:

FROM busybox
COPY haha.cpp /somedir/haha.cpp
$ docker build .
$ docker build -f Dockerfile .

This looks for a Dockerfile in the current directory and also uses all the content of the current directory as the build context. Having a big build context with lots of files and directories in it can slow down a Docker build.

The build ends printing the hash of the image it has created. This image can be run using docker run commands (see below) by using this hash.

$ docker build -f /path/to/Dockerfile path/of/build/context

Remote registry

$ docker pull imagename

$ docker pull ubuntu
$ docker pull nvidia/cuda

When no tag is specified, like above, docker automatically pulls the image with the tag latest.

Notice that we are not specifying the domain name or URL of any Docker registry. In such cases, Docker will look for and pull imagename from the default Docker registry: https://hub.docker.com/ The image name can be a single word (ubuntu) or a relative path (nvidia/cuda). For a single word image name, its tags can be seen listed at the https://hub.docker.com/_/ubuntu. The image name with a path has its repositories listed at https://hub.docker.com/u/nvidia and the tags of a particular image at https://hub.docker.com/r/nvidia/cuda.

$ docker pull ubuntu:20.04

Note how the : character is used to separate the image name and the tag. In the case of Ubuntu, the list of available tags can be viewed here. Not all registries may support viewing tags like this.

$ docker pull <registry.domain>/imagename

You might need to first authenticate to the registry domain before the pull will work:

$ docker login <registry.domain>

After a successful login, Docker will store a key provided by the registry server in your ~/.docker/config.json. It will reuse this key for subsequent pull or push to this registry.

Local images

$ docker images

This is the same as this command:

$ docker image ls

What is listed are the images that you either pulled from a registry or built locally.

$ docker images -a
$ docker images --no-trunc
$ docker rmi repo1/foobar2
$ docker rmi repo1/foobar2:tag123
$ docker image prune
$ docker image prune -a
$ docker image prune -a --filter "until=168h"
$ docker system prune

This is useful when your filesystem runs out of space due to docker images.

$ docker system prune -a

This pretty much removes all Docker data on your system. This is useful when you really want to clean out everything.

$ docker system df

In this output, the size of the Images is the disk space occupied by your Docker images.

Containers

$ docker run image_name

This is only useful if running the container runs a service or a daemon inside it automatically.

$ docker run -it image_name /bin/bash

This is usually the method to use to have interactive access to a container.

$ docker ps
$ docker ps -a
$ docker ps --no-trunc
$ docker stop container_name
$ docker rm 45235345

Tried with: Docker 18.09.6 and Ubuntu 18.04