I'm a little bit confused with the functionnality of named volumes in a docker compose file specifically when it comes to backup/restore my app.
I'm actually testing this dockercompose file :
version: '2'
services:
django:
build:
context: "{{ build_dir }}/docker/django"
depends_on:
- db
environment:
[...]
volumes:
- code:/data/code
- www:/var/www
- conf:/data/conf
networks:
- front
- db
expose:
- "8080"
entrypoint: "/init"
db:
build:
context: "{{ build_dir }}/docker/postgres"
environment:
[...]
volumes:
- data:/var/lib/postgresql/data
networks:
- db
volumes:
data:
www:
code:
conf:
networks:
front:
external:
name: "proxy_nw"
As the documentation said, I tried to use named volume instead of data only container. But how am I suppose to backup my data ?
With a data only container I would have done a docker run --rm --volume-from DOC backup_container save
which is really easy.
Now I read in this topic that I should use something like docker run --rm --volume data --volume www --volume code --volume conf backup_container save
. This is not so simple because I have many applications with different types and names of volumes so it means that my command to save my data would have to be different for each application. It complicate automation process.
Edit: Actually this syntaxe
docker run --volume data --volume www container_image my_command
is not correct.
It needs the mountpoint inside the container, so it would be
docker run --volume data:/somewhere --volume www:/somewhereelse container_image my_command
.
So it's even more complicated to use with a backup container.
So, what are the best practices in this case ? Should I use just one named volume for all my containers ?
Here is an example of how I backup and restore mongo volume data as docker image.
And restoring.
Actually it should be done same way as written in official documentation. Data volume container stores it's data in "virtual root", so you should backup with next command:
where:
Get data back into the volume(restore):
where:
I finally changed my approach. I parse the volumes of my containers looking for the mountpoints and I backup everything in a separate folder in the tar.
So I'm not using a container to do it but an external script. I don't know if it's a better approach but it works well.