Skip to content
SHC Docs

A volume

This page adds a named volume: a directory that belongs to the deployment rather than to a container, so what a service writes there outlives the container. A container is replaced whenever its image or settings change; anything it kept in its own filesystem goes with it.

guestbook gets a counter, counter, a Redis that keeps the number of visits in /data. Start it on Redis 7.2, with data mounted at /data. --appendonly yes makes Redis write every change to disk there.

~/learn/guestbook/compose.yaml
services:
web:
image: nginx:1.27-alpine
expose:
- "80"
volumes:
- ./site:/usr/share/nginx/html:ro
x-shc-primary: true
api:
image: traefik/whoami:v1.10
command: ["--port", "8080", "--name", "guestbook-api"]
expose:
- "8080"
x-shc-path: /api
x-shc-path-strip: true
counter:
image: redis:7.2-alpine
command: ["redis-server", "--appendonly", "yes"]
expose:
- "6379"
volumes:
- data:/data
volumes:
data:
home01
$ shc update guestbook
...

Count three visits.

home01
$ shc exec -s guestbook -S counter -- redis-cli incr visits
1
$ shc exec -s guestbook -S counter -- redis-cli incr visits
2
$ shc exec -s guestbook -S counter -- redis-cli incr visits
3

SHC makes each named volume a directory of the deployment, mounts/<name>, on the node that runs it, and binds it into the container.

home01
$ ls -1 /var/lib/shc/deployments/prod/guestbook/prod/mounts
data
$ ls -1 /var/lib/shc/deployments/prod/guestbook/prod/mounts/data
appendonlydir

Now upgrade Redis to the newest 7. A new image means a new container: the old one is removed with everything in its own filesystem.

~/learn/guestbook/compose.yaml
services:
web:
image: nginx:1.27-alpine
expose:
- "80"
volumes:
- ./site:/usr/share/nginx/html:ro
x-shc-primary: true
api:
image: traefik/whoami:v1.10
command: ["--port", "8080", "--name", "guestbook-api"]
expose:
- "8080"
x-shc-path: /api
x-shc-path-strip: true
counter:
image: redis:7-alpine
command: ["redis-server", "--appendonly", "yes"]
expose:
- "6379"
volumes:
- data:/data
volumes:
data:
home01
$ shc update guestbook
...
$ shc exec -s guestbook -S counter -- redis-server --version
...

The new container read the count from the volume.

home01
$ shc exec -s guestbook -S counter -- redis-cli get visits
3

Next: A healthcheck.