Skip to content

Latest commit

 

History

History
272 lines (199 loc) · 7.9 KB

File metadata and controls

272 lines (199 loc) · 7.9 KB
type slide
slideOptions
transition width height margin
slide
1400
900
0.1
<style> .reveal strong { font-weight: bold; color: orange; } .reveal p { text-align: left; } .reveal section h1 { color: orange; } .reveal section h2 { color: orange; } </style>

Containers with Docker

Image by https://www.docker.com/, Fair use, https://en.wikipedia.org/w/index.php?curid=70663056


What is a Container?

  • Container operates in "fenced off" part of the operating system (namespaces)
  • Lower overhead than a virtual machine
    • Runs on kernel (and libraries) of the host OS
    • Cheap to start and stop a container
  • Available features depend on Host (Linux, Windows)
  • Container can be isolated.

Common Use-Cases

  • Microservices
  • Reproducible environments for developing and testing (DevOps)
  • More and more in science
    • High-performance computing, "Bring Your Own Environment"
    • Reproducible research

Container Solutions

  • Plenty of different container formats
  • Different solutions with different strengths due to different use cases
    • Working on the (Super-)Userspace
    • Direct access to hardware vs. encapsulation
    • Generic or with integration in software ecosystem (e.g. job schedulers)
  • Common standard: Open Container Initiative (OCI)

Docker

  • 2010: Docker Inc. founded
  • 2013: First Docker release
    • Open source, Then based on LXC
  • 2014: Replaced LXC by own execution environment
  • 2017: Moby project for open source development
  • 2023: The most popular container solution (survey)

Building Blocks 1/2

  • Docker objects
    • Images
      • Read-only template for creating a container
      • An image can be based on another image
    • Containers
      • Runnable instance of an image

Building Blocks 2/2

  • Docker daemon dockerd
    • Controlling instance of containers and reacts to API requests
    • Server process
  • Docker client
    • User interface/tools to interact with, create, manage containers etc. via daemon
    • That means no direct interaction with containers, images etc.
  • Docker registries
    • Registries that manage Docker images to be used

Docker Architecture

https://docs.docker.com/get-started/docker-overview/


Connection to Host

  • Container communicates via daemon dockerd (runs as root)
  • Strong isolation (namespaces and cgroups)
    • You cannot access Host filesystem by default.
    • Several mount options available

Requirements

  • Root rights for installation
  • dockerd runs as root -> Interaction needs root rights
    • Option 1: Prefix commands with sudo
    • Option 2: Be member of group docker (=makes you root), expected by some applications (e.g. act)
  • Alternatives:
  • Check security notes

Useful Commands 1/4

  • docker run OPTIONS
    • Run a container
  • docker container ls
    • List running containers
    • Add -a to see also the stopped containers
    • Alias: docker ps

Useful Commands 2/4

  • docker pull NAME:TAG
    • Pulls an image from registry, TAG optional
  • docker container create IMAGE
    • Create container from image
  • docker container start/stop NAME
    • Start/stop container
  • docker container attach NAME
    • Attach to running container

Useful Commands 3/4

  • docker build
    • Creates an image from a given Dockerfile
  • docker cp
    • Copy files in/out of container
  • docker image history IMAGE
    • Show layers of image (including commands)

Useful Commands 4/4

  • docker image ls
    • List locally available images
  • docker system prune
    • Remove all unused objects (images, containers...)

Demo: Running Prebuilt images

Details available in docker_demo.md


Defining and Building own Images 1/2

  • Define container in Dockerfile
    • Git-friendly text file
  • Start from base image
    • Find images on repository such as DockerHub
  • Extend image by additional layers
    • Layers are added separately -> Keep number of layers low
    • Layers are cached
    • Changed layer requires downstream layers to be recreated
  • Container layers have commit hashes

Defining and Building own Images 2/2

  • FROM: Defines base image
  • RUN: Defines commands to execute
  • WORKDIR: Defines working directory for following commands
  • COPY: Copy for from source to destination
  • ADD: Add for from source to destination (powerful and confusing)
  • CMD: Command to run under docker run
  • ENV: Sets environment variable
  • ARG: Environment variable for only the build process

Dockerfile Example

FROM ubuntu:24.04

RUN apt update -y && apt install -y neofetch
WORKDIR /app
COPY testfile .
CMD ["neofetch"]

Demo: Building own Image

Details available in docker_demo.md


Publish own Images

  • Publication on registry (e.g. DockerHub)
  • docker build -t ACCOUNT/REPOSITORY[:TAG] .
    • Creates image
  • docker push ACCOUNT/REPOSITORY[:TAG]
    • Push image to registry (default DockerHub)
    • Needs account and must be logged in via docker login

Advanced Topics


Summary and Outlook

  • Lightweight virtualization technique
  • Run application in isolated environment
  • Run application in consistent environment
  • Share environments and applications with containers
  • Plenty of options and feature-rich CLI
  • Important building block for CI/CD pipelines (future lectures)

Further Reading