This is a rough draft for backing up and restoring data contained in a Docker volume. I can't say that this is a "best practice". For my limited testing at the moment, it seems sufficient.
version: '3'
services:
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: secret
MYSQL_DATABASE: tacos
MYSQL_USER: tacos
MYSQL_PASSWORD: secret
volumes:
- databasedata:/var/lib/mysql
volumes:
databasedata:
driver: 'local'
To start:
docker-compose up
From the host machine, where DEADBEEF0123
is the ID of the running container.
docker exec -it DEADBEEF0123 bash
In the container:
mysql -u tacos -p
From the host machine:
CONTAINER='DEADBEEF0123'
DIR_TO_BACKUP='/var/lib/mysql'
BACKUP_NAME='backup.tar'
docker run --rm --volumes-from "${CONTAINER}" -v $(pwd):/backup alpine:3.8 tar cvf "/backup/${BACKUP_NAME}" "${DIR_TO_BACKUP}"
From the host machine:
CONTAINER='DEADBEEF0123'
BACKUP_NAME='backup.tar'
docker run --rm --volumes-from "${CONTAINER}" -v $(pwd):/backup alpine:3.8 tar xvf "/backup/${BACKUP_NAME}"