This commit is contained in:
Roger Rutishauser 2024-11-20 17:11:25 +01:00
parent 0a7972a6d7
commit 53d63af068

View File

@ -6,25 +6,21 @@ include_toc: true
Installation: See separate page [Docker Installation](docker-install.md) Installation: See separate page [Docker Installation](docker-install.md)
## Summary ## Summary
Docker simplifies the development and deployment of applications by providing a lightweight, portable, and consistent containerized environment. It bridges the gap between development and production, enabling developers to focus on building applications without worrying about environment-specific issues. The applications run consistently across different computing environments, whether on a developer's laptop, a test server, or in production. Docker simplifies the development and deployment of applications by providing a lightweight, portable, and consistent containerized environment. It bridges the gap between development and production, enabling developers to focus on building applications without worrying about environment-specific issues. The applications run consistently across different computing environments, whether on a developer's laptop, a test server, or in production.
## Advantages of Docker ## Advantages of Docker
- __Portability:__ Containers ensure applications behave the same regardless of the environment (development, testing, production). - __Portability:__ Containers ensure applications behave the same regardless of the environment (development, testing, production).
- __Efficiency:__ Containers use shared OS resources, making them faster and less resource-intensive compared to VMs.
- __Scalability:__ Docker enables rapid scaling of applications by spinning up multiple container instances as needed.
- __Isolation:__ Each container runs independently, preventing conflicts between applications.
Efficiency:
Containers use shared OS resources, making them faster and less resource-intensive compared to VMs.
Scalability:
Docker enables rapid scaling of applications by spinning up multiple container instances as needed.
Isolation:
Each container runs independently, preventing conflicts between applications.
## Key Concepts of Docker ## Key Concepts of Docker
@ -75,6 +71,7 @@ Docker provides volumes for persistent storage, ensuring data remains even if a
Tools like Docker Compose and Kubernetes are used to manage and scale multiple containers in production environments. Tools like Docker Compose and Kubernetes are used to manage and scale multiple containers in production environments.
## Workflow example ## Workflow example
1. Write a Dockerfile to package the application. 1. Write a Dockerfile to package the application.
@ -82,23 +79,29 @@ Tools like Docker Compose and Kubernetes are used to manage and scale multiple c
3. Run the image as a container using docker run. 3. Run the image as a container using docker run.
4. Use Docker Compose to manage multiple containers for a complete application (e.g., web server + database). 4. Use Docker Compose to manage multiple containers for a complete application (e.g., web server + database).
---
## Docker Image
# Docker Image
Docker images are the building blocks for containers. An image is a static snapshot of an environment that contains all necessary dependencies for an application. Docker images are the building blocks for containers. An image is a static snapshot of an environment that contains all necessary dependencies for an application.
Images are created using a `Dockerfile` and can be stored and shared via a Docker registry like Docker Hub. Images are created using a `Dockerfile` and can be stored and shared via a Docker registry like Docker Hub.
### Dockerfile
## Dockerfile
`Dockerifle` ist eine einfache Textdatei, mit der man eigene Images bauen kann. Sie basieren immer auf einem bestehenden base Image (z.B. nginx:latest). Mit `docker build` wird das image erstellt, bevor man es mit `docker run` starten kann. `Dockerifle` ist eine einfache Textdatei, mit der man eigene Images bauen kann. Sie basieren immer auf einem bestehenden base Image (z.B. nginx:latest). Mit `docker build` wird das image erstellt, bevor man es mit `docker run` starten kann.
### Building Image
## Building Image
Im Ordner wo das Dockerfile liegt, ausführen: `docker build -t node-app:1.0 .`, wobei `node-app` ein x-beliebiger Name ist für das image, und anschl. die Version. Dann starten mit `docker run -d -p 80:3000 node-app:1.0` wenn man es auf Port 80 von aussen laufen lassen will. Im Ordner wo das Dockerfile liegt, ausführen: `docker build -t node-app:1.0 .`, wobei `node-app` ein x-beliebiger Name ist für das image, und anschl. die Version. Dann starten mit `docker run -d -p 80:3000 node-app:1.0` wenn man es auf Port 80 von aussen laufen lassen will.
Dockerfile Doku unter https://docs.docker.com/reference/builder Dockerfile Doku unter https://docs.docker.com/reference/builder
## Docker Hub ## Docker Hub
hier gibt es vorgefertigte Images. hier gibt es vorgefertigte Images.
@ -109,156 +112,21 @@ $ docker pull
etc. etc.
``` ```
---
## Docker Container ## Commands
Ein Container ist ein Image, welches gerade ausgeführt wird. Wenn ein Image mit `docker run nginx` ausgeführt wird, spricht man von einem Container. Es ist vergleichbar mit einem Prozess. Container wird auf Basis eines Ausgangs-Images gestartet. ### Anzeigen aller Images
## Docker Volumes
There are three volume types:
1. Docker volumes which are stored and handled internally by docker (c.f. docker config to choose where they are actually stored).
```
version: '3.9'
services:
caddy:
image: caddy:2.6.2
volumes:
- caddy_data:/data
volumes:
caddy_data
```
2. Bind mounts which are direct access to the host file system from a container
```
version: '3.9'
services:
caddy:
image: caddy:2.6.2
volumes:
- /opt/docuteam/ssl/certifcate.pem:/cert.pem:ro</code>
3. Bind mounts of remote share which are defined through docker volumes
<code>version: '3.9'
services:
fedora:
image: docker.cloudsmith.io/docuteam/docker/fcrepo:6.2.0
volumes:
- fedora_data:/fcrepo_home
volumes:
fedora_data:
driver_opts:
type: cifs
device: //remote-hostname.com/path/to/share/fedora
o: addr=remote-hostname.com,username=user,password=mysuperpassword,nodev,noexec,nosuid,vers=2.1,uid=1000,gid=1000
```
---
# Docker Befehle
## Anzeigen aller Container
```
sudo docker ps -a
```
Nur laufende:
```
sudo docker ps
```
## Anzeigen aller Images
``` ```
sudo docker images sudo docker images
``` ```
## Ausgabe eines Containers anzeigen
``` # Docker Container
docker logs <docker id>
```
## Docker logs Ein Container ist ein Image, welches gerade ausgeführt wird. Wenn ein Image mit `docker run nginx` ausgeführt wird, spricht man von einem Container. Es ist vergleichbar mit einem Prozess. Container wird auf Basis eines Ausgangs-Images gestartet.
```
journalctl -xu docker.service
```
## Container starten
```
docker run --name Test_run ubuntu:20.04
```
## Container stoppen / neu starten
```
docker stop
docker restart
```
## Befehl in Docker Container ausführen
Z.B. MySQL, wobei `wordpress-baumfreunde_db_1` der Container-Name ist, den man mit `docker ps` herausfindet.
```
sudo docker exec -it wordpress-baumfreunde_db_1 mysql -uroot -p
```
## Import DB in docker container
```
sudo docker exec -i wp_db mysql -h 172.17.0.1 -P 3306 --protocol=tcp -uroot -p wp_baum < /var/www/wordpress-from-docker/wp_baum.sql
```
## Backup DB in docker container
```
docker exec -it wordpress-baumfreunde_db_1 mysqldump --add-drop-table -uroot -pXXX wp_baum > /home/roru/wordpress-baumfreunde/wp_baum_backup.sql
```
## Bash in container
```
sudo docker exec it <container-name> /bin/bash
# Alpine
sudo docker exec it <container-name> /bin/sh
# als root
docker exec -u root -it <container-name> /bin/bash
```
## Copying files/folders
### Copy file from host to docker
```
sudo docker cp "file.txt" c30c199ec89c:/home/actions
```
### Copy folder from docker to host
```
sudo docker cp "c30c199ec89c:/home/actions/conf /home/rogrut
```
## IP des Containers auslesen
```
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name
```
## Container entfernen
Zum Container entfernen: `docker stop Test_run` und `docker rm Test_run`.
## Daten Teilen ## Daten Teilen
@ -293,6 +161,8 @@ $ exit
Die Datei ist jetzt im Datencontainer unter `/data/db`. Der Datencontainer muss gar nicht gestartet werden um ihn zu verwenden. Die Datei ist jetzt im Datencontainer unter `/data/db`. Der Datencontainer muss gar nicht gestartet werden um ihn zu verwenden.
## Container verlinken ## Container verlinken
### Ports verbinden ### Ports verbinden
@ -347,7 +217,103 @@ $ docker run -it -P --link mongodb:mongo ubuntu:20.04 /bin/bash
``` ```
## Docker compose
## Commands
### Anzeigen aller Container
```
sudo docker ps -a
```
Nur laufende:
```
sudo docker ps
```
### Ausgabe eines Containers anzeigen
```
docker logs <docker id>
```
Docker Logs generell:
```
journalctl -xu docker.service
```
### Container starten
```
docker run --name Test_run ubuntu:20.04
```
### Container stoppen / neu starten
```
docker stop
docker restart
```
### Befehl in Docker Container ausführen
Z.B. MySQL, wobei `wordpress-baumfreunde_db_1` der Container-Name ist, den man mit `docker ps` herausfindet.
```
sudo docker exec -it wordpress-baumfreunde_db_1 mysql -uroot -p
```
### Import DB in docker container
```
sudo docker exec -i wp_db mysql -h 172.17.0.1 -P 3306 --protocol=tcp -uroot -p wp_baum < /var/www/wordpress-from-docker/wp_baum.sql
```
### Backup DB in docker container
```
docker exec -it wordpress-baumfreunde_db_1 mysqldump --add-drop-table -uroot -pXXX wp_baum > /home/roru/wordpress-baumfreunde/wp_baum_backup.sql
```
### Bash in container
```
sudo docker exec it <container-name> /bin/bash
# Alpine
sudo docker exec it <container-name> /bin/sh
# als root
docker exec -u root -it <container-name> /bin/bash
```
### Copy file from host to docker container
```
sudo docker cp "file.txt" c30c199ec89c:/home/actions
```
### Copy folder from docker container to host
```
sudo docker cp "c30c199ec89c:/home/actions/conf /home/rogrut
```
### Get IP of docker container
```
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name
```
### Remove docker container
`docker stop Test_run` und `docker rm Test_run`.
# Docker compose
- __Purpose:__ Defines and manages multi-container Docker applications. - __Purpose:__ Defines and manages multi-container Docker applications.
- __Usage:__ Orchestrates multiple services (containers), networks, and volumes for an application. - __Usage:__ Orchestrates multiple services (containers), networks, and volumes for an application.
@ -359,7 +325,9 @@ $ docker run -it -P --link mongodb:mongo ubuntu:20.04 /bin/bash
`docker-compose.yml` is the file which includes all nescessary information. It can include multiple services like web (built from a Dockerfile) and db (pulled from Docker Hub). `docker-compose.yml` is the file which includes all nescessary information. It can include multiple services like web (built from a Dockerfile) and db (pulled from Docker Hub).
### Image Location
## Image Location
``` ```
services: services:
@ -367,25 +335,25 @@ services:
image: 'jc21/nginx-proxy-manager:latest' image: 'jc21/nginx-proxy-manager:latest'
``` ```
#### Docker Hub: ### Docker Hub:
By default, Docker pulls images from Docker Hub, the default public registry for Docker images. By default, Docker pulls images from Docker Hub, the default public registry for Docker images.
In the example `image: 'jc21/nginx-proxy-manager:latest'` Docker will search for the image jc21/nginx-proxy-manager on Docker Hub and pull the latest tag (or version). In the example `image: 'jc21/nginx-proxy-manager:latest'` Docker will search for the image jc21/nginx-proxy-manager on Docker Hub and pull the latest tag (or version).
#### Other Registries: ### Other Registries:
If the image is hosted on a different container registry (e.g., Amazon Elastic Container Registry, Google Container Registry, or a private registry), you must provide the full registry URL as a prefix, like e.g. `image: 'myregistry.example.com/myimage:latest'`. Docker will pull the image from myregistry.example.com. If the image is hosted on a different container registry (e.g., Amazon Elastic Container Registry, Google Container Registry, or a private registry), you must provide the full registry URL as a prefix, like e.g. `image: 'myregistry.example.com/myimage:latest'`. Docker will pull the image from myregistry.example.com.
#### local cache ### local cache
Before attempting to download the image, Docker checks if the image already exists locally. If found, it uses the local copy. Before attempting to download the image, Docker checks if the image already exists locally. If found, it uses the local copy.
#### Authentication ### Authentication
If the registry requires authentication, you must log in using `docker login <registry_url>` or configure credentials in the Docker Compose file. If the registry requires authentication, you must log in using `docker login <registry_url>` or configure credentials in the Docker Compose file.
#### Local Image ### Local Image
Don't use `image`, but `build`. Don't use `image`, but `build`.
@ -423,18 +391,64 @@ services:
image: redis:latest image: redis:latest
``` ```
### Commands
#### Start
## Docker Volumes
There are three volume types:
1. Docker volumes which are stored and handled internally by docker (c.f. docker config to choose where they are actually stored).
```
version: '3.9'
services:
caddy:
image: caddy:2.6.2
volumes:
- caddy_data:/data
volumes:
caddy_data
```
2. Bind mounts which are direct access to the host file system from a container
```
version: '3.9'
services:
caddy:
image: caddy:2.6.2
volumes:
- /opt/docuteam/ssl/certifcate.pem:/cert.pem:ro</code>
3. Bind mounts of remote share which are defined through docker volumes
<code>version: '3.9'
services:
fedora:
image: docker.cloudsmith.io/docuteam/docker/fcrepo:6.2.0
volumes:
- fedora_data:/fcrepo_home
volumes:
fedora_data:
driver_opts:
type: cifs
device: //remote-hostname.com/path/to/share/fedora
o: addr=remote-hostname.com,username=user,password=mysuperpassword,nodev,noexec,nosuid,vers=2.1,uid=1000,gid=1000
```
## Commands
### Start
Ins Verzeichnis gehen wo docker-compose.yml liegt, und dann `docker-compose start -d`. Mit `-d` wird es im Hintergrund ausgeführt. Ins Verzeichnis gehen wo docker-compose.yml liegt, und dann `docker-compose start -d`. Mit `-d` wird es im Hintergrund ausgeführt.
#### Stop ### Stop
- The `docker-compose stop` command will stop your containers, but it wont remove them. - The `docker-compose stop` command will stop your containers, but it wont remove them.
- The `docker-compose down` command will stop your containers, but it also removes the stopped containers as well as any networks that were created. - The `docker-compose down` command will stop your containers, but it also removes the stopped containers as well as any networks that were created.
- You can take down 1 step further and add the `-v` flag to remove all volumes too. This is great for doing a full blown reset on your environment by running `docker-compose down -v`. - You can take down 1 step further and add the `-v` flag to remove all volumes too. This is great for doing a full blown reset on your environment by running `docker-compose down -v`.
#### Events ### Events
`docker compose events` `docker compose events`