If a Docker container uses a volume, you need to export the volume’s data, because it is not a bind mount
- Suppose we first create a mongo container
docker run -p 27017:27017 –name mongo -v mongo-data:/data -d mongo:4.4
mongo-data is a volume mapped to /data, because mongo stores its data under data by default, including db and the configuration files
- Export (backup): create a new ubuntu container that mounts the same volume as the container to be exported
docker run –rm –volumes-from mongo -v d:/backup:/backup ubuntu tar cvf /backup/backup.tar /data/
- Import (restore): create a new ubuntu container that mounts the same volume as the exported container, then load the previously exported content back into the container
docker run –rm –volumes-from mongo -v d:/backup:/backup ubuntu bash -c “cd /data/ && tar xvf /backup/backup.tar –strip 1”