Deploy Spring Boot with Jenkins, Docker, and Nginx Using Docker‑Compose
This guide walks through setting up a Jenkins container to build and deploy a Spring Boot application, creating Docker and Docker‑Compose configurations, integrating Nginx as a reverse proxy, and automating the CI/CD workflow with detailed scripts and best‑practice recommendations.
Docker isolates resources using Linux namespaces, controls them with cgroups, and employs copy‑on‑write for efficient file handling, making it ideal for creating disposable environments, micro‑service architectures, and consistent deployments.
The author explores practical questions about using Jenkins inside a Docker container to build and deploy a Spring Boot project, comparing direct IDE Docker plugins, Jenkins scripts, and the need for additional tools such as Git and Maven.
Key responsibilities identified:
Jenkins: receives code updates, builds the project, and triggers Docker scripts.
Docker: builds required images and runs containers.
Git: synchronizes source code.
Environment Setup Steps
Install JDK.
Install Maven.
Install Git.
Install Jenkins (adjust /etc/sysconfig/jenkins to run as root if needed).
Install Docker on CentOS.
Install Docker‑Compose.
Install Docker‑Compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --versionConfiguration Files
1. SpringBoot Dockerfile
FROM java:8
MAINTAINER Wilson
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
VOLUME /ecs-application-docker
RUN mkdir /app
WORKDIR /app
COPY target/ecs-application.jar ecs-application.jar
EXPOSE 9090
ENTRYPOINT ["java","-jar","ecs-application.jar"]2. docker‑compose.yml
version: '3.7'
services:
app:
restart: always
build: ./
hostname: docker-spring-boot
container_name: docker-spring-boot
image: docker-spring-boot/latest
volumes:
- ./volumes/app:/app
nginx:
depends_on:
- app
container_name: docker-nginx
hostname: docker-nginx
image: nginx:1.17.6
environment:
TZ: Asia/Shanghai
restart: always
expose:
- 80
ports:
- 80:80
links:
- app
volumes:
- ./volumes/nginx/nginx.conf:/etc/nginx/nginx.conf
- ./volumes/nginx/conf.d:/etc/nginx/conf.d
- ./volumes/nginx/logs:/var/log/nginx3. Nginx Configuration (default.conf)
user nginx;
worker_processes 2;
error_log /etc/nginx/error.log crit;
pid /etc/nginx/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" "$http_cookie"';
access_log /var/log/nginx/access.log main;
upstream application {
server docker-spring-boot:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://application;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}Jenkins Deployment Script
cd /var/lib/jenkins/workspace/docker-spring-boot/spring-boot-nginx-docker-demo
mvn clean package
if [ -e "./volumes/app/docker-spring-boot.jar" ]; then
rm -f ./volumes/app/docker-spring-boot.jar && cp ./target/docker-spring-boot.jar ./volumes/app/docker-spring-boot.jar && docker restart docker-spring-boot && echo "update restart success"
else
mkdir -p ./volumes/app && cp ./target/docker-spring-boot.jar ./volumes/app/docker-spring-boot.jar && docker-compose -p docker-spring-boot up -d && echo "first start"
fiRunning docker-compose up builds the images and starts the containers; verify with docker ps.
Testing:
SpringBoot: access http://<em>host</em>:8080/swagger-ui.html or check logs in /volumes/app/logs.
Nginx reverse proxy: access http://<em>host</em>/swagger-ui.html and view Nginx logs in /volumes/nginx/logs.
Advantages of this container‑based CI/CD setup:
High security – each application runs in its own isolated container.
Unified configuration eliminates port, path, and version conflicts.
Easy migration – all environment, dependency, and version details are captured in Dockerfiles and compose files, avoiding manual installation on new hosts.
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.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
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.
