Added Wiki for Linux Proxmox and div. Software-installs

This commit is contained in:
ZennDev1337 2023-12-21 13:00:56 +01:00
parent d47f1dc602
commit 741584446c
17 changed files with 4537 additions and 0 deletions

View file

@ -0,0 +1,197 @@
# How To Install Docker Compose on Linux
How do I install Docker Compose on Ubuntu 22.04/20.04/18.04 | Debian 11/10 | CentOS 8 | Fedora 36/35/34/33/32?. This guide will show you how to Install the Latest Docker Compose on Linux. Compose is a tool for defining and running multi-container Docker application. A YAML file is used to configure your application's services.
This post aims to be a concise instructional step-by-step guide for developers and SysAdmins seeking to setup Docker Compose on Linux. We will check the Github API releases page for the project, and pull the latest binary file.
## How To Install Docker Compose on Linux
You need curl and wget installed on your system for this operation. And definitely, access to the Terminal as a user with sudo privileges.
```
### CentOS / RHEL ###
sudo yum -y install curl wget
### Debian / Ubuntu ###
sudo apt update
sudo apt install -y curl wget
### Fedora ###
sudo dnf -y install curl wget
```
Once curl has been installed, download the latest Compose on your Linux machine.
```
curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url | grep docker-compose-linux-x86_64 | cut -d '"' -f 4 | wget -qi -
```
Make the binary file executable.
```
chmod +x docker-compose-linux-x86_64
```
Move the file to your PATH.
```
sudo mv docker-compose-linux-x86_64 /usr/local/bin/docker-compose
```
Confirm version.
```
$ docker-compose version
Docker Compose version v2.11.2
```
Add user to docker group:
```
sudo usermod -aG docker $USER
newgrp docker
```
## Configure Compose Command-line completion
Compose has [command completion](https://en.wikipedia.org/wiki/Command-line_completion) for the bash and zsh shell.
## For Bash users
Place the completion script in `/etc/bash_completion.d`/.
```
sudo curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/bash/docker-compose -o /etc/bash_completion.d/docker-compose
```
Source the file or re-login to enjoy completion feature.
```
source /etc/bash_completion.d/docker-compose
```
## For Zsh users
Download the completion script in your `~/.zsh/completion/`
```
mkdir -p ~/.zsh/completion
curl -L https://raw.githubusercontent.com/docker/compose/master/contrib/completion/zsh/_docker-compose > ~/.zsh/completion/_docker-compose
```
Include the directory in your `$fpath` by adding in `~/.zshrc`:
```
fpath=(~/.zsh/completion $fpath)
```
Make sure `compinit` is loaded or do it by adding in `~/.zshrc`:
```
autoload -Uz compinit && compinit -i
```
Then reload your shell:
```
exec $SHELL -l
```
## Test Docker Compose installation.
Our comprehensive guide is on [Managing Docker Containers with Docker Compose](https://computingforgeeks.com/managing-docker-containers-with-docker-compose/)
Create a test Docker Compose file.
```
vim docker-compose.yml
```
Add below data to the file.
```
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
links:
- php
php:
image: php:7-fpm
```
Start service containers.
```
$ docker-compose up -d
Creating network "root_default" with the default driver
Pulling php (php:7-fpm)...
7-fpm: Pulling from library/php
b4d181a07f80: Pull complete
78b85dd8f014: Pull complete
8589b26a90be: Pull complete
f5af5d641946: Pull complete
4611dfe4969e: Pull complete
8d335174dcfe: Pull complete
e4ac2aba3855: Pull complete
1ec2992b064f: Pull complete
d0702e432261: Pull complete
508ceaa40a86: Pull complete
Digest: sha256:c5132fe8a019128a89bcc157f5e2b8544ea3078fb9aba076bfe27486f34b0fb5
Status: Downloaded newer image for php:7-fpm
Pulling web (nginx:latest)...
latest: Pulling from library/nginx
b4d181a07f80: Already exists
edb81c9bc1f5: Pull complete
b21fed559b9f: Pull complete
03e6a2452751: Pull complete
b82f7f888feb: Pull complete
5430e98eba64: Pull complete
Digest: sha256:47ae43cdfc7064d28800bc42e79a429540c7c80168e8c8952778c0d5af1c09db
Status: Downloaded newer image for nginx:latest
Creating root_php_1 ... done
Creating root_web_1 ... done
```
Show running Containers
```
$ docker-compose ps
Name Command State Ports
------------------------------------------------------------------------------------------
root_php_1 docker-php-entrypoint php-fpm Up 9000/tcp
root_web_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:8080->80/tcp,:::8080->80/tcp
```
Destroy containers
```
$ docker-compose stop
Stopping root_web_1 ... done
Stopping root_php_1 ... done
$ docker-compose rm -f
Going to remove root_web_1, root_php_1
Removing root_web_1 ... done
Removing root_php_1 ... done
```
Go through Official[ Docker documentation](https://docs.docker.com/) and [Docker Compose documentation](https://docs.docker.com/compose/) to learn more.
### Best Docker Learning Courses:
- [Docker Mastery: with Kubernetes +Swarm from a Docker Captain](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fdocker-mastery%2F)
- [Docker for the Absolute Beginner -- Hands On -- DevOps](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Flearn-docker%2F)
- [Docker and Kubernetes: The Complete Guide](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fdocker-and-kubernetes-the-complete-guide%2F)
- [Docker -- SWARM -- Hands-on -- DevOps](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Flearn-docker-advanced%2F)
- [Docker Swarm Mastery: DevOps Style Cluster Orchestration](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fdocker-swarm-mastery%2F)
### More guides:
- [Ctop -- Top command for container metrics](https://computingforgeeks.com/top-command-for-container-metrics/)
- [How to Install Portainer Docker UI manager](https://computingforgeeks.com/install-docker-ui-manager-portainer/)
- [How To run Local Kubernetes clusters in Docker](https://computingforgeeks.com/how-to-run-local-kubernetes-clusters-in-docker/)
- [Deploy Lightweight Kubernetes with MicroK8s and Snap](https://computingforgeeks.com/deploy-lightweight-kubernetes-with-microk8s-and-snap/)
- [How to run Minikube on KVM](https://computingforgeeks.com/how-to-run-minikube-on-kvm/)

View file

@ -0,0 +1,787 @@
Was ist Docker?\
Docker ist eine Plattform zum Erstellen und Ausführen von Anwendungen in Containern. Container sind leichte und portable Pakete, die alles enthalten, was zur Ausführung der Anwendung benötigt wird. Docker stellt Werkzeuge zum Erstellen, Verwalten und Bereitstellen von Containern bereit und bietet Vorteile wie Konsistenz, Portabilität, Effizienz und Sicherheit.
# Installation
## Linux
[Debian (Engine)](https://docs.docker.com/engine/install/debian/)\
[Ubuntu (Desktop)](https://docs.docker.com/desktop/install/ubuntu/)\
[Fedora (Desktop)](https://docs.docker.com/desktop/install/fedora/)\
[Arch-based distro's (Desktop)](https://docs.docker.com/desktop/install/archlinux/)
## Andere
[Windows](https://docs.docker.com/desktop/install/windows-install/)\
[Mac](https://docs.docker.com/desktop/install/mac-install/)
## Nach der Installation
Es kann sein, dass Sie als nicht-root-Benutzer nicht die Berechtigungen haben, Docker zu bearbeiten und auszuführen. Bitte beachten Sie, dass die folgenden Schritte für Debian/Ubuntu gelten. Hier sind die Schritte, um dies zu ändern:
Falls Sie keinen nicht-root-Benutzer erstellt haben, führen Sie Folgendes aus:
```
sudo useradd -s /bin/bash -m <username>
```
Um dem gerade erstellten Benutzer die Berechtigung für sudo zu geben, navigieren Sie zu /etc/sudoers und bearbeiten Sie die Datei mit nano oder vim. Es wird empfohlen, visudo zu verwenden, da es die Syntax überprüft, um Fehler zu vermeiden. In diesem Beispiel verwende ich nano wie folgt:
```
sudo nano /etc/sudoers
```
Fügen Sie am Ende der Datei Folgendes hinzu:
```
<username> ALL=(ALL:ALL) ALL
```
Stellen Sie anschließend sicher, dass Sie sich als root abmelden und sich als der neue Benutzer anmelden. Versuchen Sie etwas mit sudo auszuführen. In diesem Beispiel habe ich Debian wie folgt aktualisiert:
```
sudo apt update
```
Wenn Sie keine Fehlermeldungen erhalten haben, waren die obigen Schritte erfolgreich. Jetzt müssen wir den Benutzer zur Docker-Gruppe hinzufügen, damit wir die Operationen in Docker durchführen können, ohne dem Benutzer vollen Zugriff auf das System zu geben.
> Hinweis\
> Führen Sie Docker nicht mit Root-Rechten aus.
Überprüfen Sie, ob die Docker-Gruppe bereits erstellt wurde:
```
getent group docker
```
Die Ausgabe sollte ungefähr wie folgt aussehen:
```
docker:x:975
```
Wenn das Terminal keine Ausgabe liefert, erstellen Sie eine neue Gruppe namens "docker":
```
sudo groupadd docker
```
Fügen Sie nun Ihren Benutzer zur Gruppe hinzu und aktivieren Sie die Änderungen sofort:
```
sudo usermod -aG docker $USER
newgrp docker
```
Um zu testen, ob alles korrekt konfiguriert wurde, geben Sie Folgendes ein:
```
docker run --name test hello-world
```
# Werkzeuge für die Entwicklung
## Visual Studio
[Visual Studio Remote WSL](https://learn.microsoft.com/en-us/cpp/linux/connect-to-your-remote-linux-computer?view=msvc-170)\
[Microsoft Docker Extension for VS Code](https://code.visualstudio.com/docs/containers/overview)\
[VS with Docker Development Tools](https://learn.microsoft.com/en-us/visualstudio/containers/overview?view=vs-2022)
## Eclipse
[Doclipser](https://github.com/domeide/doclipser)
## GitLab & Docker
[GitLab Docker images](https://docs.gitlab.com/ee/install/docker.html)
## Applications
| Platform | Name | Link | Comment |
| -------- | ---------- | --------------------------------------------------- | ------------------------------ |
| Linux | Whaler | [Link](https://github.com/sdv43/whaler) | Flatpak |
| Terminal | Lazydocker | [Link](https://github.com/jesseduffield/lazydocker) | \ |
| |
| Docker | Portainer | [Link](https://www.portainer.io/) | Web-UI |
| Docker | Rancher | [Link](https://github.com/rancher/rancher) | Web-UI + better for Kubernetes |
# Nützliche Befehle und Tipps
Um die vollständige Docker CLI-Dokumentation zu sehen, folgen Sie bitte diesem [Link](https://docs.docker.com/engine/reference/commandline/cli/).Dort finden Sie Anleitungen für jeden Docker-Befehl, den Sie ausführen können, einschließlich Themen wie Docker Compose-Dateien, Dockerfiles, APIs und vielem mehr.
Standardmäßig zeigt der Befehl "docker ps" nur die laufenden Container an und nicht die beendeten. Mit dem folgenden Alias, den Sie in Ihrer .bashrc-Datei hinzufügen können, können Sie alle Container anzeigen und nur die wichtigen Informationen sehen:
```
alias dockerps='docker container ls -a --format "table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.State}}"'
```
Dieser Alias definiert den Befehl "dockerps", der die Option "-a" verwendet, um alle Container anzuzeigen, und das Format "table", um nur die Spalten für Image, Namen, Ports und Zustand anzuzeigen. Sie können dann einfach "dockerps" ausführen, um eine übersichtliche Liste aller Container und ihrer relevanten Informationen anzuzeigen.
Dies kann besonders praktisch sein, wenn Sie einen schnellen Überblick über alle Container auf Ihrem System benötigen.
## Curl
Der Curl-Befehl ist ein Befehlszeilentool, das verwendet wird, um Daten von oder zu einem Server zu übertragen. Es unterstützt verschiedene Protokolle wie HTTP, HTTPS, FTP, FTPS und mehr. Der Curl-Befehl ist auf den meisten Unix-basierten Betriebssystemen verfügbar und kann für eine Vielzahl von Aufgaben verwendet werden, wie das Herunterladen von Dateien, das Hochladen von Daten, das Testen von APIs und mehr. Hier sind einige Anwendungsbeispiele und Optionen für diesen Befehl:
```
# download the file and save it in the current directory:
curl -O http://example.com/myfile.txt
# download the file and save it in the current directory but under a different name:
curl -o mynewfile.txt http://example.com/myfile.txt
# for more options run folling:
curl --help
```
Mit diesen Beispielen können Sie eine Datei von einer bestimmten URL herunterladen und im aktuellen Verzeichnis speichern. Sie können auch den Dateinamen ändern, unter dem die heruntergeladene Datei gespeichert wird. Weitere Optionen und Funktionen des Curl-Befehls finden Sie in der Dokumentation oder indem Sie den Befehl "curl --help" ausführen.
# Images
Ein Docker-Image ist ein ausführbares Paket, das alles enthält, was benötigt wird, um eine Software auszuführen. Es umfasst den Code, die Laufzeitumgebung, Systemwerkzeuge, Bibliotheken und Einstellungen. Docker-Images werden aus Dockerfiles erstellt. Wenn Sie den Befehl "docker images" ausführen, sehen Sie folgende Informationen:
- Repository: The name of the repository from which the image was pulled.
- Tag: The specific version of the image.
- Image ID: A unique identifier for the image.
- Created: The date and time at which the image was created.
- Size: The size of the image in bytes.
## Docker hub
Um alle verfügbaren Container anzuzeigen, besuchen Sie [hub.docker.com](https://hub.docker.com/)\
oder suchen Sie nach den Containern über das Terminal mit:
```
docker search
```
## Herunterladen (Pull)
Um ein Image herunterzuladen, führen Sie den folgenden Befehl aus:
```
docker pull <repository>
```
Wenn Sie nicht angegeben haben, welche Version heruntergeladen werden soll, wird standardmäßig die ":latest"-Version heruntergeladen. Um eine spezifische Version des Containers herunterzuladen, verwenden Sie:
```
docker pull <repository>:version
```
Beispiel: Um den Nginx-Webserver herunterzuladen, der auf Alpine Linux läuft:
```
docker pull linuxserver/nginx
```
## Anzeigen
Um alle Images anzuzeigen, verwenden Sie den Befehl:
```
docker images
```
## Löschen
Um ein Image zu löschen, verwenden Sie den Befehl:
```
docker image rm <container1> # Ein einzelnes Container-Image entfernen
docker image rm <container1>:1.04 # Eine spezifische Version eines Images entfernen
docker image rm <container1> <container2> # Mehrere Container-Images entfernen
```
> **Hinweis**\
> Löschen Sie kein Image, das von einem laufenden Container verwendet wird.
## Bereinigen (Prune)
Das Prune-Kommando löscht nicht verwendete Images. Verwenden Sie es mit Vorsicht.
> **Verwenden Sie auf eigene Gefahr**
```
docker image prune
```
# Volumes
Sie können ein Volume erstellen, um Daten zu persistieren und zwischen Containern und dem Host zu teilen. Ein Volume ist ein Verzeichnis, das außerhalb des Dateisystems des Containers gespeichert wird und von Docker verwaltet wird. Nachdem das Volume erstellt wurde, erstellt Docker ein neues Verzeichnis, das Sie in einem Container einbinden können.
> **Hinweis**\
> Sie müssen kein Container-Volume erstellen, wenn der Container nur einmal verwendet wird.
## Erstellen
Um ein Volume zu erstellen, verwenden Sie den folgenden Befehl:
```
docker volume create <volume-name>
```
## Inspektion & Auflistung
Um alle Volumes anzuzeigen oder Informationen zu einem bestimmten Volume anzuzeigen, verwenden Sie die folgenden Befehle:
```
docker volume ls # Alle Volumes auflisten
docker volume inspect <volume-name> # Informationen zu einem Volume anzeigen
```
## Löschen von Volumes
Um ein bestimmtes Volume zu löschen, verwenden Sie den folgenden Befehl:
```
docker volume rm <container1> # Ein einzelnes Volume entfernen
docker volume rm <container1> <container2> # Mehrere Volumes entfernen
```
Um alle Volumes zu löschen, die von keinem Container verwendet werden, verwenden Sie den Befehl:
```
docker volume prune
```
> **Verwenden Sie auf eigene Gefahr**
# docker run
Der Befehl "docker run" ist ein All-in-One-Tool. Wenn Sie diesen Befehl ausführen, wird das Image heruntergeladen (falls noch nicht vorhanden), der Container erstellt und gestartet. Der Befehl "docker run" besteht aus den folgenden Schritten:
```
docker image pull <image>
docker container create <container-name>
docker container start <container-name>
```
Es ist ratsam, Docker-Container im Hintergrund auszuführen (detached). Das bedeutet, dass Sie die Protokolle nicht sehen und nicht in Echtzeit mit dem Container interagieren können. Um einen Container im Hintergrund auszuführen, fügen Sie einfach **_-d_** hinzu, z.B.:
```
docker run -d --name <container-name> <image>
```
Um den Port anzugeben, verwenden Sie den folgenden Befehl:
```
docker run -p <port-host>:<port-container> --name <container-name> <image>
```
Um weitere Optionen anzuzeigen, verwenden Sie den Befehl:
```
docker run --help
```
> **Hinweis**
>
> - Es spielt keine Rolle, welche Optionen und in welcher Reihenfolge sie angegeben werden.
> - Wenn Sie weitere Optionen hinzufügen möchten, ist es besser, dies mit Docker Compose zu tun, da es einfacher ist, eine Datei anstatt eines Befehls zu bearbeiten.
Hier ist ein Beispiel für den Befehl **_docker run_**:
```
docker run -d\
--name=calibre-web\
-e PUID=1000\
-e PGID=1000\
-e TZ=Etc/UTC\
-e DOCKER_MODS=linuxserver/mods:universal-calibre `#optional`\
-e OAUTHLIB_RELAX_TOKEN_SCOPE=1 `#optional`\
-p 8083:8083\
-v /PATH/TO/DATA:/config\
-v /PATH/TO/CALIBRE/LIBRARY:/books\
--restart unless-stopped\
lscr.io/linuxserver/calibre-web:latest
```
# Docker-Container
Der Befehl "docker container" bietet eine Reihe von Unterbefehlen zum Erstellen, Starten, Stoppen und Löschen von Containern sowie zur Überprüfung und Verwaltung ihrer Konfiguration, Protokolle und Leistung. Hier sind einige wichtige Befehle:
## List
Mit diesem Befehl können Sie alle Container auflisten. Sie können auch Filter verwenden, um die Standardansicht anzupassen. Dies kann am besten mit einem Alias in Linux erfolgen.
```
docker container ls # Alle laufenden Container auflisten
docker container ls -a # Alle Container auflisten
```
## Create
Dieser Befehl erstellt den Container, ohne ihn zu starten. Hier ist ein Beispiel:
```
docker container create --name my_container -p 8080:80 nginx:latest
```
## Start & Stop, Kill
Mit diesen Befehlen können Sie den Container starten und stoppen. Mit dem Befehl **_docker container kill_** können Sie den Container abrupt beenden. Beachten Sie jedoch, dass dabei keine Aufräumarbeiten im Container durchgeführt werden. Verwenden Sie **_kill_** daher mit Vorsicht.
```
docker container start <container1> <container2> # Einen oder mehrere Container starten
docker container stop <container1> <container2> # Einen oder mehrere Container stoppen
docker container kill <container1> <container2> # Vorsicht: Container abrupt beenden
```
## Pause & Unpause
Diese Befehle sind nützlich, um die Prozesse eines Containers vorübergehend anzuhalten, ohne den Container zu stoppen oder zu löschen. Dadurch können Wartungs- oder Problembehandlungsaufgaben durchgeführt werden, während der Zustand des Containers erhalten bleibt.
```
docker container ls #list all running container
docker container ls -a #list all container
```
## Rename
Um den Namen eines Containers zu ändern, verwenden Sie den folgenden Befehl:
```
docker container <container_name_old> <container_name_new>
```
## Exec
Mit diesem Befehl können Sie Befehle im Container ausführen und die Ausgabe anzeigen. Hier sind einige Beispiele:
- Um den Inhalt des Home-Verzeichnisses anzuzeigen:
```
docker container exec my_container ls /home
```
- Um eine Verbindung zum Terminal des Containers herzustellen:
```
docker container exec -it <container> bash
```
## Logs
Dieser Befehl ermöglicht es Ihnen, die Protokolle des Containers anzuzeigen. Beachten Sie, dass dieser Befehl nur funktioniert, wenn der Docker-Container mit dem "json-file" oder "journald" Protokollierungs-Treiber gestartet wurde. Wenn in der "daemon.json"-Konfigurationsdatei keine spezifische Protokollierungskonfiguration angegeben ist, ist "json-file" der Standardtreiber. Die "daemon.json"-Datei befindet sich unter "/etc/docker". Verwenden Sie den folgenden Befehl, um die Protokolle des Containers anzuzeigen. Sie können auch weitere Optionen verwenden, um die Ausgabe anzupassen:
```
docker container logs <container> # Vollständiges Protokoll anzeigen
docker container logs --details <container> # Alle Protokolldetails anzeigen
docker container logs -f <CONTAINER_NAME> # Echtzeitprotokoll anzeigen
docker container logs --tail <COUNT> <CONTAINER_NAME> # Ausgabe begrenzen
```
Zusätzlich stehen die Optionen --since und --until zur Verfügung. Um weitere Informationen zu erhalten, führen Sie den folgenden Befehl aus:
```
docker container logs --help
```
## Alternative
> Die meisten häufig verwendeten Befehle sind bereits in Docker integriert. Zum Beispiel können Sie anstelle von **_docker container start_** den Befehl **_docker start_** verwenden
# Docker-File
Mit einem Dockerfile können Sie ein Container-Image erstellen. In den folgenden Beispielen verwenden wir ein bereits vorhandenes Container-Image und passen es an unsere Bedürfnisse an. Schauen wir uns an, aus welchen Teilen ein Dockerfile besteht:
| Command | Alternative | Explanation |
| ------- | ----------- | ------------------------------------------------------------------------------------------------------------------------- |
| FROM | - | Gibt das Basis-Image an |
| WORKDIR | - | Legt das Arbeitsverzeichnis fest |
| COPY | ADD | Kopiert Dateien vom Hostsystem in den Container |
| RUN | - | Führt Befehle im Container aus, um Pakete zu installieren, das System zu aktualisieren oder die Umgebung zu konfigurieren |
| EXPOSE | - | Gibt an, auf welchen Ports der Container hören soll |
| CMD | ENTRYPOINT | Definiert den Standardbefehl, der beim Starten des Containers ausgeführt werden soll |
## Wichtig
CMD und RUN sind nicht dasselbe. RUN wird verwendet, um Befehle im Container während des Build-Prozesses auszuführen. Dies wird normalerweise verwendet, um etwas einmalig auszuführen, z.B. das Aktualisieren und Installieren von Paketen. Wenn Sie mehrere Dinge installieren müssen, führen Sie nicht mehrere RUN-Befehle aus. Verwenden Sie stattdessen den folgenden Syntax:
```
RUN apt update && apt upgrade && apt install python3
```
Achten Sie darauf, den richtigen Port für die webbasierte Anwendung freizugeben.
## 1\. Beispiel: Node.js-Anwendung
```
# Verwenden Sie ein offizielles Node.js-Laufzeitimage als Basisimage
FROM node:14-alpine
# Setzen Sie das Arbeitsverzeichnis im Container
WORKDIR /app
# Kopieren Sie die package.json- und package-lock.json-Dateien in den Container
COPY package*.json ./
# Installieren Sie die Abhängigkeiten
RUN npm install
# Kopieren Sie den Anwendungscode in den Container
COPY . .
# Exponieren Sie den Port 3000 nach außen
EXPOSE 3000
# Definieren Sie den Befehl zum Starten der Anwendung beim Starten des Containers
CMD [ "npm", "start" ]
```
## 2\. Beispiel: Wordpress
```
# Verwenden Sie ein offizielles PHP-Laufzeitimage als Basisimage
FROM php:7.4-apache
# Setzen Sie das Arbeitsverzeichnis im Container
WORKDIR /var/www/html
# Kopieren Sie die WordPress-Dateien in den Container
COPY . .
# Setzen Sie den Besitzer der WordPress-Dateien auf den Webserver-Benutzer
RUN chown -R www-data:www-data .
# Installieren Sie die erforderlichen PHP-Erweiterungen für WordPress
RUN docker-php-ext-install mysqli pdo_mysql
# Exponieren Sie den Port 80 nach außen
EXPOSE 80
# Definieren Sie den Befehl zum Starten des Apache-Webservers
CMD [ "apache2-foreground" ]
```
## 3\. Beispiel: Apache Webserver
```
# Verwenden Sie ein offizielles Apache-Laufzeitimage als Basisimage
FROM httpd:2.4-alpine
# Kopieren Sie die benutzerdefinierte Konfigurationsdatei in den Container
COPY httpd.conf /usr/local/apache2/conf/httpd.conf
# Exponieren Sie den Port 80 nach außen
EXPOSE 80
# Definieren Sie den Befehl zum Starten des Apache-Webservers
CMD [ "httpd-foreground" ]
```
## Erstellen und Veröffentlichen des Containers
Nachdem Sie das Dockerfile erstellt haben, um den Docker-Container auszuführen, müssen Sie das Image erstellen und veröffentlichen. Es gibt zwei Möglichkeiten, es zu veröffentlichen: lokal oder im Docker-Register (Docker Hub).\
Um das Image lokal zu erstellen und zu veröffentlichen:
```
docker build -t <myimage>
docker push <myimage>
```
Um das Image im Docker Hub zu erstellen und zu veröffentlichen:
```
# Falls noch nicht angemeldet, über das Terminal anmelden
docker login
# Sie können auch zuvor die Anmeldeinformationen angeben, z.B.:
docker login -u <benutzername> -p <passwort>
docker build -t <meinimage>
docker push -t <benutzername>/<meinimage>
```
Es besteht auch die Möglichkeit, das Image unter einer anderen Version zu veröffentlichen. Verwenden Sie dazu folgendes Format:
```
# Lokal
docker push <meinimage>:v1.0
# Docker Hub
docker push <benutzername>/<meinimage>:v1.0
```
Beachten Sie, dass, wenn Sie keine Image-Version angeben, standardmäßig immer die Version **_:latest_** verwendet wird.
# docker compose
Dieses Tool wird verwendet, um einzelne oder mehrere Docker-Anwendungen mit mehreren Containern zu definieren und auszuführen. Es ermöglicht Ihnen, eine Reihe von Diensten mit jeweils eigener Konfiguration zu definieren und sie mit einem einzigen Befehl zu starten und zu stoppen. Docker Compose verwendet eine YAML-Datei, um die Dienste, Netzwerke und Volumes für die Anwendung zu definieren.\
Der beste Weg, Anwendungen mit Docker Compose zu installieren, besteht darin, die vom Herausgeber bereitgestellte Konfigurationsdatei zu bearbeiten.
## Installation
Um Docker Compose zu installieren, folgen Sie diesem [Link](https://docs.docker.com/compose/install/)\
Um zu überprüfen, ob die Installation erfolgreich war, führen Sie den folgenden Befehl aus:
```
docker-compose --version
```
Um den Workflow zu optimieren, habe ich für jede Anwendung und Gruppe ein Verzeichnis erstellt und die YAML-Datei und die Verzeichnisse für die Volumes darin platziert. Zum Beispiel:
```
mkdir calibre-web
cd calibre-web
mkdir books config
nano docker-compose.yaml
```
## Bereitstellung
Die folgenden Befehle sind wichtig, um die compose.yaml auszuführen:
```
docker-compose up # ähnlich wie docker run / docker container start
docker-compose down # stoppt den Container-Stack
```
### Einzelner Container
Zunächst schauen wir uns an, wie man einen einzelnen Container erstellt. In diesem Beispiel verwenden wir calibre-web. Auf der Website hat der Herausgeber folgendes gepostet:
```
version: "2.1"
services:
calibre-web:
image: lscr.io/linuxserver/calibre-web:latest
container_name: calibre-web
environment:
- PUID=1000
- PGID=1000
- TZ=Etc/UTC
- DOCKER_MODS=linuxserver/mods:universal-calibre #optional
- OAUTHLIB_RELAX_TOKEN_SCOPE=1 #optional
volumes:
- /path/to/data:/config
- /path/to/calibre/library:/books
ports:
- 8083:8083
restart: unless-stopped
```
Wir sehen, dass der Herausgeber die Konfiguration für die Bereitstellung bereits festgelegt hat. Das Einzige, was wir jetzt tun müssen, ist die Pfade zu den Volumes und falls gewünscht den Port anzupassen. Nach der Anpassung könnte das docker-compose.yaml-File wie folgt aussehen:
```
version: "2.1"
services:
calibre-web:
image: lscr.io/linuxserver/calibre-web:latest
container_name: calibre-web
environment:
- PUID=1000
- PGID=1000
- TZ=Etc/UTC
volumes:
- /home/admoin/callibre-web/config:/config
- /home/admoin/callibre-web/books:/books
ports:
- 80:8083
restart: unless-stopped
```
### multiple container
Um mehrere Container bereitzustellen, habe ich ein Beispiel mit Wordpress erstellt. Wie oben können Sie die Volumes-Pfade und andere Einstellungen anpassen. In diesem Beispiel verwende ich Wordpress.\
Erstellen Sie zunächst das Verzeichnis und die **_.yaml_**-file:
```
mkdir ~/wordpress && cd ~/wordpress
mkdir /html /database
nano docker-compose.yaml
```
Fügen Sie nun den folgenden Inhalt in die Datei ein, aber denken Sie daran, die Dateipfade für Ihren Benutzernamen, den DB-Benutzer und das Passwort sowie die IP-Adresse anzupassen:
```
wordpress_wordpress:
image: wordpress
links:
- mariadb:mysql
environment:
- WORDPRESS_DB_PASSWORD=password
- WORDPRESS_DB_USER=root
ports:
- "public_ip:80:80"
volumes:
- /home/admoin/wordpress/html:/var/www/html
wordpress_mariadb:
image: mariadb
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=wordpress
volumes:
- /home/admoin/wordpress/database:/var/lib/mysql
```
> Für weitere Informationen zu Docker Compose folgen Sie diesem [Link](https://docs.docker.com/compose/compose-file/)
# Netzwerk
Dieser Befehl ermöglicht das Erstellen und Verwalten von Netzwerken.\
Standardmäßig erstellt Docker drei integrierte Netzwerke:
- _default bridge_
- _host_
- _none_
Folgende Netzwerke werden von Docker nicht standardmäßig erstellt werden, aber Sie können sie erstellen:
- _user-defined bridge_
- _macvlan (L2, L3)_
- _ipvlan_
Es gibt auch zwei weitere Optionen, die jedoch veraltet oder nicht häufig verwendet werden:
- _docker swarm_
- _overlay_
Hier sind die Befehle zum Erstellen und Verwalten von Netzwerken:
```
docker network create <network-type> # Erstellt das Netzwerk
docker network rm <network-name> # Entfernt das Netzwerk
docker network ls # Listet alle Netzwerke auf
docker network inspect <network-name> # Zeigt detaillierte Informationen an
docker network connect <network-name> <container-name> # Verbindet den Container mit einem Netzwerk
docker network disconnect <network-name> <container-name> # Trennt den Container vom Netzwerk
```
Von der Docker-Hostmaschine aus gibt es zwei sehr nützliche Befehle bei der Arbeit mit Docker:
```
ip address show
ifconfig # Ähnlich wie der obige Befehl
bridge link
```
> **Note**
>
> - Führen Sie --help für weitere Optionen aus oder verwenden Sie die [docker docs](https://docs.docker.com/engine/reference/commandline/network/) um vollständige Informationen zu jedem Docker-Befehl anzuzeigen.
> - Hilfreiches YouTube-Video [Link](https://www.youtube.com/watch?v=bKFMS5C4CG0)\
> **_Bitte beachten Sie, dass NetworkChuck Docker nicht korrekt installiert hat und jeden Container mit Root-Rechten ausführt. Das liegt an dem Inhalt. Das bedeutet auch, dass jeder Container volle Root-Rechte hat._**
## default bridge
Dieses Netzwerk erstellt ein virtuelles Netzwerk auf dem Host und verbindet den Container mit dem Netzwerk. Dieses Netzwerk ermöglicht es mehreren Containern, miteinander zu kommunizieren, erfordert jedoch, dass Sie Portweiterleitungen konfigurieren, um den externen Zugriff auf die Container zu ermöglichen. Die standardmäßige Docker-Bridge hat einen IP-Adressbereich. Der Docker-Host würde die Adresse **_172.17.0.1/16_** erhalten. Alle Docker-Container werden mit diesem Netzwerk verbunden, sofern kein anderes Netzwerk angegeben wurde.
## host
In diesem Netzwerkmodus kann ein Container den Netzwerkstack des Hosts verwenden. Wenn ein Container im host-Netzwerkmodus gestartet wird, teilt er denselben Netzwerknamespace wie der Host und kann dieselben Schnittstellen und Dienste wie der Host nutzen. Die Container kommunizieren über die MAC-Adresse des Hosts und erhalten eine eigene IP-Adresse.
> **_Bitte beachten Sie, dass diese Art der Netzwerkkommunikation nicht isoliert ist, was bedeutet, dass Ihr Docker-Container frei mit anderen Geräten oder Maschinen im Netzwerk kommunizieren kann_**
Führen Sie die folgenden Befehle aus, um einen Container in diesem Netzwerkmodus auszuführen:
```
docker run --name <container-name> --network host nginx
```
## none
Wie der Name schon sagt, deaktiviert dieses Netzwerk die Netzwerkkonnektivität für einen Container. Wenn Sie einen Container im none-Netzwerkmodus starten, hat der Container keinen Zugriff auf Netzwerkschnittstellen und kann nicht mit anderen Containern oder externen Netzwerken kommunizieren. Dieser Modus ist nützlich, um Aufgaben auszuführen, die keine Netzwerkkonnektivität erfordern.
```
docker run --name websrv-test --network host nginx
```
## Benutzerdefinierte Bridge
Dieses Netzwerk funktioniert ähnlich wie die Standard-Bridge. Der Unterschied besteht darin, dass ein neues virtuelles Netzwerk erstellt wird, in dem Sie Informationen wie IP-Adresse, Bereich, Gateway usw. festlegen können. Dieses Netzwerk verbessert die Leistung, da der Datenverkehr zwischen Containern im selben Netzwerk nicht über eine NAT-Firewall geroutet werden muss. Diese Netzwerke bieten eine flexible und anpassbare Möglichkeit zur Verwaltung der Container-Netzwerkkommunikation und sind ein nützliches Werkzeug zum Aufbau skalierbarer und zuverlässiger verteilter Anwendungen.
Um dieses Netzwerk zu erstellen, verwenden Sie den folgenden Befehl:
```
docker network create <network-name>
```
Dieser Befehl erstellt nicht nur ein neues Netzwerk, sondern legt auch einen neuen IP-Bereich, Subnetz, Gateway usw. fest. Die neue Netzwerk-ID wäre standardmäßig **_172.18.0.0/16_**, wenn sie nicht spezifiziert wird.
## macvlan
Dieser Netzwerktreiber ermöglicht es Ihnen, eine Netzwerkschnittstelle in einem Container mit einer eindeutigen MAC-Adresse zu erstellen, sodass der Container wie ein separates Gerät direkt mit dem physischen Netzwerk verbunden erscheint. Jeder Container erhält seine eigene MAC-Adresse.\
MACvlan hat einige Modi:
- **bridge**\
_In diesem Modus wird jeder Container mit einem physischen Netzwerk gebridged und der Docker-Host routet den Datenverkehr zwischen dem physischen Netzwerk und den Containern._
- **passthru**\
_Dieser Modus gibt die Netzwerkkarte an die Container weiter._
- **private**\
_In diesem Modus kann jeder Container mit anderen Geräten kommunizieren, jedoch nicht mit Containern im selben Netzwerk._
Standardmäßig wird bei Erstellung des macvlan-Netzwerks ohne Angabe des Modus der **bridge**-Modus verwendet.\
Erstellen wir ein macvlan-Netzwerk:
```
# bridge mode
sudo docker network create -d macvlan\
--subnet 192.168.0.0/24\
--gateway 191.168.0.1\
-o parent=enp0s3.0\
mynetwork-bridge
# passthru mode
docker network create -d macvlan\
--subnet=192.168.1.0/24\
--gateway=192.168.1.1\
--aux-address="mode=passthru"\
-o parent=enp0s3.1\
mynetwork-passthru
# private mode
docker network create -d macvlan\
--subnet=192.168.2.0/24\
--gateway=192.168.2.1\
--aux-address="mode=private"\
-o parent=enp0s3.2\
mynetwork-private
```
> **Hinweis**\
> In diesem Netzwerk können Sie auch die Sub-Schnittstelle angeben.
## ipvlan
This network allows you to create a network interface that shares the same MAC address as the parent interface, but has a separate and unique IP address. It provides a way to create multiple containers with their own IP addresses, while using the same physical or virtual network interface.
### L2
Dieses Netzwerk ermöglicht es Ihnen, eine Netzwerkschnittstelle zu erstellen, die dieselbe MAC-Adresse wie die übergeordnete Schnittstelle teilt, jedoch eine separate und eindeutige IP-Adresse besitzt. Es bietet eine Möglichkeit, mehrere Container mit ihren eigenen IP-Adressen zu erstellen, während dieselbe physische oder virtuelle Netzwerkschnittstelle verwendet wird.
```
docker network create -d ipvlan\
--subnet 10.7.1.0/24\
--gateway 10.7.1.3\
-o parent=enp0s3\
ip-vlan_l2
```
### L3
In diesem Modus hat jeder Container seine eigene IP-Adresse, teilt jedoch dieselbe MAC-Adresse wie die übergeordnete Schnittstelle. Die Container kommunizieren mit anderen Geräten im Netzwerk über IP-Routing.
```
docker network create -d ipvlan\
--subnet 192.168.94.0/24\
-o parent=enp0s3 -o ipvlan_mode=L3\
--subnet 192.168.95.0/24\
ip-vlan_l3
```
> Um den Container in der Lage zu machen, mit dem globalen Netzwerk zu kommunizieren, müssen Sie eine statische Route erstellen.
### L2bridge
Dies ist ein Hybridmodus, der die Aspekte der L2- und L3-Modi kombiniert. Jeder Container hat seine eigene IP-Adresse und teilt dieselbe MAC-Adresse wie die übergeordnete Schnittstelle. Container innerhalb dieses Netzwerks können über IP-Routing mit anderen Geräten im Netzwerk kommunizieren und dabei Layer-2-Bridging verwenden.
```
docker network create -d ipvlan\
--subnet 192.168.101.0/24\
-o parent=enp0s3 -o ipvlan_mode=mode=l2bridge\
ip-vlan_l2bridge
```
## weitere Netzwerke
Es gibt zwei weitere Netzwerkoptionen: das Overlay-Netzwerk und das Docker-Swarm. Diese werden jedoch nicht häufig verwendet und/oder sind veraltet. Wenn Sie erfahren möchten, wie Docker Swarm funktioniert, empfehle ich Ihnen, sich mit Kubernetes zu befassen, da diese Art der Containerisierung mehr Optionen bietet.

View file

@ -0,0 +1,788 @@
# What is Docker?
Docker is a platform for building and running applications inside containers. Containers are lightweight and portable packages that include everything needed to run the application. Docker provides tools for building, managing, and deploying containers, and it offers benefits such as consistency, portability, efficiency, and security.
# Installation
## Linux
[Debian (Engine)](https://docs.docker.com/engine/install/debian/)\
[Ubuntu (Desktop)](https://docs.docker.com/desktop/install/ubuntu/)\
[Fedora (Desktop)](https://docs.docker.com/desktop/install/fedora/)\
[Arch-based distro's (Desktop)](https://docs.docker.com/desktop/install/archlinux/)
## Other
[Windows](https://docs.docker.com/desktop/install/windows-install/)\
[Mac](https://docs.docker.com/desktop/install/mac-install/)
## Post-install
You may do not have the rights as a non-root user to edit and deploy docker. Please note that the steps below work for Debian/ Ubuntu. Here are the steps to change it:\
If you didn't create a non-root user run following:
```
sudo useradd -s /bin/bash -m <username>
```
To give the user you have just created the right for sudo navigate to the /etc/sudoers and edit the file with nano or vim. It is reccomended to use the visudo because the performs synthax checking for preventing errors. In this example I will use the nano as following:
```
sudo nano /etc/sudoers
```
Now add the following to the bottom of the file:
```
<username> ALL=(ALL:ALL) ALL
```
Afterwise be shure that you logout from root and login as the new user.\
Try to perform something with sudo. In this Example I tried to update Debian like this:
```
sudo apt update
```
The steps above were successful if you haven't ancounted any errors. Now we need to add the user to the docker group so that we could perform the operations in docker without giving the full access to the system.
> **Note**\
> Do not run docker with root rights
Check if the docker group has already been created:
```
getent group docker
```
The output should be something like this:
```
docker:x:975
```
If Terminal didn't give any output give create a new group called docker:
```
sudo groupadd docker
```
Now add your user to the group and activate the changes immidiatly:
```
sudo usermod -aG docker $USER
newgrp docker
```
To test if everything was configured correctly enter following:
```
docker run --name test hello-world
```
# Tools for developing
## Visual Studio
[Visual Studio Remote WSL](https://learn.microsoft.com/en-us/cpp/linux/connect-to-your-remote-linux-computer?view=msvc-170)\
[Microsoft Docker Extension for VS Code](https://code.visualstudio.com/docs/containers/overview)\
[VS with Docker Development Tools](https://learn.microsoft.com/en-us/visualstudio/containers/overview?view=vs-2022)
## Eclipse
[Doclipser](https://github.com/domeide/doclipser)
## GitLab & Docker
[GitLab Docker images](https://docs.gitlab.com/ee/install/docker.html)
## Applications
| Platform | Name | Link | Comment |
| -------- | ---------- | --------------------------------------------------- | ------------------------------ |
| Linux | Whaler | [Link](https://github.com/sdv43/whaler) | Flatpak |
| Terminal | Lazydocker | [Link](https://github.com/jesseduffield/lazydocker) | \ |
| |
| Docker | Portainer | [Link](https://www.portainer.io/) | Web-UI |
| Docker | Rancher | [Link](https://github.com/rancher/rancher) | Web-UI + better for Kubernetes |
# Useful
To see the full docker cli documentation follow this [Link](https://docs.docker.com/engine/reference/commandline/cli/)\
There are Guides for every docker command that you can execute including stuff like docker compose-files, docker-files, API's and so on.\
By running the "docker ps" command it doesn't display not running containers. With this alias, that you can put in your .bashrc, you can display all containers and see only the important stuff
```
alias dockerps='docker container ls -a --format "table {{.Image}}\t{{.Names}}\t{{.Ports}}\t{{.State}}"'
```
## Curl
This command is a command-line tool that is used to transfer data from or to a server, using a variety of protocols such as HTTP, HTTPS, FTP, FTPS, and more. The curl command is available on most Unix-based operating systems and can be used to perform a wide range of tasks, such as downloading files, uploading data, testing APIs, and more. Below are some use cases and options for this comand:
```
# download the file and save it in the current directory:
curl -O http://example.com/myfile.txt
# download the file and save it in the current directory but under a different name:
curl -o mynewfile.txt http://example.com/myfile.txt
# for more options run folling:
curl --help
```
# Images
A Docker image is a executable package that contains everything needed to run a piece of software. It includes: the code, runtime, system tools, libraries, and settings. Docker images are created from a docker files. By runnung "docker images" you would see following:
- Repository: The name of the repository from which the image was pulled.
- Tag: The specific version of the image.
- Image ID: A unique identifier for the image.
- Created: The date and time at which the image was created.
- Size: The size of the image in bytes.
## Docker hub
To see all available containers go to [hub.docker.com](https://hub.docker.com/)\
or search for the containers via Terminal with:
```
docker search
```
## Download (pull)
To download the image run following:
```
docker pull <repository>
```
If you haven't specified which version to pull it will pull the ":latest".\
To download a specific version of the container:
```
docker pull <repository>:version
```
For example: to download the nginx webserver that runs alpine-linux underneath:
```
docker pull linuxserver/nginx
```
## View
To view all images run following:
```
docker images
```
## Delete
To delete a image run following:
```
docker image rm <container1> # To remove a single container image
docker image rm <container1>:1.04 # To remove specific version of an image
docker image rm <container1> <container2> # To remove a multiple containers images
```
> **Note**\
> Do not delete an image when used by a running container
## Prune
Prune is like delete but it deletes all images that are not used by any container
> **Use at your own risk**
```
docker image prune
```
# Volumes
You can create a volume to persist data and share data between containers and the host. A volume is a directory that is stored outside of the container's filesystem and is managed by Docker. After creating the volume, docker creates a new directory that you can mount to to a container.
> **Note**\
> You don't need to create a container-volume if the container is a one time use only
## Create
To create a volume paste following:
```
docker volume create <volume-name>
```
## Inspect & List
To view all the containers or view info about a specific container paste following:
```
docker volume ls # list all volumes
docker volume inspect <volume-name> # list all information about a volume
```
## Delete volume(s)
To delete a specific volume run following:
```
docker volume rm <container1> # To remove a single container
docker volume rm <container1> <container2> # To remove a multiple containers
```
To delete all volumes that are not in use by any container:
```
docker volume prune
```
> **Use at your own risk**
# docker run
This command is a All-in-One Tool. By executing this command you will pull the image (if not pulled), create the container and start it. This command consist of:
```
docker image pull <image>
docker container create <container-name>
docker container start <container-name>
```
You should run docker containers detached. Running container detached means you won't see the logs and interact with it in real time) To do so just add **_-d_** like so:
```
docker run -d --name <container-name> <image>
```
To specify the port run following:
```
docker run -p <port-host>:<port-container> --name <container-name> <image>
```
to display more options run following:
```
docker run --help
```
> **Note**
>
> - It does not matter which options and which sequence they are
> - If you would like to add more options then it's better to do it with docker-compose since it is easier to edit a file instead of a command
This is how a **_docker run_** could look like:
```
docker run -d\
--name=calibre-web\
-e PUID=1000\
-e PGID=1000\
-e TZ=Etc/UTC\
-e DOCKER_MODS=linuxserver/mods:universal-calibre `#optional`\
-e OAUTHLIB_RELAX_TOKEN_SCOPE=1 `#optional`\
-p 8083:8083\
-v /PATH/TO/DATA:/config\
-v /PATH/TO/CALIBRE/LIBRARY:/books\
--restart unless-stopped\
lscr.io/linuxserver/calibre-web:latest
```
# docker container
This command provides a set of sub-commands for creating, starting, stopping, and deleting, as well as for inspecting and managing their configuration, logs and performance. Here are some few:
## List
With this command you can list all the container. You can also filter them if you don't like the standard view (best done with alias in linux)
```
docker container ls #list all running container
docker container ls -a #list all container
```
## Create
This command creates the container witout starting it. For example:
```
docker container create --name my_container -p 8080:80 nginx:latest
```
## Start & Stop, Kill
With these commands you can start and stop the container. By using **_docker container kill_** you will pull out the power plug from the container. Please use the **_kill_**-command with care since it the container does no cleanup before. Command as follows:
```
docker container start <container1> <container2> # start one or muliple container
docker container stop <container1> <container2> # stop one or multiple container
docker container kill <container1> <container2> # use with caution. may result errors
```
## Pause & Unpause
Those commands are useful for temporarly suspending a containers processes without stopping or deleting the container, allowing to perform the maintenance or troubleshooting tasks while preserving the container's state
```
docker container ls #list all running container
docker container ls -a #list all container
```
## Rename
To rename a single container paste following:
```
docker container <container_name_old> <container_name_new>
```
## Exec
With this command you can execute commands into the container and get the output.\
Here are some examples:
- to see what's in the home directory:
```
docker container exec my_container ls /home
```
- To make a connection to the containers- terminal
```
docker container exec -it <container> bash
```
## Logs
This command allows you to see logs of the container. This command only works if the docker container was started with with the .json file or journald. If in deamon.json wasn't any specified configuration file then json-file would be the default driver. The deamon.json is located in **/etc/docker**. To see logs of the container run the first command. If you would like to specify the output use one or more commands below:
```
docker container logs <container> # show full log
docker container logs --details <container> # show all details
docker container logs -f <CONTAINER_NAME> # show real time log
docker container logs --tail <COUNT> <CONTAINER_NAME> # to reduce the output
```
Also available options are --since and --until. To know more run following:
```
docker container logs --help
```
## Alternative
> Most used commands are already build into docker. For example:\
> Instead of using the **_docker container start_** you can use the: **_docker start_**
# Docker-File
With a docker-file you can create a container-image. In these following examples we will use a already finished container image and modify it to our needs.\
Let's start with looking out of which parts the docker-file is build:
| Command | Alternative | Explanation |
| ------- | ----------- | ------------------------------------------------------------------------------------------------------ |
| FROM | - | Specifies the base image |
| WORKDIR | - | Sets the working directory |
| COPY | ADD | Copies the Files from host system into the container |
| RUN | - | Executes Command in the container to install packages, update the system, or configure the environment |
| EXPOSE | - | Specifies which ports the container shouls listen on |
| CMD | ENTRYPOINT | Defines the default command to run when the container starts |
## Important
CMD and RUN aren't the same things. RUN is used to execute the commands inside the container during the build prescess. This is typically used to execute something once like update and install packages.\
If you need to install multiple things **do not** run multiple RUN commands. Instead run it like so:
```
RUN apt update && apt upgrade && apt install python3
```
Make sure to expose correct Port for the web-based application.
## 1\. Example: Node.js Applicaton
```
# Use an official Node.js runtime as the base image
FROM node:14-alpine
# Set the working directory inside the container
WORKDIR /app
# Copy the package.json and package-lock.json files into the container
COPY package*.json ./
# Install the dependencies
RUN npm install
# Copy the application code into the container
COPY . .
# Expose port 3000 to the outside world
EXPOSE 3000
# Define the command to run the application when the container starts
CMD [ "npm", "start" ]
```
## 2\. Example: Wordpress
```
# Use an official PHP runtime as the base image
FROM php:7.4-apache
# Set the working directory inside the container
WORKDIR /var/www/html
# Copy the WordPress files into the container
COPY . .
# Set the ownership of the WordPress files to the web server user
RUN chown -R www-data:www-data .
# Install the necessary PHP extensions for WordPress
RUN docker-php-ext-install mysqli pdo_mysql
# Expose port 80 to the outside world
EXPOSE 80
# Define the command to start the Apache web server
CMD [ "apache2-foreground" ]
```
## 3\. Example: Apache Webserver
```
# Use an official Apache runtime as the base image
FROM httpd:2.4-alpine
# Copy the custom configuration file into the container
COPY httpd.conf /usr/local/apache2/conf/httpd.conf
# Expose port 80 to the outside world
EXPOSE 80
# Define the command to start the Apache web server
CMD [ "httpd-foreground" ]
```
## Building and Publishing the container
After the creation of the docker-file to run the docker container you need to build the image and push it. There are two ways of pushing it, locally and in the docker-registy (Docker Hub).\
To build and to publish the image locally:
```
docker build -t <myimage>
docker push <myimage>
```
To build and to publish the image into the docker hub:
```
# if not logged in login via Terminal:
docker login
# you can also specify the login credentials before, like so:
docker login -u <myusername> -p <mypassword>
docker buld -t <myimage>
docker push -t <myusername>/<myimage>
```
There is also the option to push the image under a different version. To do so add following:
```
# locally
docker push <myimage>:v1.0
# docker hub
docker push <myusername>/<myimage>:v1.0
```
Remember, if you don't specify the image it will push always as the **_:latest_**
# docker compose
This tool is used to define and run single-, multi-container Docker applications. It allows you to define a set of services, each with its own configuration, and start and stop them all with a single command. Docker Compose uses a YAML file to define the services, networks, and volumes needed for the application.\
Best way to install applications with docker compose is by editing the configuration file given by the publisher(s).
## install
To install docker compose follow this [Link](https://docs.docker.com/compose/install/)\
To proove that installation was successful run following:
```
docker-compose --version
```
To optimise the workflow I have created for every application and a group a directory and pasted inside the yaml file and the directories for the volumes. For example:
```
mkdir calibre-web
cd calibre-web
mkdir books config
nano docker-compose.yaml
```
## deployment
Following commands are important to run the compose.yaml
```
docker-compose up # is like docker run/ docker container start
docker-compose down # stops the container-stack
```
### single container
First let's look how to build a single container. In this example we will use the calibre-web. On the website publisher have posted following:
```
version: "2.1"
services:
calibre-web:
image: lscr.io/linuxserver/calibre-web:latest
container_name: calibre-web
environment:
- PUID=1000
- PGID=1000
- TZ=Etc/UTC
- DOCKER_MODS=linuxserver/mods:universal-calibre #optional
- OAUTHLIB_RELAX_TOKEN_SCOPE=1 #optional
volumes:
- /path/to/data:/config
- /path/to/calibre/library:/books
ports:
- 8083:8083
restart: unless-stopped
```
We see that the publisher have already specified the configuration for the deployment. The only thing that we have to do now is to customize the paths to the volumes and if we want the port. After the customization the docker-compose.yaml could look like so:
```
version: "2.1"
services:
calibre-web:
image: lscr.io/linuxserver/calibre-web:latest
container_name: calibre-web
environment:
- PUID=1000
- PGID=1000
- TZ=Etc/UTC
volumes:
- /home/admoin/callibre-web/config:/config
- /home/admoin/callibre-web/books:/books
ports:
- 80:8083
restart: unless-stopped
```
### multiple container
To deploy multiple container I have created an wordpress example. As above you can customise the paths volumes and so on. In this example I used wordpress.\
First create the directory and the **_.yaml_**-file:
```
mkdir ~/wordpress && cd ~/wordpress
mkdir /html /database
nano docker-compose.yaml
```
Now paste following into the file but remember to change the filepath to your username, DB user and password and the ip:
```
wordpress_wordpress:
image: wordpress
links:
- mariadb:mysql
environment:
- WORDPRESS_DB_PASSWORD=password
- WORDPRESS_DB_USER=root
ports:
- "public_ip:80:80"
volumes:
- /home/admoin/wordpress/html:/var/www/html
wordpress_mariadb:
image: mariadb
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=wordpress
volumes:
- /home/admoin/wordpress/database:/var/lib/mysql
```
> For more information about docker compose follow this [Link](https://docs.docker.com/compose/compose-file/)
# Network
This command allows you to create and manage networks.\
By default docker creates three build-in networks:\*
- _default bridge_
- _host_
- _none_
Following one's docker doesn't ship but you can create them:
- _user-defined bridge_
- _macvlan (L2, L3)_
- _ipvlan_
Following you also can create but they are outdated or not used:
- _docker swarm_
- _overlay_
These are the commands to create or manage networks:
```
docker network create <network-type> # Creates the network
docker network rm <network-name> # Removes the network
docker network ls # list all networks
docker network inspect <network-name> # See detailed information
docker network connect <network-name> <container-name> # Connects the container to a network
docker network disconnect <network-name> <container-name> # Disconnects the container from a network
```
From the docker host there are two very useful commands when working with docker:
```
ip address show
ifconfig # similar to the command above
bridge link
```
> **Note**
>
> - Run _--help_ for more options or use the [docker docs](https://docs.docker.com/engine/reference/commandline/network/) to see full infromation about every docker command
> - Helpful Youtube video [Link](https://www.youtube.com/watch?v=bKFMS5C4CG0)\
> **_Please note that networkchuck didn't install correctly docker and runs every container with root that's because of the content. That also means that every container has full root rights._**
## default bridge
This network creates a virtual network on the host and connects the container to the network. This network allows multiple containers to communicate with each other but it requires that you configure port mappings to allow external access to the containers. Docker's default bridge has a ip address range. The docker host would get the address **_172.17.0.1/16_**\
All docker container get connected to this network if network hasn't been specified.
## host
This netork allows a container to use the network stack of the host machine. When a container is started in _host_ network mode, it shares the same network namespace as the host, which means that it ca access the same interfaces and services as the host. The containers communicate through the hosts MAC-address and get's it's own ip address.
> **_Please note that this type of network communication is not not isolated, which means that your docker container can communicate freely with other devices or machines in the network_**
Run following commands to run a container in this network:
```
docker run --name <container-name> --network host nginx
```
## none
As the name may already say this network disables networking for a container. When you start a container in _none_ network mode, the container does not have access to any network interfaces and cannot communicate with other containers or external networks. This mode is useful to run and perform useful work.
```
docker run --name websrv-test --network host nginx
```
## user-defined bridge
This network works similiar to the default bridge. The difference between them, is that a new virtual network is created and in this network you can specifiy the information like ip, range, gateway and so on. This network boosts the performance because the traffic between the container on the same network doesn't need to be routed through a NAT firewall. These networks provide a flexible and customizable way to manage container networking and it's useful tool for building scalable and reliable distributed applications.
To create this network run following:
```
docker network create <network-name>
```
This command not only creates a nw network but also sets up a new ip-range, subnet, gateway and so on. The new net-id would be **_172.18.0.0/16_** if not specified
## macvlan
This network driver allows you to create a network interface in a container with a unique MAC aderess, making it appear as if the container is connected directly to the physical network as a separate device. Each container is assigned its own MAC address.\
MACvlan has a few modes:
- **bridge**\
_In this mode each container is bridged to a physical network and the docker host routes traffic between the physical network and the containers_
- **passthru**\
_This mode passes the network card to the containers_
- **private**\
_In this mode every container can communicate with other devices but not to the containers in the same network_
By default when you create the macvlan network without specifying the mode ot will use the **bridge** mode.\
Let's create a macvlan:
```
# bridge mode
sudo docker network create -d macvlan\
--subnet 192.168.0.0/24\
--gateway 191.168.0.1\
-o parent=enp0s3.0\
mynetwork-bridge
# passthru mode
docker network create -d macvlan\
--subnet=192.168.1.0/24\
--gateway=192.168.1.1\
--aux-address="mode=passthru"\
-o parent=enp0s3.1\
mynetwork-passthru
# private mode
docker network create -d macvlan\
--subnet=192.168.2.0/24\
--gateway=192.168.2.1\
--aux-address="mode=private"\
-o parent=enp0s3.2\
mynetwork-private
```
> **Note**\
> In this network you can also specify the sub interface
## ipvlan
This network allows you to create a network interface that shares the same MAC address as the parent interface, but has a separate and unique IP address. It provides a way to create multiple containers with their own IP addresses, while using the same physical or virtual network interface.
### L2
In this mode every container can communicate with ither devices on the network as if the were physically connected. Every container gets its own uniqe IP and MAC address.
```
docker network create -d ipvlan\
--subnet 10.7.1.0/24\
--gateway 10.7.1.3\
-o parent=enp0s3\
ip-vlan_l2
```
### L3
In this mode each container has its own IP address but shares the same MAC address as the parent interface. Containers communcate with other devices on the network using IP routing.
```
docker network create -d ipvlan\
--subnet 192.168.94.0/24\
-o parent=enp0s3 -o ipvlan_mode=L3\
--subnet 192.168.95.0/24\
ip-vlan_l3
```
> To make the container able to talk to the global netwoek you need t create a static route
### L2bridge
This is a hybrid mode that combines the aspects of the L2, L3 modes. Wach container has its own IP address and shares the same MAC address as the parent interface. Containers within this network can communicate with other devices on the network through IP routing while usinf Layer 2 bridging.
```
docker network create -d ipvlan\
--subnet 192.168.101.0/24\
-o parent=enp0s3 -o ipvlan_mode=mode=l2bridge\
ip-vlan_l2bridge
```
## other
There are two more network options: the overlay netowork and the docker swarm. But these are not commonly used and/or are outdated. If you want to learn how the docker swarm works I would reccomend you to look into Kubernetes, because this type of containerisation has more options.

View file

@ -0,0 +1,223 @@
# Install Docker-ce
How do I install Docker CE on Debian 11/10?, How can I install Docker Compose on Debian 11/10 Linux system?. In this guide, I'll discuss a step by step installation of Docker and Docker Compose on Debian 11/10.
Docker is the most popular and widely used container runtime. It enables you to package and run your applications in isolated containers in a single server or cluster of Linux servers orchestrated by Kubernetes and similar tools.
## Docker Editions
There are two editions of Docker available.
- **Community Edition (CE)**: ideal for individual developers and small teams looking to get started with Docker and experimenting with container-based apps.
- **Enterprise Edition (EE)**: Designed for enterprise development and IT teams who build, ship, and run business-critical applications in production at scale.
This guide will cover installation of Docker CE on Debian 10 / Debian 11 Linux. But let's first look at common docker terminologies.
## Docker Components / Terminologies
Below are commonly used terminologies in Docker ecosystem.
- **Docker daemon**: This is also called Docker Engine, it is a background process which runs on the host system responsible for building and running of containers.
- **Docker Client**: This is a command line tool used by the user to interact with the Docker daemon.
- **Docker Image**: An image is an immutable file that's essentially a snapshot of a container. A docker image has a file system and application dependencies required for running applications.
- **Docker container**: This is a running instance of a docker image with an application and its dependencies. Each container has a unique process ID and isolated from other containers. The only thing containers share is the Kernel.
- **Docker registry**: This is an application responsible for managing storage and delivery of Docker container images. It can be private or public.
# Install Docker CE on Debian 11 (Bullseye) / Debian 10 (Buster)
Follow the steps covered in the next parts of this article to install and use Docker CE on Debian 11/10.
## Step 1: Install Dependency packages
Start the installation by ensuring that all the packages used by docker as dependencies are installed.
```
sudo apt update
sudo apt -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common
```
## Step 2: Add Docker's official GPG key:
Import Docker GPG key used for signing Docker packages.
```
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker-archive-keyring.gpg
```
## Step 3: Add the Docker repository to Debian 10 / Debian 11
Add Docker repository which contain the latest stable releases of Docker CE.
```
sudo add-apt-repository\
"deb [arch=amd64] https://download.docker.com/linux/debian\
$(lsb_release -cs)\
stable"
```
This command will add the line shown in `/etc/apt/sources.list` file.
## Step 4: Install Docker & Docker Compose on Debian 11/10
Update the `apt` package index.
```
sudo apt update
```
To install Docker CE on Debian, run the command:
```
sudo apt install docker-ce docker-ce-cli containerd.io -y
```
Start and enable docker service:
```
sudo systemctl enable --now docker
```
This installation will add `docker` group to the system without any users. Add your user account to the group to run docker commands as non-privileged user.
```
sudo usermod -aG docker $USER
newgrp docker
```
Check docker and compose version.
```
$ docker version
Client: Docker Engine - Community
Version: 20.10.17
API version: 1.41
Go version: go1.17.11
Git commit: 100c701
Built: Mon Jun 6 23:03:17 2022
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.17
API version: 1.41 (minimum version 1.12)
Go version: go1.17.11
Git commit: a89b842
Built: Mon Jun 6 23:01:23 2022
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.6.6
GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
runc:
Version: 1.1.2
GitCommit: v1.1.2-0-ga916309
docker-init:
Version: 0.19.0
GitCommit: de40ad0
```
Log out and log back in so that your group membership is re-evaluated.
```
exit
```
### Install Docker Compose on Debian
Use the guide below to install latest Docker Compose on Debian 10 / Debian 11
- [How To Install Latest Docker Compose on Linux](https://computingforgeeks.com/how-to-install-latest-docker-compose-on-linux/)
## Step 5: Test Docker installation
Run a test docker container:
```
$ docker run --rm -it --name test alpine:latest /bin/sh
latest: Pulling from library/alpine
2408cc74d12b: Pull complete
Digest: sha256:686d8c9dfa6f3ccfc8230bc3178d23f84eeaf7e457f36f271ab1acc53015037c
Status: Downloaded newer image for alpine:latest
/ # cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.16.0
PRETTY_NAME="Alpine Linux v3.16"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"
/ # exit
```
## Step 6: Test Docker Compose installation.
Create a test Docker Compose file.
```
vim docker-compose.yml
```
Add below data to the file.
```
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
links:
- php
php:
image: php:7-fpm
```
Start service containers.
```
$ docker-compose up -d
```
Output:
![docker compose start containers](https://computingforgeeks.com/wp-content/uploads/2019/03/docker-compose-start-containers.png?ezimgfmt=rs:696x404/rscb23/ng:webp/ngcb23)
Show running Containers
```
$ docker-compose ps
```
Destroy containers
```
$ docker-compose stop
$ docker-compose rm
Going to remove vagrant_web_1, vagrant_php_1
Are you sure? [yN] y
Removing vagrant_web_1 ... done
Removing vagrant_php_1 ... done
```
You have successfully installed Docker on Debian 10 / Debian 11 Linux. Go through Official[ Docker documentation](https://docs.docker.com/) and [Docker Compose documentation](https://docs.docker.com/compose/) to learn more.
## Setup Docker UI (Optional)
If you need a UI management console for Docker hosts and containers, checkout Portainer.
- [How to Install Portainer Docker UI manager](https://computingforgeeks.com/install-docker-ui-manager-portainer/)
## Monitoring Docker containers
Monitoring Docker containers can be achieved by using Monitoring tools such as [Netdata, Prometheus and Grafana](https://computingforgeeks.com/category/monitoring/). Below guide should also be helpful [Check Container container metrics with Ctop.](https://computingforgeeks.com/top-command-for-container-metrics/)
### Best Docker Learning Courses:
- [Docker Mastery: with Kubernetes +Swarm from a Docker Captain](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fdocker-mastery%2F)
- [Docker for the Absolute Beginner -- Hands On -- DevOps](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Flearn-docker%2F)
- [Docker and Kubernetes: The Complete Guide](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fdocker-and-kubernetes-the-complete-guide%2F)
- [Docker -- SWARM -- Hands-on -- DevOps](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Flearn-docker-advanced%2F)
- [Docker Swarm Mastery: DevOps Style Cluster Orchestration](https://click.linksynergy.com/deeplink?id=2sb89XJC/EQ&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fdocker-swarm-mastery%2F)