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.

C:\Users\Chairat\jenkinsdemo>docker run --rm --volumes-from jenkinsdemo -v C:\Users\Chairat\jenkinsdemo:/backup ubuntu tar cvf /backup/jenkins_home.tar /var/jenkins_home

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
C:\Users\Chairat\jenkinsdemo>docker run -v /var/jenkins_home --name jenkinsdata ubuntu /bin/bash

2. Use command docker ps -a you will see the new container has been created.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d6f6b6e82592 ubuntu "/bin/bash" 7 days ago Exited (0) 3 seconds ago jenkinsdata
ad139a52c20e jenkins:1.651.3 "/bin/tini -- /usr/lo" 9 days ago Exited (143) 40 minutes ago jenkinsdemo
612835a9bf7f mariadb:10.1.17 "docker-entrypoint.sh" 9 days ago Exited (0) 40 minutes ago mariadb
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.

C:\Users\Chairat\jenkinsdemo>docker run --rm --volumes-from jenkinsdata -v C:\Users\Chairat\jenkinsdemo:/backup ubuntu bash -c "cd /var && tar xvf /backup/jenkins_home.tar --strip 1"

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
C:\Users\Chairat\jenkinsdemo>docker run -d --volumes-from jenkinsdata --name jenkins2 -p 8081:8080 jenkins:1.651.3
e278421128ace35288e36de382d3902a7d813eb417a0ad3497d6e856e604054b
5. You may remove the unused container:

C:\Users\Chairat\jenkinsdemo>docker rm jenkinsdata
jenkinsdata

3 comments: