3

I have a Jenkins setup in a docker container in my local computer. Can I move it to a company's CI server and re-use job items?

I tried this at local computer

docker commit
docker push

At CI server

docker pull
docker run 

However, when I run Jenkins on CI server, Jenkins was initialized.

How can I get all the configurations and job items using Docker?

John
  • 1,139
  • 3
  • 16
  • 33

2 Answers2

5

As described in the docs for the commit command

The commit operation will not include any data contained in volumes mounted inside the container.

The jenkins home is mounted as a volume, thus when you commit the container the jenkins home won't be commited. Therefore all the job configuration that is currently on the running local container won't be part of the commited image.

Your problem reduces to how would you migrate the jenkins_home volume that is on your machine, to the another machine. This problem is solved and you can find the solution here.

I would suggest however a better and more scalable approach, specifically for jenkins. The problem with the first approach, is that there is quiet some manual intervention that needs to be done whenever you want to start a similar jenkins instance on a new machine.

The solution is as follows:

  1. Commit the container that is currently running
  2. Copy the job configuration that is inside the container using the command: docker cp /var/jenkins_home/jobs ./jobs. This will copy the job config from the running container into your machine. Remember to clean the build folders
  3. Create a Dockerfile that inherits from the commited image and copy the job config under the jenkins_home.
  4. Push the image and you should have an image that you can pull and will have all the jobs configured correctly

The dockerfile will look something like:

FROM <commited-container>
COPY jobs/* /var/jenkins_home/jobs/
yamenk
  • 46,736
  • 10
  • 93
  • 87
  • 1
    Nice easy solution. I just wanted to provide a 'sample' Jenkins image for our software which needs a lot of plugins, so I copied the whole of jenkins_home in, not just jobs. ie: (for your step 2) I did ` docker cp :/var/jenkins_home ./JENKINSHOME` , then in the dockerfile (your step 3.): ` FROM COPY ./JENKINSHOME /var/jenkins_home/ USER root RUN chmod -R 777 /var/jenkins_home/ USER jenkins` – MetalRules Sep 18 '20 at 06:18
1

You need to check how the Jenkins image (hub.docker.com/r/jenkins/jenkins/) was launched on your local computer: if it was mounting a local volume, that volume should include the JENKINS_HOME with all the job configurations and plugins.

docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts

You need to export that volume too, not just the image.
See for instance "Docker & Jenkins: Data that Persists ", using a data volume container that you can then export/import.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250