Continuous Delivery with Docker and Jenkins Third Edition Create secure applications by building complete CI/CD pipelines Rafał Leszko BIRMINGHAM—MUMBAI Continuous Delivery with Docker and Jenkins Third Edition Copyright © 2022 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Associate Group Product Manager: Rahul Nair Publishing Product Manager: Niranjan Naikwadi Senior Editor: Athikho Sapuni Rishana Content Development Editor: Sayali Pingale Technical Editor: Shruthi Shetty Copy Editor: Safis Editing Associate Project Manager: Neil Dmello Proofreader: Safis Editing Indexer: Tejal Daruwale Soni Production Designer: Alishon Mendonca Senior Marketing Coordinator: Sanjana Gupta Marketing Coordinator: Nimisha Dua First published: August 2017 Second edition: May 2019 Third edition: April 2022 Production reference: 1110422 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. 978-1-80323-748-0 www.packt.com – To my wonderful wife, Maria, for all her love, wisdom, and smile. Contributors About the author Rafał Leszko is a passionate software developer, trainer, and conference speaker living in Krakow, Poland. He has spent his career writing code, designing architecture, and being responsible for tech in a number of companies and organizations, including Hazelcast, Google, and CERN. Always open to new challenges, he has given talks and conducted workshops at numerous international conferences, including Devoxx and Voxxed Days. About the reviewer Werner Dijkerman is a freelance cloud (certified), Kubernetes, and DevOps engineer, currently focused on, and working with, cloud-native solutions and tools including AWS, Ansible, Kubernetes, and Terraform. He is also focused on Infrastructure as Code and monitoring the correct “thing” with tools such as Zabbix, Prometheus, and the ELK Stack, with a passion for automating everything and avoiding doing anything that resembles manual work. – Big thanks, hugs, and shoutouts to Anca Borodi, Theo Punter, and everyone else at COERA! Table of Contents Preface Section 1 – Setting Up the Environment 1 Introducing Continuous Delivery Understanding CD 4 Organizational prerequisites 16 The traditional delivery process 4 Technical and development prerequisites 18 The benefits of CD 7 Success stories 8 Combining CD and microservices 19 The automated deployment pipeline 10 Building the CD process 20 Continuous integration 11 Introducing tools 21 Automated acceptance testing 11 Creating a complete CD system 23 Configuration management 15 Summary 30 Prerequisites to CD 15 Questions 30 Further reading 30 2 Introducing Docker Technical requirements 32 Alternative containerization technologies 36 What is Docker? 32 Containerization versus virtualization 32 Installing Docker 37 The need for Docker 34 Prerequisites for Docker 37 Kittens and cattle 36 Installing on a local machine 38 Installing on a server 40 viii Table of Contents Running Docker hello-world 42 Exposing container ports 58 Docker components 43 Automatic port assignment 59 Docker client and server 43 Using Docker volumes 60 Docker images and containers 44 Using names in Docker 62 Docker applications 46 Naming containers 62 Building Docker images 47 Tagging images 63 docker commit 47 Docker cleanup 64 Dockerfile 49 Cleaning up containers 64 Complete Docker application 50 Cleaning up images 65 Environment variables 52 Docker commands overview 66 Docker container states 53 Summary 67 Docker networking 55 Exercises 68 Running services 55 Questions 69 Container networks 56 Further reading 69 3 Configuring Jenkins Technical requirements 72 Configuring agents 86 What is Jenkins? 72 Communication protocols 86 Installing Jenkins 73 Setting agents 87 Testing agents 98 Installing Jenkins with Docker 74 Comparing Docker pipeline builds and Installing Jenkins with Docker agents 99 dedicated packages 76 Initial configuration 76 Custom Jenkins images 101 Installing Jenkins in Kubernetes 78 Building the Jenkins agent 101 Jenkins in the cloud 79 Building the Jenkins master 103 Jenkins – Hello World 80 Configuration and Jenkins architecture 82 management 105 Master and agents 83 Plugins 105 Scalability 84 Security 106 Test and production instances 85 Backup 106 Sample architecture 85 Jenkins Blue Ocean UI 107 Table of Contents ix Summary 108 Questions 109 Exercises 108 Further reading 109 Section 2 – Architecting and Testing an Application 4 Continuous Integration Pipeline Technical requirements 114 SonarQube 136 Introducing pipelines 114 Triggers and notifications 137 The pipeline structure 114 Triggers 137 A multi-stage Hello World 115 Notifications 140 The pipeline syntax 116 Team development strategies 142 The commit pipeline 120 Development workflows 143 Checkout 120 Adopting continuous integration 145 Compile 122 Jenkins multi-branch 147 Unit tests 125 Non-technical requirements 149 Jenkinsfile 128 Summary 150 Code-quality stages 131 Exercises 151 Code coverage 131 Questions 151 Static code analysis 134 Further reading 152 5 Automated Acceptance Testing Technical requirements 153 Acceptance tests in the Introducing acceptance Jenkins pipeline 163 testing 154 The Docker build stage 165 Installing and using the The Docker push stage 167 Docker Registry 155 The acceptance testing stage 167 The artifact repository 155 Writing acceptance tests 169 Installing a Docker Registry 157 Writing user-facing tests 169 Using the Docker Registry 161