Linuxhotel Wiki

Wie ging das nochmal?

Benutzer-Werkzeuge

Webseiten-Werkzeuge


admin_grundlagen:docker:docker_volumes_by_examples

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.

Link zu der Vergleichsansicht

Nächste Überarbeitung
Vorherige Überarbeitung
admin_grundlagen:docker:docker_volumes_by_examples [2026/05/08 11:34] – angelegt peter_rossbach2admin_grundlagen:docker:docker_volumes_by_examples [2026/05/29 14:01] (aktuell) – peter_rossbach2
Zeile 1: Zeile 1:
 ===== Docker volumes by examples ===== ===== Docker volumes by examples =====
  
-==== NFS ====+==== Start nginx with local path ==== 
 + 
 +Create Website content: 
 + 
 +<code bash> 
 +mkdir -p htdocs 
 +cat >htdocs/index.html <<EOF 
 +<!doctype html> 
 +<html lang="en"> 
 +<head> 
 +  <meta charset="utf-8"> 
 +  <title>Docker Local Path Nginx</title> 
 +</head> 
 +<body> 
 +  <h2>Hello from Nginx container of `hostname`</h2> 
 +</body> 
 +</html> 
 +EOF 
 +</code> 
 + 
 +<code bash> 
 + 
 +docker run -d --name nginx-local \ 
 +-v $(pwd)/htdocs:/usr/share/nginx/html nginx 
 + 
 +IPADDRESS=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' nginx-local) 
 +# Alternative with jq 
 +# docker inspect nginx-local| jq -r '.[0].NetworkSettings.Networks.bridge.IPAddress' 
 +curl $IPADDRESS 
 + 
 +</code> 
 + 
 +==== Start nginx with docker volumes ==== 
 + 
 +Prepare docker managed volume: 
 + 
 +<code bash> 
 +docker volume create htdocs-volume 
 + 
 +docker container run \ 
 +    -v $(pwd)/htdocs:/htdocs \ 
 +    -v htdocs-volume:/htdocs1  \ 
 +    -ti --rm alpine /bin/sh 
 +cp htdocs/index.html htdocs1 
 +exit 
 + 
 +</code> 
 + 
 +Use docker managed volume: 
 + 
 +<code bash> 
 +docker run -d --name web-server \ 
 +-v htdocs-volume:/usr/share/nginx/html nginx 
 + 
 +# check content 
 +docker exec web-server ls /usr/share/nginx/html 
 +docker run --rm -ti --volumes-from web-server alpine /bin/sh 
 +ls /usr/share/nginx/html 
 +exit 
 + 
 +</code> 
 + 
 +Inspect volumes: 
 + 
 +<code bash> 
 +docker volume ls 
 +docker volume inspect htdocs-volume 
 + 
 +#docker volume rm htdocs-volume 
 +</code> 
 + 
 +==== Start with mount options and publish nginx ==== 
 + 
 +<code bash> 
 +docker run \ 
 +  --mount type=bind,src=$(pwd)/htdocs,dst=/usr/share/nginx/html,readonly \ 
 +  -p 8080:80 \ 
 +  nginx-public 
 +</code> 
 + 
 +==== Start local mysql ==== 
 + 
 +<code bash> 
 +# hostpath bind mount 
 +docker run --name mysql-db \ 
 +  -v $(pwd)/datadir:/var/lib/mysql 
 +  -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:8.4.9 
 + 
 +# Access DB 
 + 
 +# Stop db 
 +docker stop mysql.db 
 +docker rm mysql-db 
 + 
 +# or docker managed volume /var/lib/docker/volumes 
 +docker volume create mysql-data 
 +docker run --name mysql-db \ 
 +  -v mysql-data:/var/lib/mysql 
 +  -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:8.4.9 
 + 
 +</code> 
 + 
 +++++ NFS | 
 + 
 +  - https://oneuptime.com/blog/post/2026-01-30-docker-nfs-volumes/view 
 +  - https://docs.docker.com/engine/storage/volumes/
  
 Install NFS kernel server Install NFS kernel server
Zeile 30: Zeile 135:
 sudo exportfs -v sudo exportfs -v
 </code> </code>
 +
 +
 +Create docker volume with a NFS Share:
 +
 +<code bash>
 +docker volume create \
 +  --driver local \
 +  --opt type=nfs \
 +  --opt o=addr=192.168.1.100,rw,nfsvers=4.1 \
 +  --opt device=:/exports/shared \
 +  shared-data
 +</code>
 +
 +
 +Use volume inside docker-compose service:
 +
 +<code bash>
 +mkdir -p web
 +cd web
 +cat >docker-compose.yml <<EOF
 +services:
 +  app:
 +    image: nginx:alphine
 +    volumes:
 +      - shared-data:/usr/share/nginx/html
 +    deploy:
 +      replicas: 3
 +
 +  processor:
 +    image: python:3.11-slim
 +    volumes:
 +      - shared-data:/data
 +    command: python -c "while True: pass"
 +
 +volumes:
 +  shared-data:
 +    driver: local
 +    driver_opts:
 +      type: nfs
 +      o: addr=192.168.1.100,rw,nfsvers=4.1,soft,timeo=300
 +      device: ":/exports/shared"
 +EOF
 +docker compose uo -d
 +# access service with curlimages/curl many times
 +docker run --rm --network default-web curlimages/curl curl -s app
 +...
 +# check logs
 +
 +# down
 +docker compose down
 +</code>
 +
 +
 +Use nfs share with docker --mount directly:
 +
 +<code bash>
 +docker run -d \
 +  --name web \
 +  --mount type=volume,volume-driver=local,\
 +volume-opt=type=nfs,\
 +volume-opt=o=addr=192.168.1.100,\
 +volume-opt=device=:/exports/shared,\
 +dst=/usr/share/nginx/html \
 +  nginx:alpine
 +</code>
 +
 +++++
admin_grundlagen/docker/docker_volumes_by_examples.1778240091.txt.gz · Zuletzt geändert: 2026/05/08 11:34 von peter_rossbach2