1

I had an EC2 instance with "Amazon Linux 2" ami where i started a Jenkins docker container. We had a requirement to store everything in jenkins_home/ folder in s3 bucket and use the same when we bring our instance up again when we want to use it, as running instance continuously would cost us when we don't need to run it.

I started my container with the below commands.

yes | yum install docker
systemctl start docker
docker pull jenkins/jenkins:2.387.3
docker pull jenkins/jenkins
docker run -d --name my-jenkins -p 8080:8080 --restart on-failure jenkins/jenkins:2.387.3

and entered into the container using the docker exec command and copied the file from inside docker container to my EC2 instance and created a tar file from it and uploaded it in s3 bucket. Everything worked fine until now.

When I started my EC2 again and ran the jenkins/jenkins image and copied the s3 bucket tar file into EC2 instance and uploaded it in container path at "var/jenkins_home/" using the below commands.

aws s3 cp s3://dcp-jenkins-backup/jenkins.tar.gz .
tar xvzf jenkins.tar.gz
rm jenkins.tar.gz
docker cp . my-jenkins:/var/jenkins_home 

I looked into the container and everything was getting copied successfully.

But when I did a docker restart the container was not getting restarted and giving me the below logs.

touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

I tried changing the permissions of the files before copying into the container but that didn't work. Also tried to change permission after copying in container before docker restart but I was getting permission denied and didn't able to do it there either.

Anyway I can make this work?

I did something similar where we don't run jenkins on container either on host only. There it worked like charm.

Thanks

Nitin k
  • 95
  • 7
  • For the use case you're doing, you almost certainly should avoid `docker exec`, `docker cp`, and other commands that operate on the running container: it is routine to delete and recreate the container and these changes will be lost. The thing I don't see in your invocation is a `docker run -v` option to cause Jenkins's data to be persisted outside the container. For static data, also consider creating a custom image `FROM jenkinsci/jenkins` and `COPY` the data into the image at build time. – David Maze May 05 '23 at 16:11
  • yes I have already tried with the -v flag in command still had the permission issue. I saw that building custom image but the only problem is how can I run it while trying to launch my instance – Nitin k May 05 '23 at 16:42
  • As currently i was running the docker run command using user-data script of EC2 instance – Nitin k May 05 '23 at 16:47
  • Build the image offline and push it to a repository of some sort (in an AWS environment, possibly ECR), and then `docker run` your custom image instead of standard Jenkins in the userdata script. – David Maze May 05 '23 at 17:13
  • Yeah! I'll do that. Will create Jenkins job which will build that image and push in ECR and later terminate my instance and use this custom image at bootup – Nitin k May 06 '23 at 03:56

0 Answers0