wiki:dockerized_jenkins_install_docker_via_ansible_dind
Table of Contents
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
References
wiki/dockerized_jenkins_install_docker_via_ansible_dind.txt · Last modified: 2023/12/21 10:48 by antisa