I have been using data volume containers to persist data in docker containers. There are various reasons why this tends to be a better option than just using data volumes, but probably the most important is portability.
Of course now we have to backup the data in the data containers. This can be for archiving, or when the containers that use the data need to be upgraded or recreated. If your backup requirements are simple you can simply use the docker cp command or something like tar.
A Jenkins example
As a simple example, let’s run a Jenkins server in a docker container and use a data volume container to persist its data.
1. Pull or build a Jenkins image from the official repository.
2. The Jenkins images uses the directory /var/jenkins_home as the volume to store it’s data, so we need a data volume container for that volume. Here is a sample of a Dockerfile to build the data container:
Build and tag the image from the Dockerfile.
docker build -t your_repository:jenkins-data .
You can now create the data container, giving it a name for convenience. Optionally we can run the docker ps command afterwards to check that the container has been created, it should be in a stopped state.
docker run -i -t --name jenkins-data your_repository:jenkins-data docker ps -a
3. Run the Jenkins server with the data container attached and make some changes, e.g. create a job, etc. The Jenkins data volume should have your changes in it now.
docker run --name=jenkins-sample -p 8080:8080 --volumes-from=jenkins-data jenkins
4. For this example we will use tar to backup the data container, using this command to create a temporary container to access the data container.
docker run -rm --volumes-from jenkins-data -v $(pwd):/backup busybox tar cvf /backup/jenkins_backup.tar /var/jenkins_home
There should now be a file jenkins_backup.tar in the current directory. Of course for real usage, we would probably run this command from a script and make it generic to be able to backup any data volume container.
I do give a fig …
Something else I use for development with Docker is the orchestration tool Fig (this has saved me a lot of typing!). So here is an example of doing the same backup on the Jenkins data container using Fig.
1. Create Fig YAML file, using the same information that we used in the backup command.
2. Run Fig, that’s it!
This is a simple example that has only scratched the surface of what can be done with Docker (and Fig). If the backup requirements for the data is more complex, then you could also consider creating a dedicated container just for doing backups, with all the required tools installed in it.
The great thing about Docker is that once everything has been setup, you can get applications such as Jenkins up and running very quickly.