Introduction
If you are like me, you have probably gotten used to using containers. Everywhere. Containers are fast, easy to manage and allow you to host software much more easily, specially if you want to use the same VPS for more than one thing. In today's guide, you will learn how to easily export and import volumes.
Exporting
At your VPS, check the name of your docker volume. To do so, you can list them with
docker volume ls
The output will be similar to
Now take the container name, and replace it in the command below:
docker run -it --rm -v VOLUMENAME:/data -v $(pwd):/backup ubuntu tar -czf /backup/backup.tar.gz -C /data ./
This command will mount your VOLUMENAME into a new container and then it will export the content of the volume to the file backup.tar.gz. This might seem hard to understand, but just keep in mind that volumes are just like folders. You can mount any folder at your container at the volume, so that folder inside the container will hve the content of the volume. Once the export is done, you will have the backup.tar.gz file.
Importing
To import the container, you can run the following command:
docker run -it --rm -v VOLUMENAME:/data -v $(pwd):/backup ubuntu tar -xzf /backup/backup.tar.gz -C /data
Make sure that you are in the same folder of backup.tar.gz and that your volume already exists on the new VPS. Create your volume first, do not let the container create it automatically or the import may fail since files would be merged.
To create a new volume, you canse
docker volume create <volumename>
That is it for the import.
Bonus
If you are using docker compose, you may need to allow your container to run with an external volume setting. To allow that, just add "external: true" in your volume:
Make sure that you are adding two spaces to ident; otherwise it won't work.
Conclusion
That is it! I wish you have a wonderful day. If you want to support with Crypto, wallets are here.