How to enable Docker auto-start on system boot for Jenkins?

JenkinsJenkinsBeginner
Practice Now

Introduction

This tutorial will guide you through the process of configuring Docker to automatically start on system boot, enabling a seamless integration with your Jenkins continuous integration and deployment workflows. By ensuring Docker is always available, you can streamline your development processes and maintain reliable Jenkins operations.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL jenkins(("`Jenkins`")) -.-> jenkins/PipelineGroup(["`Pipeline`"]) jenkins(("`Jenkins`")) -.-> jenkins/BlueOceanGroup(["`Blue Ocean`"]) jenkins(("`Jenkins`")) -.-> jenkins/InstallingJenkinsGroup(["`Installing Jenkins`"]) jenkins/PipelineGroup -.-> jenkins/pipeline("`Pipeline`") jenkins/PipelineGroup -.-> jenkins/running_pipelines("`Running Pipelines`") jenkins/BlueOceanGroup -.-> jenkins/creating_a_pipeline("`Creating a Pipeline`") jenkins/InstallingJenkinsGroup -.-> jenkins/docker_installation("`Use Docker Installation`") jenkins/InstallingJenkinsGroup -.-> jenkins/war_files_installation("`Use War files installation`") subgraph Lab Skills jenkins/pipeline -.-> lab-415668{{"`How to enable Docker auto-start on system boot for Jenkins?`"}} jenkins/running_pipelines -.-> lab-415668{{"`How to enable Docker auto-start on system boot for Jenkins?`"}} jenkins/creating_a_pipeline -.-> lab-415668{{"`How to enable Docker auto-start on system boot for Jenkins?`"}} jenkins/docker_installation -.-> lab-415668{{"`How to enable Docker auto-start on system boot for Jenkins?`"}} jenkins/war_files_installation -.-> lab-415668{{"`How to enable Docker auto-start on system boot for Jenkins?`"}} end

Introduction to Docker and Jenkins

Docker is a popular open-source platform that enables developers to build, deploy, and run applications in containerized environments. It provides a consistent and reliable way to package and distribute software, making it easier to manage and scale applications across different environments.

Jenkins, on the other hand, is a widely used open-source automation server that helps developers automate various aspects of the software development lifecycle, such as building, testing, and deploying applications.

Docker Basics

Docker uses a client-server architecture, where the Docker client communicates with the Docker daemon (the server) to execute commands and manage containers. Docker containers are lightweight, standalone, and executable packages that include everything needed to run an application, including the code, runtime, system tools, and libraries.

Jenkins Basics

Jenkins is a powerful tool that helps automate the software development process. It can be used to build, test, and deploy applications, as well as to manage various other tasks, such as code analysis, artifact management, and deployment to production environments.

Integrating Docker and Jenkins

By integrating Docker with Jenkins, developers can take advantage of the benefits of both technologies. Jenkins can be used to build, test, and deploy Docker-based applications, while Docker can be used to create consistent and reproducible environments for running those applications.

graph TD A[Developer] --> B[Jenkins] B --> C[Docker] C --> D[Application]

Table 1: Key features of Docker and Jenkins

Feature Docker Jenkins
Containerization -
Automated Builds
Automated Testing
Automated Deployment
Scalability
Flexibility

By understanding the basics of Docker and Jenkins, developers can leverage the power of both technologies to streamline their software development and deployment processes.

Configuring Docker Auto-start on System Boot

To ensure that the Docker daemon starts automatically on system boot, you can configure it using the following steps:

Step 1: Create a systemd service file

Create a new systemd service file for Docker by running the following command:

sudo nano /etc/systemd/system/docker.service

Add the following content to the file:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/bin/dockerd -H fd://
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

[Install]
WantedBy=multi-user.target

Step 2: Reload the systemd daemon

After creating the service file, reload the systemd daemon to make the changes effective:

sudo systemctl daemon-reload

Step 3: Enable and start the Docker service

Enable the Docker service to start automatically on system boot, and then start the service:

sudo systemctl enable docker.service
sudo systemctl start docker.service

You can verify the status of the Docker service using the following command:

sudo systemctl status docker.service

The output should show that the Docker service is running and enabled to start on system boot.

graph TD A[System Boot] --> B[systemd] B --> C[Docker Service] C --> D[Docker Daemon] D --> E[Docker Containers]

By configuring Docker to start automatically on system boot, you can ensure that your Docker-based applications are always available and ready to use, even after system reboots or power outages.

Integrating Jenkins with Auto-started Docker

To integrate Jenkins with the auto-started Docker daemon, you can follow these steps:

Step 1: Install Jenkins

First, you need to install Jenkins on your system. You can follow the official Jenkins installation guide for your operating system, which in this case is Ubuntu 22.04.

Step 2: Configure Jenkins to use the Docker daemon

After installing Jenkins, you need to configure it to use the Docker daemon. Follow these steps:

  1. Log in to the Jenkins web interface.
  2. Go to "Manage Jenkins" > "Configure System".
  3. Scroll down to the "Cloud" section and click "Add a new cloud" > "Docker".
  4. In the "Docker URL" field, enter the URL of the Docker daemon, which in this case is unix:///var/run/docker.sock.
  5. Click "Test Connection" to verify that Jenkins can communicate with the Docker daemon.
  6. Configure any other settings as needed, such as Docker image options, and save the changes.

Step 3: Create a Jenkins pipeline using Docker

Now that Jenkins is configured to use the auto-started Docker daemon, you can create a Jenkins pipeline that utilizes Docker. Here's an example pipeline:

pipeline {
    agent {
        docker {
            image 'ubuntu:22.04'
            args '-v /var/run/docker.sock:/var/run/docker.sock'
        }
    }
    stages {
        stage('Build') {
            steps {
                sh 'docker build -t my-app .'
            }
        }
        stage('Test') {
            steps {
                sh 'docker run my-app pytest'
            }
        }
        stage('Deploy') {
            steps {
                sh 'docker push my-app:latest'
            }
        }
    }
}

In this example, the pipeline uses the ubuntu:22.04 Docker image as the agent, and the args parameter mounts the Docker socket to the container, allowing the pipeline to interact with the auto-started Docker daemon.

By integrating Jenkins with the auto-started Docker daemon, you can create robust and reliable CI/CD pipelines that leverage the benefits of both technologies, ensuring that your applications are built, tested, and deployed consistently and efficiently.

Summary

In this tutorial, you have learned how to enable Docker auto-start on system boot to seamlessly integrate with your Jenkins continuous integration and deployment workflows. By automating the Docker startup process, you can ensure a reliable and efficient Jenkins environment, allowing you to focus on your development tasks without worrying about the underlying infrastructure.

Other Jenkins Tutorials you may like