Protecting Your Docker Data

Protecting Your Docker Data

In this article, I'll guide you through the steps of running Jenkins within a Docker container, creating a test project within Jenkins, and exploring the power of Docker for portability. But what sets this article apart is the deep dive into the process of recovering the Jenkins container data at the time of container disasters.

Running Jenkins as a Docker Container

In order to start the Jenkins service as a docker container run the below commands.

docker pull jenkins/jenkins
docker run -d -p 8080:8080 --name Jenkins jenkins/jenkins

The above commands will start your container application under the bridge network. You can then access your Jenkins application in localhost:8080

To get the administrator password required for Jenkins run the below commands

docker exec -it Jenkins bash
cat /var/jenkins_home/secrets/initialAdminPassword

After getting the password set up your Jenkins server by providing all the information.

It will look similar to below

Creating a test project within Jenkins

You can create any project based on your choice. For this demo, I have created a freestyle project in the name of TestProject-1 that will only print "Hello from Jenkins!"

Recovering the Jenkins container data

There could be a number of reasons for the failure of your containers. If that happens we should be in a position to recover our old data back to our containers. For that reason, it is very important to understand in which location the data are actually being stored by the containers.

We can inspect the containers using the command docker container inspect <container-name> and over there we can see the container volume details under Mounts.

In the above picture as you can see the Source section has the underlying host machine location where it has all the container data with their unique name. The Destination section is the location inside the container having all the container data in it.

Let's now delete our Jenkins container which we created earlier and try to get back the data again.

To delete the Jenkins container run the below command

docker kill Jenkins

In order to start the Jenkins container again with its old data run the below command

docker run -d -p 8080:8080 --name newJenkins -v var/lib/docker/volumes/c8a05df52f7b2f1444fc4b766642482d60dcc2518037a5ab578c71623f5be460/_data:/var/jenkins_home jenkins/jenkins

Thus we can recover container data from disasters by attaching the volume of the containers with the newly created containers.

Great job! If you have received value from this article then you can support this by liking, commenting, and sharing it with others.