Introduction to Docker Containers

All material (C) 2022-2023 by CSC -IT Center for Science Ltd. This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 Unported License, http://creativecommons.org/licenses/by-sa/4.0/

Outline

  • In this lecture, you will learn:
    • The basics of Docker containers
    • The practical usage of important Docker commands

Why Learn Containers?

A Solution with Containerisation

Reminder: What is Containerisation?

  • A container is a standard unit of software that packages up code and all its dependencies so that the application runs quickly and reliably from one computing environment to another.
  • You probably know:
    • Container ≠ VM
    • Share Host OS
    • An isolated process
    • Motto: Build, ship and then run it anywhere

Key Benefits of Using Docker Containers

Key Docker Terminology

  • Docker Image: The basis of a Docker container. Represents a full application
  • Docker Container: The standard unit in which the application service resides and executes
  • Docker Engine: Creates, ships and runs Docker containers deployable on a physical or virtual, host locally, in a datacenter or cloud service provider
  • Registry Service: Cloud or server based storage and distribution service for your images

Central Dogma of Containerisation

Docker Architecture

Important Docker Commands

Referencing an Image from a Registry

  • library: Pull an image from Sylabs cloud library
    • library://user/collection/image:tag
  • docker: Pull an image from Docker Hub
    • docker://repository/image:tag
  • Quay.io : Pull an image form RedHat Quay.io registry
    • docker: //quay.io/repository/image:tag
  • shub: Pull an image from Singularity Hub
    • shub://repository/image:tag

Pulling an Image from a Registry

  • Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
  • Example:

  • Example with a specific tag: docker pull ubuntu:23.10

Running Docker Containers: A Toy Example

More on Running Docker Containers

  • Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]
    • docker run … # simple run
    • docker run -d … # run in detached mode (= in the background)
  • Example:

Running Docker Containers in an Interactive Mode

  • Usage: docker run -it IMAGE[:TAG|@DIGEST] [COMMAND] [ARG…]
  • Containers are non-interactive by default
  • Example:

Running Status of Containers

  • Usage: docker ps # list all running containers
  • Example:
  • Usage: docker ps -a # list all running and stopped containers

Entering and Exiting Out of Containers

  • Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG…]

  • Example:

  • Just to come out of container, use exit or control+d

  • Save and exit out of container, use control + p and then control+q

Running a Command in an Existing Container

  • Usage: docker exec < options > < container ID> < command >
  • Container ID/name should be from a running container
  • Example:

Create a New Image From a Modified Container

  • Usage: docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
    • Create an image of an edited container on local system
  • Example:

Containers in Docker Archive Files

  • Docker allows saving images into single file as tar archives
  • Tar archives contains layers and metadata that make up a Docker container
  • One can convert the tar file into an Apptainer container using the docker-archive bootstrap agent
  • Example:
    apptainer build local_tar.sif docker-archive:local.tar