Say you want to quickly get started with Jenkins and “kick the tires” before fully committing to rolling out your own instance. How do you go about doing this?
Or say your company fully controls your Jenkins instance and they won’t give you the permissions to create your own pipeline and try out your build. It all has to go through them.
The second case is where I found myself. Good news is running Jenkins locally via docker just to test out your build file is super easy.
There are a couple guides that will walk you through this process. But in my case, my Jenkins file was starting a docker container itself — i.e. we would be running a docker image inside Jenkins docker image. That’s where things got tricky and I ended up making some changes to make that happen.
If you’re not sure what that means, look at the JenkinsFile below. As you can see the agent is a dockerfile.
pipeline {
agent {
dockerfile true
}
stages {
stage('Install') {
steps {...
}
stage('Build') {
steps {
sh 'npm run build'
}
}
stage('Test') {
steps {
sh 'npm run test_ci'
}
}
}
First pull the latest Jenkins image on docker:
docker image pull jenkins/jenkins:lts
Once that’s done, you can run the container using the following command:
docker run -u 0 --privileged
-p 8082:8080
-p 50000:50000
-v /var/run/docker.sock:/var/run/docker.sock
-v $(which docker):/usr/bin/docker
-v jenkinsvol:/var/jenkins_home
--name jenkins-local jenkins/jenkins:lts
There is a lot to unpack there so let’s have a look at the details:
- -p will forward the needed ports over to your local machine so you can access Jenkins in a browser
- the first -v will map the docker sock volume. You will need to do this if your JenkinsFile will start a docker image itself.
- the second -v will create a volume to store the Jenkin’s state
- — name will give a name to this container.
Run the command and check to make sure the container is running:
Now navigate to http://localhost:8082/ and you should be greeted with a Jenkins first time install page:
The easiest way to find the admin password is to do docker logs [container_id]
where container_id
is obtained from docker ps
.
And that should be all you’ll need to get your Jenkins up and running. The setup process is fairly straightforward so I won’t bore you with going over it. There are a lots of documentation available on it if need be.
Good luck!