How to start Docker service on Linux for Jenkins?

JenkinsJenkinsBeginner
Practice Now

Introduction

This tutorial will guide you through the process of setting up Docker on a Linux system and integrating it with Jenkins, a widely-used continuous integration and deployment tool. We'll cover the fundamental concepts of Docker, the installation process, and the steps to seamlessly incorporate Docker into your Jenkins workflow.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL jenkins(("`Jenkins`")) -.-> jenkins/PipelineGroup(["`Pipeline`"]) jenkins(("`Jenkins`")) -.-> jenkins/InstallingJenkinsGroup(["`Installing Jenkins`"]) jenkins/PipelineGroup -.-> jenkins/pipeline("`Pipeline`") jenkins/InstallingJenkinsGroup -.-> jenkins/docker_installation("`Use Docker Installation`") subgraph Lab Skills jenkins/pipeline -.-> lab-415669{{"`How to start Docker service on Linux for Jenkins?`"}} jenkins/docker_installation -.-> lab-415669{{"`How to start Docker service on Linux for Jenkins?`"}} end

Docker Fundamentals

What is Docker?

Docker is an open-source platform that allows developers to build, deploy, and run applications in a containerized environment. Containers are lightweight, portable, and self-contained packages that include an application and all its dependencies, ensuring that the application will run consistently across different computing environments.

Docker Architecture

Docker follows a client-server architecture, where the Docker client communicates with the Docker daemon (the server) to perform various operations. The Docker daemon is responsible for managing Docker objects, such as images, containers, networks, and volumes.

graph LD subgraph Docker Architecture client[Docker Client] daemon[Docker Daemon] client -- API --> daemon daemon -- Images, Containers, Networks, Volumes --> client end

Docker Images

Docker images are the foundation of containerized applications. An image is a read-only template that contains the application code, libraries, dependencies, and other necessary files to run the application. Images are built using a Dockerfile, which is a text file that contains instructions for building the image.

Docker Containers

Containers are instances of Docker images. When you run a Docker image, it creates a container, which is an isolated and self-contained environment that runs the application. Containers can be started, stopped, and managed using the Docker client.

Docker Networking

Docker provides built-in networking capabilities that allow containers to communicate with each other and the outside world. Docker supports various network drivers, such as bridge, host, and overlay, which can be used to create and manage networks for your containerized applications.

Docker Volumes

Docker volumes are used to persist data generated by a container. Volumes are independent of the container's lifecycle and can be shared among multiple containers. Volumes can be used to store application data, configuration files, and other persistent data.

Installing Docker on Linux

Prerequisites

Before installing Docker on your Linux system, ensure that you have the following prerequisites:

  • A Linux distribution, such as Ubuntu 22.04 LTS
  • Administrative privileges (sudo or root access)

Install Docker on Ubuntu 22.04

  1. Update the package index:
sudo apt-get update
  1. Install the necessary packages to allow apt to use a repository over HTTPS:
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
  1. Add the official Docker GPG key:
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  1. Set up the Docker repository:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  1. Install Docker Engine, containerd, and Docker Compose:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
  1. Verify the installation:
sudo docker run hello-world

This command downloads a test image and runs it in a container, verifying that Docker is installed correctly.

Integrating Docker with Jenkins

Why Integrate Docker with Jenkins?

Integrating Docker with Jenkins provides several benefits for your continuous integration and deployment (CI/CD) pipeline:

  • Consistent Environments: Docker containers ensure that your application and its dependencies are packaged and deployed consistently across different environments.
  • Scalability: Docker containers can be easily scaled up or down to handle increased workloads or traffic.
  • Faster Builds and Deployments: Docker containers can be built and deployed much faster than traditional virtual machines.
  • Improved Reliability: Docker containers are isolated and self-contained, reducing the risk of conflicts between applications and their dependencies.

Setting up Docker Integration in Jenkins

  1. Install Docker Plugin in Jenkins: Install the "Docker" plugin in your Jenkins instance, which provides integration between Jenkins and Docker.

  2. Configure Docker in Jenkins: In the Jenkins global configuration, set up the Docker server connection details, such as the Docker host URL and Docker API version.

  3. Create a Docker-based Jenkins Agent: Configure a Jenkins agent that can run in a Docker container. This allows you to run your build and test steps in a containerized environment.

graph LR Jenkins --> Docker_Agent Docker_Agent --> Docker_Host
  1. Use Docker in your Jenkins Pipeline: In your Jenkins pipeline, you can use the Docker steps provided by the plugin to build, push, and pull Docker images, as well as run your application in a Docker container.
pipeline {
    agent {
        docker {
            image 'maven:3.8.2-openjdk-11'
            args '-v /tmp:/tmp'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Docker Build and Push') {
            steps {
                script {
                    docker.withRegistry('https://registry.example.com', 'docker-registry-credentials') {
                        def dockerImage = docker.build("my-app:${env.BUILD_NUMBER}")
                        dockerImage.push()
                    }
                }
            }
        }
    }
}

By integrating Docker with Jenkins, you can create a more robust, scalable, and reliable CI/CD pipeline for your applications.

Summary

By the end of this tutorial, you will have a solid understanding of Docker and its integration with Jenkins. You'll be able to set up a Docker environment on your Linux system and leverage Docker's capabilities within your Jenkins pipeline, streamlining your continuous integration and deployment processes.

Other Jenkins Tutorials you may like