20 Docker-Installation und Konfiguration

20.1 Einführung

Die Installation und Konfiguration von Docker ist der erste Schritt zur Nutzung der Containerisierungsplattform. Docker kann auf verschiedenen Betriebssystemen installiert werden, darunter Linux, Windows und macOS. In diesem Kapitel wird die Installation und grundlegende Konfiguration von Docker unter Linux beschrieben.

20.2 Installation von Docker

20.2.1 Schritt 1: Voraussetzungen

Bevor Docker installiert wird, sollten einige grundlegende Voraussetzungen erfüllt sein:

20.2.2 Schritt 2: Docker-Repository hinzufügen

Befehl (Debian/Ubuntu):

sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

20.2.3 Schritt 3: Docker-Engine installieren

Befehl (Debian/Ubuntu):

sudo apt-get update
sudo apt-get install -y docker-ce

Überprüfung der Installation:

Befehl:

sudo docker --version

Ausgabe:

Docker version 20.10.7, build f0df350

20.2.4 Schritt 4: Docker-Dienst starten und aktivieren

Befehl:

sudo systemctl start docker
sudo systemctl enable docker

Überprüfung des Dienststatus:

Befehl:

sudo systemctl status docker

Ausgabe:

● docker.service - Docker Application Container Engine
   Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2024-07-05 10:05:17 CEST; 1h 2min ago

20.3 Konfiguration von Docker

20.3.1 Benutzer zu Docker-Gruppe hinzufügen

Um Docker-Befehle ohne sudo ausführen zu können, kann der Benutzer zur Docker-Gruppe hinzugefügt werden.

Befehl:

sudo usermod -aG docker $USER

Hinweis: Nach Ausführung dieses Befehls muss der Benutzer sich ab- und wieder anmelden, damit die Änderung wirksam wird.

20.3.2 Konfigurationsdatei: daemon.json

Die Datei /etc/docker/daemon.json wird zur Konfiguration des Docker-Daemons verwendet. Änderungen an dieser Datei erfordern einen Neustart des Docker-Dienstes.

Beispielkonfiguration:

{
  "log-driver": "json-file",
  "log-level": "warn",
  "storage-driver": "overlay2"
}

Neustarten des Docker-Dienstes:

Befehl:

sudo systemctl restart docker

20.3.3 Docker-Proxy-Konfiguration

Falls Docker hinter einem Proxy betrieben wird, müssen Proxy-Umgebungsvariablen konfiguriert werden.

Beispielkonfiguration:

Datei: /etc/systemd/system/docker.service.d/http-proxy.conf

[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80/"
Environment="HTTPS_PROXY=https://proxy.example.com:443/"

Neustarten des Docker-Dienstes:

Befehl:

sudo systemctl daemon-reload
sudo systemctl restart docker

20.3.4 Docker-Daemon mit bestimmten Optionen starten

Docker-Daemon kann mit zusätzlichen Optionen gestartet werden, um bestimmte Einstellungen zu aktivieren.

Beispiel: Aktivieren des Debug-Modus

Befehl:

sudo dockerd --debug

20.4 Beispiel: Docker Hello World

Nach der Installation und Konfiguration von Docker kann ein einfacher Test durchgeführt werden, um sicherzustellen, dass Docker korrekt funktioniert.

Befehl:

docker run hello-world

Ausgabe:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
478afc919002: Pull complete 
Digest: sha256:94323f3e5e09a8b9515d74337010375a456c909543e1ff1538f5116d38ab3989
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (arm64v8)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/