Operations 13 min read

How to Build an Automated CI/CD Pipeline with Jenkins and Docker

This guide explains how to create a fast, reliable CI/CD workflow by integrating Jenkins, Docker, Maven, and Alibaba Cloud, covering pipeline benefits, step‑by‑step configuration, Docker image building, registry push/pull, Jenkins container setup, GitLab webhook integration, and common pitfalls with suggested optimizations.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Build an Automated CI/CD Pipeline with Jenkins and Docker

Jenkins and Docker Automated CI/CD

Efficient CI/CD environments provide early problem detection, dramatically reduce failure rates, accelerate iteration speed, cut time costs, and enable end‑to‑end pipelines with one‑click deployment, elastic scaling, and gray‑release capabilities.

Timely issue discovery: early integration feedback and fixes.

Significant failure‑rate reduction: process‑driven, less human error.

Faster iteration: dozens or hundreds of builds in minutes.

Reduced time cost: no manual project management or cumbersome deployments.

End‑to‑end pipeline: one‑click deployment, elastic scaling, gray release.

Achieving such a pipeline requires integrating DevOps tools, multi‑environment adaptation, and designing automation‑first processes like one‑click deployment and upgrade.

Jenkins + Docker

The following network diagram illustrates the overall architecture.

Architecture diagram
Architecture diagram

Pipeline steps:

Developer creates a tag in GitLab.

GitLab pushes the tag event to Jenkins.

Jenkins fetches the source, compiles, packages, and builds a Docker image.

Jenkins pushes the image to Alibaba Cloud registry.

Jenkins runs remote scripts to pull the image, stop old containers, and start new ones.

Testers are notified of the deployment result.

Build image with Maven and upload to Alibaba Cloud Docker registry

Add the Docker Maven plugin to pom.xml:

<plugin>
    <groupId>com.spotify</groupId>
    <artifactId>docker-maven-plugin</artifactId>
    <version>0.4.11</version>
    <configuration>
        <imageName>${docker.image.prefix}/${project.artifactId}</imageName>
        <imageTags>
            <imageTag>${project.version}</imageTag>
            <imageTag>latest</imageTag>
        </imageTags>
        <dockerDirectory>src/main/docker</dockerDirectory>
        <resources>
            <resource>
                <targetPath>/</targetPath>
                <directory>${project.build.directory}</directory>
                <include>${project.build.finalName}.jar</include>
            </resource>
        </resources>
    </configuration>
</plugin>
${docker.image.prefix} is the image prefix. ${project.artifactId} is the image name. ${project.version} is the version tag.

Create src/main/docker/Dockerfile:

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD demo-service-ver-0.0.1.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

Build and push the image: $ sudo mvn package docker:build Upload to Alibaba Cloud Docker registry (login, tag, push):

$ sudo docker login --username=USERNAME --password=PASSWORD registry-internal.cn-hangzhou.aliyuncs.com
$ sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/xxx/demo-service:[VERSION]
$ sudo docker push registry.cn-hangzhou.aliyuncs.com/xxx/demo-service:[VERSION]
Replace xxx with your registry namespace.

After pushing, the image appears in the registry:

Image list
Image list

Jenkins deployment configuration

Build a custom Jenkins image with Docker:

FROM jenkins
USER root
RUN apt-get update && apt-get install -y sudo && rm -rf /var/lib/apt/lists/*
RUN echo "jenkins ALL=NOPASSWD: ALL" >> /etc/sudoers
USER jenkins

Build and run the container:

$ sudo docker build -t buxiaoxia/jenkins:1.0 .
$ sudo docker run --memory 1.5G --name buxiaoxia-jenkins \
    -p 18181:8080 -p 50000:50000 -u root -d \
    --env JAVA_OPTS="-Xms256m -Xmx512m -XX:MaxNewSize=256m" \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v /usr/bin/docker:/usr/bin/docker \
    -v /home/buxiaoxia/software/jenkins:/var/jenkins_home \
    -v /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7 \
    buxiaoxia/jenkins:1.0
-v /var/run/docker.sock:/var/run/docker.sock and -v /usr/bin/docker:/usr/bin/docker map the host Docker daemon into the container. -v /home/buxiaoxia/software/jenkins:/var/jenkins_home sets the Jenkins home directory on the host. -v /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7 provides missing libraries on CentOS 7.

If permission issues arise, either grant socket access: $ sudo chmod 777 /var/run/docker.sock or add the Jenkins user to the Docker group: $ sudo usermod -a -G docker jenkins After Jenkins starts, install required plugins:

Maven Integration plugin

docker-build-step

Docker plugin

GitLab Hook Plugin

GitLab Plugin

Create a Maven job, configure the source code repository, and add a build step that runs the following script to push the image and trigger remote deployment:

echo '================Start pushing image================'
sudo docker login --username=USERNAME --password=PASSWORD registry-internal.cn-hangzhou.aliyuncs.com
sudo docker push registry-internal.cn-hangzhou.aliyuncs.com/xxx/demo-service
echo '================End pushing image================'

echo '================Start remote launch================'
ssh [email protected] -tt << remotessh
cd /home/buxiaoxia/xiaw
./jenkins.sh registry-internal.cn-hangzhou.aliyuncs.com/xxx/demo-service
sudo docker login --username=USERNAME --password=PASSWORD registry-internal.cn-hangzhou.aliyuncs.com
sudo docker pull registry-internal.cn-hangzhou.aliyuncs.com/xxx/demo-service
sudo docker run -d -m 300m --name=demo-service-`date +%Y-%m-%d` --restart=always registry-internal.cn-hangzhou.aliyuncs.com/xxx/demo-service
echo "finished!"
exit
remotessh

echo '================End remote launch================'

The helper script jenkins.sh stops the previous container and starts the new one:

#!/bin/sh
sudo docker stop $(sudo docker ps | grep $1 | awk '{print $1}' | sed 's/%//g')

Configure GitLab webhook: install the GitLab plugin in Jenkins, enable the “Build when a change is pushed” trigger, copy the generated URL (shown in the screenshot), and paste it into the GitLab project’s webhook settings.

GitLab webhook configuration
GitLab webhook configuration

Summary

By following these steps, a functional CI/CD pipeline based on Jenkins and Docker is established. Developers only need to push a Git tag, and the code is automatically built, containerized, pushed to Alibaba Cloud, and deployed to the target server.

Existing Issues

Docker containers are managed independently without orchestration, requiring knowledge of server network locations.

Docker push/pull commands embed plain‑text Alibaba Cloud credentials.

No version‑rollback process is defined.

Shell scripts lack robust error handling.

Optimizations

Address the above problems by introducing Docker Swarm or Kubernetes for orchestration, using credential stores or IAM roles for secure registry access, implementing rollback strategies, and enhancing script robustness.

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.

Dockerci/cdDevOpsmavenAlibaba CloudJenkins
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.