Cloud Native 18 min read

Master Jenkins Pipelines with Docker for Seamless Continuous Delivery

This article explains how to replace traditional, error‑prone release cycles with a Docker‑based Jenkins pipeline that automates building, testing, image creation, and deployment to Docker, Kubernetes or remote hosts, while sharing practical tips, sample Groovy code, and common Q&A for DevOps teams.

dbaplus Community
dbaplus Community
dbaplus Community
Master Jenkins Pipelines with Docker for Seamless Continuous Delivery

Traditional Delivery Challenges

Conventional projects follow a linear flow: requirement gathering, design, development, module integration, packaging, testing, bug fixing, and repeat. Multiple environments (dev, test, prod) and manual packaging introduce uncertainty, long dev‑test‑fix cycles, and missed opportunities for early bug detection.

Continuous Delivery Principles

Continuous delivery aims for on‑demand, reliable releases by automating build, test, and deployment. Key points include:

Automated, script‑driven pipelines triggered by time, code commits, or manual actions.

Building in a clean, reproducible environment rather than ad‑hoc developer machines.

Locking down OS, build‑tool, and dependency versions.

Avoiding network‑based dependency fetching during builds (especially for Node.js).

Docker as an Enabler

Docker provides strong isolation, allowing the entire build environment to be packaged with the application. Benefits include:

Consistent environment across developers, CI servers, and production.

Elimination of host‑side pollution and easy parallel builds of different language versions.

Simple deployment via a single docker run command.

Jenkins Pipeline Overview

Jenkins, with its rich plugin ecosystem, orchestrates the CI/CD workflow. Using the Pipeline (Groovy) syntax, stages such as code checkout, unit testing, building, image creation, and deployment are defined. Docker plugins enable containerized execution of each stage.

Running Jenkins Inside Docker

Start a Jenkins container with Docker access:

docker run -d -u root \
  -p 8080:8080 \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v $(which docker):/bin/docker \
  -v /var/jenkins_home:/var/jenkins_home \
  jenkins

This mounts the Docker socket and binary, allowing Jenkins jobs to invoke Docker commands. The container runs as root to perform privileged operations, and /var/jenkins_home stores jobs, plugins, and logs for persistence.

Sample Pipeline Script

node {
    // Checkout code
    stage('get Code') {
        git credentialsId: 'git-credentials-id', url: 'http://192.168.19.250/ufleet/uflow.git'
    }

    // Unit testing inside Golang container
    stage('unit testing') {
        docker.image('golang:1.7').inside {
            sh './script/unittest.sh'
        }
    }

    // Build inside Golang container
    stage('Build') {
        def confFilePath = 'conf/app.conf'
        def config = readFile confFilePath
        config = config.replaceFirst(/runmode = dev/, "runmode = prod")
        writeFile file: confFilePath, text: config
        docker.image('golang:1.7').inside {
            sh './script/build.sh'
        }
    }

    // Build and push Docker image
    def imagesName = '192.168.18.250:5002/ufleet/uflow:v0.9.1.${BUILD_NUMBER}'
    stage('Image Build And Push') {
        docker.withRegistry('http://192.168.18.250:5002', 'registry-credentials-id') {
            docker.build(imagesName).push()
        }
    }

    // Deploy the new image
    stage('deploy images') {
        try { sh 'docker rm -f cicdDemo' } catch (e) { /* ignore */ }
        docker.image(imagesName).run('-p 9091:80 --name cicdDemo')
    }
}

Pipeline Execution Flow

Git plugin fetches the latest source into the Jenkins workspace. docker.image().inside mounts the workspace into a container and runs the specified script (unit test or build).

Dockerfile in the source builds the final image, which is pushed to a private registry.

Old containers are removed, and the new image is launched.

Best Practices and Tips

Use Git for version control; trigger pipelines on each commit.

Keep the main branch clean; fix bugs promptly to avoid cascading issues.

Adopt test‑driven development: write unit tests before code.

Avoid committing code late in the day to reduce merge conflicts.

Store jenkins_home on persistent storage for easy recovery.

Periodically clean up old images to save disk space.

Prefer pipeline scripts stored in source control over UI‑only definitions.

Key Q&A Highlights

Jenkins can trigger pipelines via webhooks, cron schedules, or SCM polling.

Kubernetes integration is possible through kubectl steps or external APIs; Jenkins itself focuses on CI.

Docker is not mandatory for image builds; alternative artifact repositories can be used.

Pipeline input step supports interactive prompts, file uploads, and parameter validation.

Automated triggers rely on cron expressions and commit ID changes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerKubernetesContinuous DeliveryPipelineJenkins
dbaplus Community
Written by

dbaplus Community

Enterprise-level professional community for Database, BigData, and AIOps. Daily original articles, weekly online tech talks, monthly offline salons, and quarterly XCOPS&DAMS conferences—delivered by industry experts.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.