jenkins
automating the build, test and deployment process
Docker Compose Configuration
Below is the docker-compose.yaml
file used to set up Jenkins. This configuration ensures Jenkins runs with the necessary permissions and mounts the Docker socket to enable Docker commands inside Jenkins pipelines.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
sservices:
jenkins:
image: jenkins/jenkins:lts-jdk17
container_name: jenkins
privileged: true
user: root
ports:
- "8080:8080"
- "50000:50000"
volumes:
- jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
environment:
- DOCKER_HOST=unix:///var/run/docker.sock
restart: unless-stopped
volumes:
jenkins_home:
Retrieving the Initial Admin Password
After starting the Jenkins container, retrieve the initial admin password to complete the setup.
1
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
Installing Docker Inside the Jenkins Container
To enable Docker commands in Jenkins pipelines, install Docker inside the Jenkins container.
1
2
3
docker exec jenkins apt update
docker exec jenkins apt install -y docker.io
docker exec jenkins docker --version
Example Jenkins Pipeline
Here’s an example Jenkins pipeline that runs a Docker container to test the setup.
1
2
3
4
5
6
7
8
9
10
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'docker run hello-world'
}
}
}
}
Setting Up Jenkins via CLI
If Jenkins is not already set up, you can use the CLI to perform initial configuration.
Install wget and Download Jenkins CLI
1
2
3
docker exec jenkins apt update
docker exec jenkins apt install wget
docker exec jenkins wget http://localhost:8080/jnlpJars/jenkins-cli.jar
Test the CLI Installation
1
docker exec jenkins java -jar jenkins-cli.jar -s http://localhost:8080/ help
Installing Plugins via CLI
You can install Jenkins plugins using the CLI. Below is an example of installing the Docker plugin.
Replace
1
docker exec -it jenkins java -jar jenkins-cli.jar -auth admin:apiToken -s http://localhost:8080/ install-plugin <plugin-name>
Restart Jenkins After Plugin Installation
1
docker exec -it jenkins java -jar jenkins-cli.jar -auth admin:apiToken -s http://localhost:8080/ safe-restart
Troubleshooting
If you encounter issues, check the Jenkins logs for errors.
1
docker logs jenkins | less
This setup provides a robust Jenkins environment with Docker integration, ready for CI/CD pipelines. Let me know if you need further assistance!