Spring Boot Deployment
Deploying a Spring Boot app means packaging it and running it on a server or cloud platform. Spring Boot's embedded server makes this straightforward — the app ships as a single file that runs anywhere Java is installed.
Packaging Options
Package Type Contents How to Run
──────────── ──────────────────────────────── ────────────────────────────
JAR (default) App code + all dependencies java -jar app.jar
+ embedded Tomcat
WAR App code only (no Tomcat) Deploy to external Tomcat
(legacy approach)
Use JAR for almost all modern deployments. The fat JAR contains everything — no external dependencies to install on the server.
Building the JAR
# Using Maven mvn clean package # The output file appears at: target/demo-0.0.1-SNAPSHOT.jar
# Skip tests to build faster during deployment pipelines mvn clean package -DskipTests
Running the JAR
# Basic run java -jar target/demo-0.0.1-SNAPSHOT.jar # Run with a specific profile java -jar app.jar --spring.profiles.active=prod # Override any property at runtime java -jar app.jar --server.port=9090 --spring.datasource.url=jdbc:mysql://...
Deployment to a Linux Server
STEP 1: Build the JAR on your machine mvn clean package -DskipTests STEP 2: Copy to the server scp target/app.jar ubuntu@your-server:/opt/myapp/ STEP 3: Create a systemd service so the app runs as a background process (on the server) STEP 4: Start the app sudo systemctl start myapp sudo systemctl enable myapp ← Auto-start on server reboot
Running with Docker
Docker packages your app and its runtime environment into a container image. The image runs identically on any machine with Docker installed.
# Dockerfile (place in project root) FROM eclipse-temurin:17-jdk-alpine ← Base image with Java 17 WORKDIR /app COPY target/demo-0.0.1-SNAPSHOT.jar app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "app.jar"]
# Build the image docker build -t estudy247/my-app:1.0 . # Run the container docker run -d -p 8080:8080 \ -e SPRING_PROFILES_ACTIVE=prod \ -e DB_PASSWORD=secret \ estudy247/my-app:1.0
Docker + Environment Variables
Docker Container
┌──────────────────────────────────────────────┐
│ Spring Boot App │
│ │
│ Reads: SPRING_PROFILES_ACTIVE=prod │
│ Reads: DB_PASSWORD=secret │
│ Reads: SERVER_PORT=8080 │
│ │
└──────────────────────────────────────────────┘
↑ injected at container start
Deployment to AWS
Option What It Is Best For ───────────────────── ───────────────────────────────── ───────────────────── EC2 Virtual server — full control Custom setups Elastic Beanstalk Upload JAR → AWS manages infra Quick deployment ECS (Fargate) Run Docker containers Containerized apps Lambda Serverless — pay per request Lightweight functions
Deployment to Render / Railway (Simple Option)
- Push your code to GitHub
- Connect your GitHub repo to Render or Railway
- Set environment variables in the platform dashboard
- The platform builds and deploys automatically on every push
Health Check for Load Balancers
Production deployments typically sit behind a load balancer. Configure it to check your Actuator health endpoint:
Load Balancer Health Check: GET /actuator/health
Expected response: { "status": "UP" }
If "DOWN" → load balancer routes traffic away from this instance
If "UP" → load balancer sends traffic to this instance
Deployment Checklist
Before Going Live Done? ───────────────────────────────── ───── Set spring.profiles.active=prod □ Set ddl-auto=validate or none □ Move all secrets to env variables □ Enable only needed actuator endpoints□ Set logging.level.root=WARN □ Configure HTTPS (SSL certificate) □ Set up database connection pool □ Configure proper error messages □
Summary
- Spring Boot apps package into a single fat JAR that includes the embedded server
- Run with
java -jar app.jar— no separate server installation needed - Docker containerizes the app for consistent deployment across any environment
- Use environment variables to pass secrets and environment-specific config at runtime
- Always use the
prodprofile in production and disableddl-autotable creation
