Linuxhotel Wiki

Wie ging das nochmal?

Benutzer-Werkzeuge

Webseiten-Werkzeuge


admin_grundlagen:docker:docker_volumes_by_examples

Dies ist eine alte Version des Dokuments!


Docker volumes by examples

Start nginx with local path

Create Website content:

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
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

Start nginx with docker volumes

Prepare volume:

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

Use Volume:

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

Inspect

docker volume ls docker volume inspect htdocs-volume #docker volume rm htdocs-volume
 

Start with mount options and publish nginx

docker run \
  --mount type=bind,src=$(pwd)/htdocs,dst=/usr/share/nginx/html,readonly \
  -p 8080:80 \
  nginx-public

Start local mysql

# 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

++++ NFS |

Install NFS kernel server

sudo apt update
sudo apt install -y nfs-kernel-server
 
# Create the export directory
sudo mkdir -p /exports/shared
sudo chown nobody:nogroup /exports/shared
sudo chmod 755 /exports/shared
 
# add this to /etc/exports
sudo vi /etc/exports
/exports/shared    192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
 
 
# Export the shares
sudo exportfs -ra
 
# Start and enable NFS server
sudo systemctl enable nfs-kernel-server
sudo systemctl start nfs-kernel-server
 
# Verify exports
sudo exportfs -v

Create docker volume with a NFS Share:

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

Use volume inside docker-compose service:

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

Use nfs share with docker –mount directly:

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

++++

admin_grundlagen/docker/docker_volumes_by_examples.1780063144.txt.gz · Zuletzt geändert: (Externe Bearbeitung)