{{tag>docker ansible jenkins}}
====== Dockerized jenkins install with docker in docker via ansible (dind) ======
Example of compose file:
version: '3'
services:
jenkins:
build:
context: .
dockerfile: Dockerfile-jenkins-docker
container_name: jenkins-docker
networks:
- jenkins
restart: always
environment:
- DOCKER_HOST=tcp://docker:2376
- DOCKER_CERT_PATH=/certs/client
- DOCKER_TLS_VERIFY=1
# increase user session duration
- JENKINS_OPTS=--sessionTimeout=1440 --sessionEviction=43200
volumes:
- jenkins-data:/var/jenkins_home
- jenkins-docker-certs:/certs/client:ro
ports:
- 127.0.0.1:8080:8080
jenkins-docker-executor:
image: docker:dind
container_name: jenkins-docker-executor
environment:
- DOCKER_TLS_CERTDIR=/certs
networks:
jenkins:
aliases:
- docker
restart: always
privileged: True
volumes:
- jenkins-data:/var/jenkins_home
- jenkins-docker-certs:/certs/client
ports:
- 127.0.0.1:2376:2376
networks:
jenkins:
driver: bridge
volumes:
jenkins-data:
driver: local
jenkins-docker-certs:
driver: local
As you can see we need a custom dockerfile (//Dockerfile-jenkins-docker//) which should look like:
FROM jenkins/jenkins:{{ jenkins_version }}
USER root
ENV TZ=Europe/Zagreb
RUN apt-get update && apt-get install -y lsb-release
RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \
https://download.docker.com/linux/debian/gpg
RUN echo "deb [arch=$(dpkg --print-architecture) \
signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \
https://download.docker.com/linux/debian \
$(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
RUN apt-get update && apt-get install -y docker-ce-cli
USER jenkins
Add these 2 files to templates or files folder in ansible role and add a task similar to this:
...
- name: Copy docker compose file
template:
src: docker-compose.j2
dest: /opt/jenkins-docker/docker-compose.yml
mode: 0600
- name: Copy Jenkins docker Dockerfile
template:
src: Dockerfile-jenkins-docker.j2
dest: /opt/jenkins-docker/Dockerfile-jenkins-docker
mode: 0600
- name: Run compose command
command:
chdir: /opt/jenkins-docker
cmd: docker-compose up --detach --build
...
====== Tested on ======
* Debian 11
* Docker Compose version v2.6.0
* ansible 2.9.27
====== See also ======
* [[wiki:docker_troubleshooting|Docker troubleshooting]]
* [[wiki:install_2_node_jira_datacenter_docker_behind_apache_reverse_proxy|Install 2 node Jira Datacenter in Docker behind apache reverse proxy]]
* [[wiki:docker_commands#connect_to_different_docker_daemon_running_on_different_port|Connecting to different docker daemon]]
* [[wiki:increase_jenkins_user_session_timeout|Increase jenkins user session timeout]]
====== References ======
* https://hub.docker.com/_/docker
* https://www.jenkins.io/doc/book/installing/docker/
* https://stackoverflow.com/questions/66492160/connecting-to-docker-daemon-from-jenkins-running-inside-a-docker-container
* https://stackoverflow.com/questions/75649284/jenkins-docker-session-timeout-configuration