How to Build a Jenkins‑Docker Automated CI/CD Pipeline from Scratch
This guide walks you through installing Jenkins and Docker, configuring Jenkins agents, creating jobs and pipelines, mastering essential Docker commands and Dockerfile syntax, and finally integrating the two tools to establish a fully automated CI/CD environment for building and deploying applications.
Jenkins Overview
Jenkins is an open‑source automation server that orchestrates building, testing, and deploying applications. It integrates with build tools such as Ant, Maven, Gradle and version‑control systems like Git and Subversion, and can connect directly to GitHub or Bitbucket repositories.
Jenkins Installation
Download the latest jenkins.war from http://mirrors.jenkins-ci.org/war/latest/ and start it with Java 1.6+: java -jar jenkins.war The server listens on port 8080 by default and stores configuration under ~/.jenkins. If automatic plugin download fails, plugins can be installed manually by uploading the .hpi files.
System Configuration
After the first launch, navigate to Manage Jenkins → Global Tool Configuration and define the paths for JDK, Maven, Git, etc., on the Jenkins host.
Jenkins Cluster (Slave) Setup
Add build agents (slaves) via Manage Jenkins → Manage Nodes → New Node . Choose “Launch slave agents on UNIX machines via SSH”. Jenkins will SSH to the remote host and start the agent with: java -jar slave.jar Multiple agents can be added to distribute workloads.
Job Creation and Pipeline
Create a freestyle job or a Pipeline view. For a freestyle job, add an “Execute shell” build step and write the required shell script. For a Pipeline, use the Jenkins Pipeline plugin to define stages (e.g., checkout, build, static analysis, unit test, deployment, UI test) in a Jenkinsfile. The Pipeline view visualizes stage progression and success/failure status.
Docker Overview
Docker provides lightweight containers that share the host kernel but have isolated file systems, libraries, and runtime environments. Containers consume far less memory and CPU than full virtual machines, making them ideal for reproducible build and test environments.
Docker Installation
Follow the official documentation at https://docs.docker.com. On Linux install the native Docker Engine. On Windows use Docker Desktop (or Docker Machine) and on macOS use Docker Desktop with the HyperKit backend.
Docker Basic Commands
docker version/ docker info – display client and daemon information docker pull <image> – download an image from a registry docker run -d -p 8080:8080 --name myapp <image> – start a container in detached mode and map ports docker exec -it <container> /bin/bash – open an interactive shell inside a running container docker logs -f <container> – stream container logs docker kill <container> – stop a container immediately docker rm <container> – remove a stopped container docker commit -a "author" -m "msg" <container> – create a new image from container changes docker push <repo/image> – upload an image to a registry
docker ps -a | grep "Exited" | awk '{print $1}' | xargs docker rm– clean up exited containers docker rmi $(docker images -f "dangling=true" -q) – remove dangling images
Dockerfile Basics
FROM <base_image:tag>– specify the parent image MAINTAINER <name> – optional author metadata ENV VAR=value – set environment variables COPY src dest – copy files from build context into the image WORKDIR /path – set the working directory for subsequent instructions RUN <command> – execute a command during image build (e.g., install packages, compile code) EXPOSE <port> – document the port the container will listen on ENTRYPOINT ["executable","param"] – define the default command when the container starts
Tip: combine multiple RUN commands with && or use a single RUN with a subshell to reduce the number of image layers.
Jenkins + Docker Integrated Workflow
The typical integration uses Jenkins to trigger jobs and Docker to provide isolated build and test environments. The workflow consists of:
Trigger: schedule, Git commit, or pull‑request webhook.
Pipeline definition: a Jenkinsfile that declares stages (checkout, build, static analysis, unit test, deployment, UI test).
Job design: either embed the entire build (including source checkout and compilation) inside a Docker image, or keep the build tools on the Jenkins host and run only the runtime inside a container.
Execute shell: use Docker commands to start containers, compile code, expose ports, and launch the application.
Example Dockerfile for a Java application:
FROM openjdk:8-jdk
MAINTAINER [email protected]
ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . $APP_HOME
RUN ./mvnw package
EXPOSE 8080
ENTRYPOINT ["java","-jar","target/app.jar"]Build and run the image:
docker build -t myapp .
# Map host port 8080 to container port 8080
docker run -d -p 8080:8080 --name myapp_container myappBy keeping the build environment inside containers, the host remains clean, multiple language versions can coexist, and the production and test environments are guaranteed to be identical.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
