|
| 1 | +# Migrating Jenkins Container from One Host To Another |
| 2 | + |
| 3 | +## Caution |
| 4 | + |
| 5 | +- Identify the Jenkins version you are using, it should be the same image on the new host |
| 6 | +- Both server should be able to communicate with each other usin SSH |
| 7 | + |
| 8 | +## Identity Jenkins Home Directory |
| 9 | + |
| 10 | +On your current server, find the volume used to persist jenkins data. If you are using bind mounts volume try to find the location using following command: |
| 11 | + |
| 12 | +```shell |
| 13 | +docker inspect jenkins-container-name | grep volume |
| 14 | +``` |
| 15 | + |
| 16 | +of if using docker named volume: |
| 17 | + |
| 18 | +```shell |
| 19 | +docker volume ls |
| 20 | +``` |
| 21 | + |
| 22 | +## Backup Jenkins Data |
| 23 | + |
| 24 | +Before creating a backup, make sure jenkins server stopped: |
| 25 | + |
| 26 | +```shell |
| 27 | +docker stop jenkins-container-name |
| 28 | +``` |
| 29 | + |
| 30 | +Archive the jenkins home data: |
| 31 | + |
| 32 | +```shell |
| 33 | +tar -czvf jenkins_backup.tar.gz /path/of/bind/mount |
| 34 | +``` |
| 35 | + |
| 36 | +If it's a named volume: |
| 37 | + |
| 38 | +```shell |
| 39 | +docker run --rm -v jenkins_home:/data -v $(pwd):/backup alpine \ |
| 40 | +tar czvf /backup/jenkins_backup.tar.gz -C /data . |
| 41 | +``` |
| 42 | + |
| 43 | +> Here, we have used a new container using `alpine` image to create a backup of `jenkins` docker volume. it will create a `jenkins_backup.tar.gz` file in your current directory. |
| 44 | +
|
| 45 | +## Transfer to New Server |
| 46 | + |
| 47 | +Copy the backup file to new server: |
| 48 | + |
| 49 | +```shell |
| 50 | +scp jenkins_backup.tar.gz user@new_host:/path/to/new_host |
| 51 | +``` |
| 52 | + |
| 53 | +## Restore Data |
| 54 | + |
| 55 | +You can extract data into a directory to use as bind mount or a docker volume. |
| 56 | + |
| 57 | +To extract into a directory: |
| 58 | + |
| 59 | +```shell |
| 60 | +mkdir -p /var/jenkins_home |
| 61 | +tar -xzvf jenkins_backup.tar.gz -C /var/jenkins_home |
| 62 | +chown -R 1000:1000 /var/jenkins_home |
| 63 | +``` |
| 64 | + |
| 65 | +To extract into a docker volume: |
| 66 | + |
| 67 | +```shell |
| 68 | +# create docker volume |
| 69 | +docker volume create jenkins_home |
| 70 | + |
| 71 | +# extract data into this volume and fix file permissions |
| 72 | +docker run --rm -v jenkins_home:/data -v /directory_path/of/backup_file/:/backup alpine \ |
| 73 | +sh -c "cd /data && tar xzvf /backup/jenkins_backup.tar.gz && chown -R 1000:1000 /data" |
| 74 | +``` |
| 75 | + |
| 76 | +> What's happening here?: docker creating a new alpine container using jenkins_home volume, and extracted backup data into that directory and modified the files permission. |
| 77 | +
|
| 78 | +## Run Jenkins on the New Host |
| 79 | + |
| 80 | +Let's run the jenkins container on the new host with resorted data volume. |
| 81 | + |
| 82 | +```shell |
| 83 | +docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts |
| 84 | +``` |
| 85 | + |
| 86 | +We are done. |
0 commit comments