update docker readme

This commit is contained in:
Roger Rutishauser 2025-04-22 15:14:29 +02:00
parent 521b772188
commit eb69814391

View File

@ -70,55 +70,115 @@ 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.
## Workflow example
1. Write a Dockerfile to package the application.
2. Build the Docker image using docker build.
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).
1. Write a __Dockerfile__ to __package the application__.
2. Build the Docker __image__ using __docker build__.
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).
# 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.
Images are created using a `Dockerfile` and can be stored and shared via a Docker registry like Docker Hub.
__Images can either be built, or existing images can be pulled from a registry.__
## Docker Registry
## Dockerfile
By default, Docker __pulls__ images from __Docker Hub__, the default public registry for Docker images. \
For 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).
`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.
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`.
Before attempting to download the image, Docker checks if the image already exists locally in the __cache__. If found, it uses the local copy.
## 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.
Dockerfile Doku unter https://docs.docker.com/reference/builder
## Docker Hub
hier gibt es vorgefertigte Images.
If the registry requires __authentication__, you must log in using `docker login <registry_url>` or configure credentials in the Docker Compose file.
```
$ docker login
$ docker pull
docker login
docker pull
etc.
```
## Own App: Dockerfile
## Commands
`Dockerfile` ist eine einfache Textdatei, mit der man eigene Images bauen kann. Sie basieren immer auf einem bestehenden base Image (z.B. `nginx:latest` oder `node:16.13.0-alpine`). Mit `docker build` wird das image erstellt, bevor man es mit `docker run` starten kann.
### Anzeigen aller Images
Dockerfile documentation: https://docs.docker.com/reference/builder
## Build Docker image
Im Ordner wo das Dockerfile liegt, ausführen:
```
sudo docker images
docker build .
```
Um dem Image einen Namen und einen Tag zu geben (1.0 ist im folgenden Beispiel der Tag)
```
docker build -t node-app:1.0 .
```
## Run docker image
Image, which was either pulled from a registry or was built on the system, can be run with:
```
docker run -d -p 3000:80 -e FOO='bar' -FOO2='bar2' --name myApp node-app:1.0
```
- `-d` Optional. For detached mode (run in background)
- `-p 3000:80` Optional. Traffic on port 3000, and port 80 inside the container.
- `-e` Optional. Environment Parameter, that will be passed on to the container
- `--name` Optional. Name for container.
- `--restart` Optional. Restart policy. `no`, `on-failure:5`, `always`
- `node-app:1.0` image/tag to be started.
## Show all docker images
```
docker images
docker image ls # alternativ
```
Example:
```
rogrut@zidbacons02:/$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker dind 0f7ea23310b3 3 weeks ago 397MB
docker <none> 7a9eec921ea3 2 months ago 378MB
cr.gitlab.uzh.ch/dba/digicert/export-digicert/main e9af5b08 95248f27c850 2 months ago 261MB
caddy latest 1b7d0a82297a 3 months ago 48.5MB
alpine <none> b0c9d60fc5e3 3 months ago 7.83MB
curlimages/curl latest 7551dbeefe0d 4 months ago 21.8MB
```
## Delete Docker image
```
docker rmi <REPOSITORY:TAG>
docker rmi ubuntu:2010
```
## Add Tag to Docker image
The following will clone the image with a new tag, but has the same IMAGE ID.
```
docker tag <REPOSITORY>:<TAG> <REPOSITORY>:<new tag, e.g. 1.0 instead of latest>
```
## Push Docker image to repository
the following will clone an image and add a tag that can be used to push to a repository. It will then push it to the repo.
```
docker tag myApp:latest myApp:latest repository-example.com/rogerrutishauser/myApp:latest
docker login
docker push
```
@ -127,8 +187,7 @@ sudo docker images
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.
## Daten Teilen
## Share data
### 1. Möglichkeit: Verzeichnisse mappen (Host-Verzeichnis im Container mappen)
@ -218,18 +277,20 @@ $ docker run -it -P --link mongodb:mongo ubuntu:20.04 /bin/bash
## Commands
## Container Commands
### Anzeigen aller Container
```
sudo docker ps -a
docker ps -a
docker container ls -a # alternativ
```
Nur laufende:
```
sudo docker ps
docker ps
docker container ls # alternativ
```
### Ausgabe eines Containers anzeigen
@ -247,14 +308,21 @@ journalctl -xu docker.service
### Container starten
```
docker run --name Test_run ubuntu:20.04
docker run -d -p 3000:80 -e --name <beliebiger_containername> <IMAGE ID>
```
### Container stoppen / neu starten
```
docker stop
docker restart
docker stop <containername>
docker restart <containername>
```
### Container (schneller) stoppen und löschen
```
docker kill <containername>
docker rm <containername>
```
### Befehl in Docker Container ausführen
@ -351,29 +419,25 @@ docker-compose --version
## Image Location
Example:
```
services:
nproxy-app:
image: 'jc21/nginx-proxy-manager:latest'
build: .
```
### Docker Hub:
### Registry
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 docker compose, use `image` to use an existing image from the registry.
### 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.
### local cache
Before attempting to download the image, Docker checks if the image already exists locally. If found, it uses the local copy.
### Authentication
If the registry requires authentication, you must log in using `docker login <registry_url>` or configure credentials in the Docker Compose file.
```
services:
redis:
image: redis:latest
```
### Local Image
@ -411,10 +475,12 @@ services:
redis:
image: redis:latest
webapp:
build: .
image: registry.cs.zi.uzh.ch/zi-adb-dba/export-digicert-report:latest
```
## Docker Volumes
There are three volume types:
@ -457,7 +523,11 @@ volumes:
o: addr=remote-hostname.com,username=user,password=mysuperpassword,nodev,noexec,nosuid,vers=2.1,uid=1000,gid=1000
```
### Show docker volumes
```
docker volume ls
```
## Commands
@ -474,3 +544,14 @@ Ins Verzeichnis gehen wo docker-compose.yml liegt, und dann `docker-compose star
### Events
`docker compose events`
# Allgemeinte Commands
## Aufräumen
Löscht alle Images, Volumes, Netzwerke, die nicht mit mind. 1 Container verbunden sind, sowie den Build Cache.
```
docker system prune --all --volumes
```