Skip to the content.

Docker

Overview

This content is intended as a summary introduction for developers that may need to work with and support Docker-based applications in production, by guiding users to other information resources and suggesting a sensible order in which to learn things.

External learning references

In addition to this page, the following links may contain useful or more-detailed content for learning Docker:

What is Docker, and what is a Container?

Docker is a open source computer program that performs operating-system-level virtualisation (also known as containerisation). Docker allows us to deploy and run software applications inside Containers, which (roughly) are applications that are packaged up with all the assets needed to run (binaries, libraries, static files, etc.) on any suitable host that runs the Docker daemon. A Container is (approximately) an executable which is a kind of extremely lightweight VM without including any OS of its own. In effect, Docker provides a set of APIs that a Container can use to access the underlying OS functions.

References:

What is an Image?

A Docker image is a read‑only, versioned package that contains an application and everything it needs to run—code, runtime, libraries, environment variables, and configuration.

Images are stored in a Registry (see below), and are (roughly) equivalent to a tagged Git commit of a Container. Images are built from instructions in a Dockerfile (see below).

References:

What is a Dockerfile?

A Dockerfile is a text document that contains all of the commands a user would call on the command line to assemble an Image. An analogy: a Dockerfile is like a cake recipe, an Image is a pre-made cake mix, a Container is a finished cake.

References:

What is a Registry?

A Docker registry is a storage and content delivery system for named Docker images. It can be self-hosted, third-party-hosted, or the official Docker Hub registry (https://hub.docker.com/).

References:

Docker command examples

Container creation and usage

Pull an image from the online registry to local storage:

docker pull <IMAGE NAME>

Basic run command:

docker container run --publish 80:80 nginx

Breakdown:

docker container run --publish 80:80 --detach nginx

Same as above, but detached. Stop a container:

docker container stop CONTAINER_ID

List containers:

docker container ls
docker container ls -a

Give it a name (“webhost”):

docker container run --publish 80:80 --detach --name webhost nginx

View logs, use top and stop it:

docker container logs NAME
docker container top NAME
docker container inspect NAME
docker container stop NAME

Remove containers:

docker container ls -a
docker container rm ID1 ID2
docker container rm -f ID

Manage containers assignment (command list):

docker container run --publish 8082:80 --detach --name webserver httpd
docker container logs webserver
docker container run --publish 3306:3306 -d -n mysql_db -e MYSQL_RANDOM_ROOT_PASSWORD=true
docker container run --publish 3306:3306 -d --name mysql_db -e MYSQL_RANDOM_ROOT_PASSWORD=true
docker container run --publish 3306:3306 --detach --name mysql_db -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql
docker image ls
docker container ls -a

Viewing information about running containers:

docker container top NAME
docker container stats

Getting inside a running container:

# Start a container interactively and run sh in it:
docker container start -it alpine sh
# Run a bash shell on a running container:
docker container exec -it NAME /bin/bash

Misc. command dump:

# Display IP address of a running container:
docker container inspect --format '' NAME
# Remove all exited containers:
docker ps -a | grep Exit | cut -d ' ' -f 1 | xargs docker rm

Docker container networking: https://docs.docker.com/network/

docker network ls
docker network inspect NAME
docker network create NAME
docker network connect NETWORK CONTAINER
docker network disconnect NETWORK CONTAINER

Container lifetime & persistent data

Define a named volume when starting a container:

docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v mysql-db:/var/lib/mysql mysql
docker container run -d --name pgdb -e POSTGRES_PASSWORD=pass -p 5432:5432 -v pgdb:/var/lib/postgresql/data postgres

Bind mounting maps a host file/dir to a container file/dir. Can’t use this in a Dockerfile, must be at container run. E.g.:

docker container run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=True -v /path/on/host:/path/in/container mysql

A basic Docker image recipe

Documentation: https://docs.docker.com/build/

Basic steps are as follows:

Best practices for building Docker images: https://docs.docker.com/build/building/best-practices/.

Using the GitHub Container Repository (ghcr.io)

There are a couple of steps required for a developer to use the GitHub Container Repository to upload build Docker images.

  1. The developer needs to create a Personal Access Token (instructions here) and use that as the password to authenticate with the registry (i.e. docker login ghcr.io -u <USERNAME>).
  2. The uploaded image needs to be linked to a repository (instructions here), either manually in GitHub or using a LABEL in the Dockerfile.
  3. Permission settings for Github packages need to be set IN ADDITION to permissions on the linked repository. Having linked a package to a repo and pushed an image, open the repo in GitHub, click the container package on the right-hand side, then click “Package settings” on the right hand side again. Set the required Inherited Access settings on this page.