Tuesday, January 3, 2017

How-To Backup and Restore Your Jenkins Data Volume in Docker

Refer to the instructions on this page, you can normally use command docker commit to create your own image from your container and even publish it to Docker Hub so everyone else can reuse your image.

However, Jenkins images declares the jenkins home directory (/var/jenkins_home) as a volume (See reference) so you cannot use the command docker commit to create the image as your changes made to Jenkins won't go with the image. You have to make a backup of your data volume instead (refer instructions on this page).

Here are the instructions to backup your Jenkins data from your Jenkins container (recently created from this blog), and how-to restore it:

Backup Jenkins Data Volume

1. Make sure your local drive is shared to your containers by selecting the drive you want to keep your back up file in Docker Settings. (You may need to enter your Windows account password)

2. Use command docker run --rm --volumes-from <container> -v <local_path>:<container_path> ubuntu tar cvf <container_path>/jenkins_home.tar /var/jenkins_home to backup Jenkins data volume into a TAR file.

3. Once complete, you will get a TAR file in your <local_path>

Now you can send this backup file to whoever you want.

Restore Jenkins Data Volume

1. Use command docker run -v /var/jenkins_home --name jenkinsdata ubuntu /bin/bash to create a new container with a new data volume

2. Use command docker ps -a you will see the new container has been created.
3. Use command docker run --rm --volumes-from jenkinsdata -v <local_path>:<container_path> ubuntu bash -c "cd /var && tar xvf <container_path>/jenkins_home.tar --strip 1" to un-tar from the backup file.

Now, you have the Jenkins data volume restored from backup.

4. You can create a new Jenkins container using the restored data volume by using command docker run -d --volumes-from jenkinsdata --name jenkins2 -p 8081:8080 jenkins:1.651.3
5. You may remove the unused container: