Dockerize Spring Boot Applications for AWS
Learn how to Dockerize Spring Boot applications for AWS step by step. This guide covers Docker fundamentals, Dockerfile creation, multi-stage builds, Docker Compose, image optimization, Amazon ECR integration, ECS deployment, Kubernetes readiness, and production best practices.
Introduction
Modern cloud applications are rarely deployed directly as JAR files.
Instead, they are packaged as Docker containers that can run consistently across:
- Developer laptops
- QA environments
- Staging
- Production
- AWS ECS
- AWS EKS
- Amazon App Runner
- Kubernetes
- Docker Swarm
Docker solves the classic problem:
"It works on my machine, but not in production."
In this article, we will learn how to Dockerize a Spring Boot application and prepare it for deployment on AWS.
Learning Objectives
After completing this article, you will understand:
- What is Docker?
- Why Containers?
- Docker Architecture
- Docker Images
- Docker Containers
- Dockerfile
- Multi-Stage Builds
- Docker Compose
- Amazon ECR
- ECS Integration
- Kubernetes Readiness
- Production Best Practices
Why Docker?
Without Docker:
Developer Laptop
↓
QA Server
↓
Production
Different Configurations
Problems:
- Different Java versions
- Missing libraries
- Different operating systems
- Configuration drift
- Deployment failures
With Docker:
Developer
↓
Docker Image
↓
AWS
↓
Runs Identically
Benefits:
- Consistent deployments
- Isolation
- Portability
- Faster deployments
- Easier scaling
What is Docker?
Docker is a containerization platform that packages an application with all its dependencies.
A Docker container includes:
- Application
- JDK
- Libraries
- Configuration
- Runtime
Everything needed to run the application.
Docker Architecture
flowchart LR
DEV[Developer]
IMAGE[Docker Image]
CONTAINER[Docker Container]
AWS[AWS ECS / EKS]
DEV --> IMAGE
IMAGE --> CONTAINER
CONTAINER --> AWS
Enterprise Architecture
flowchart TD
Developer
GitHub
CI_CD
AmazonECR
AmazonECS
ALB
SpringBootContainer
Aurora
Developer --> GitHub
GitHub --> CI_CD
CI_CD --> AmazonECR
AmazonECR --> AmazonECS
AmazonECS --> ALB
ALB --> SpringBootContainer
SpringBootContainer --> Aurora
Docker Components
| Component | Description |
|---|---|
| Docker Engine | Container Runtime |
| Docker Image | Immutable Package |
| Docker Container | Running Image |
| Dockerfile | Build Instructions |
| Docker Hub | Public Registry |
| Amazon ECR | AWS Image Registry |
Container vs Virtual Machine
| Container | Virtual Machine |
|---|---|
| Lightweight | Heavy |
| Shares Host OS | Full Guest OS |
| Starts in Seconds | Starts in Minutes |
| Lower Memory | Higher Memory |
| Ideal for Microservices | Traditional Workloads |
Spring Boot Packaging
Build the application.
mvn clean package
Output:
target/
springboot-demo-0.0.1.jar
Project Structure
springboot-docker-demo
├── src
├── target
│ └── app.jar
├── Dockerfile
├── docker-compose.yml
└── pom.xml
Step 1 Install Docker
Verify installation.
docker --version
Example:
Docker version 27.x
Step 2 Create Dockerfile
Create file:
Dockerfile
Simple Dockerfile
FROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/app.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]
Build Image
docker build -t codewithvenu/springboot-demo:1.0 .
Output
Successfully built
Verify Images
docker images
Output
REPOSITORY
codewithvenu/springboot-demo
Run Container
docker run -p 8080:8080 codewithvenu/springboot-demo:1.0
Application
http://localhost:8080
Stop Container
docker ps
docker stop <container-id>
Multi-Stage Docker Build
Recommended for production.
FROM maven:3.9-eclipse-temurin-21 AS builder
WORKDIR /build
COPY . .
RUN mvn clean package -DskipTests
FROM eclipse-temurin:21-jre
WORKDIR /app
COPY --from=builder /build/target/*.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
Benefits:
- Smaller image
- Faster startup
- Better security
- No Maven in runtime image
Docker Layers
Base Image
↓
Dependencies
↓
Application
↓
Container
Docker caches layers to speed up builds.
Docker Ignore
Create:
.dockerignore
Example:
target/
.git/
.idea/
*.log
Reduces build context and image size.
Environment Variables
Pass runtime configuration.
docker run \
-e SPRING_PROFILES_ACTIVE=prod \
-p 8080:8080 \
codewithvenu/springboot-demo
Mount Volumes
docker run \
-v /logs:/app/logs \
codewithvenu/springboot-demo
Useful for log persistence.
Docker Compose
Create
docker-compose.yml
Example
version: "3.9"
services:
app:
image: codewithvenu/springboot-demo
ports:
- "8080:8080"
environment:
SPRING_PROFILES_ACTIVE: dev
Run
docker compose up
Spring Boot + PostgreSQL
version: "3.9"
services:
postgres:
image: postgres:16
environment:
POSTGRES_DB: appdb
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
app:
image: codewithvenu/springboot-demo
depends_on:
- postgres
Container Lifecycle
flowchart LR
Build
Image
Run
Container
Stop
Build --> Image
Image --> Run
Run --> Container
Container --> Stop
Docker Networking
Containers communicate using Docker networks.
Example
Spring Boot
↓
Docker Network
↓
PostgreSQL
Health Check
Dockerfile
HEALTHCHECK \
CMD curl -f http://localhost:8080/actuator/health || exit 1
Requires Spring Boot Actuator.
Amazon ECR
Amazon Elastic Container Registry stores Docker images securely.
Flow:
flowchart LR
Developer
DockerBuild
AmazonECR
AmazonECS
Developer --> DockerBuild
DockerBuild --> AmazonECR
AmazonECR --> AmazonECS
Push Image to ECR
Login
aws ecr get-login-password \
| docker login
Tag
docker tag \
codewithvenu/springboot-demo:1.0 \
123456789012.dkr.ecr.us-east-1.amazonaws.com/springboot-demo:1.0
Push
docker push \
123456789012.dkr.ecr.us-east-1.amazonaws.com/springboot-demo:1.0
ECS Deployment
Amazon ECS pulls the image directly from ECR.
Amazon ECR
↓
Amazon ECS
↓
Container Running
Kubernetes Ready
The same Docker image can be deployed on:
- Amazon EKS
- Kubernetes
- OpenShift
- Minikube
No code changes required.
Logging
Best practice:
Write logs to:
STDOUT
STDERR
CloudWatch automatically collects container logs.
Avoid writing logs only to local files.
Monitoring
Monitor:
- CPU
- Memory
- Container Restarts
- Health Checks
- Request Latency
- JVM Metrics
Using:
- CloudWatch
- Prometheus
- Grafana
Common Errors
Image Not Found
Verify:
docker images
Port Already Used
Example:
8080
Use another port.
Container Exits Immediately
Check logs.
docker logs <container-id>
Out Of Memory
Increase container memory.
or
Optimize JVM.
Production Architecture
flowchart TD
Developer
GitHub
GitHubActions
DockerBuild
AmazonECR
AmazonECS
ALB
SpringBootContainers
Aurora
CloudWatch
Developer --> GitHub
GitHub --> GitHubActions
GitHubActions --> DockerBuild
DockerBuild --> AmazonECR
AmazonECR --> AmazonECS
AmazonECS --> ALB
ALB --> SpringBootContainers
SpringBootContainers --> Aurora
SpringBootContainers --> CloudWatch
Best Practices
- Use Multi-Stage Builds
- Use JRE images instead of JDK images in production
- Keep images small
- Never store secrets in Docker images
- Use AWS Secrets Manager
- Use IAM Roles
- Use environment variables
- Enable health checks
- Scan images for vulnerabilities
- Push images to Amazon ECR
- Write logs to STDOUT
- Use immutable image tags
- Keep containers stateless
Developer Checklist
Before production deployment:
- Docker installed
- Dockerfile created
- Multi-stage build configured
.dockerignoreadded- Image built successfully
- Container tested locally
- Health checks enabled
- Image pushed to ECR
- Secrets externalized
- Logs sent to CloudWatch
- ECS/EKS deployment verified
Interview Questions
What is Docker?
Docker is a containerization platform that packages an application and its dependencies into a portable, lightweight container.
What is the difference between an Image and a Container?
| Image | Container |
|---|---|
| Blueprint | Running instance |
| Read-only | Read/Write runtime |
| Used to create containers | Executes the application |
Why use Multi-Stage Builds?
They separate the build environment from the runtime environment, producing smaller, more secure Docker images.
Why should Docker images be immutable?
Immutable images ensure consistent deployments, simplify rollbacks, and prevent configuration drift.
Why store images in Amazon ECR?
Amazon ECR is a secure, managed container registry that integrates directly with Amazon ECS, Amazon EKS, IAM, and CI/CD pipelines.
Can the same Docker image run on ECS and EKS?
Yes.
A Docker image is platform-independent and can be deployed to ECS, EKS, Kubernetes, App Runner, or any OCI-compatible container runtime.
Summary
In this article, we learned how to Dockerize Spring Boot applications for AWS.
We covered:
- Docker fundamentals
- Docker architecture
- Dockerfile creation
- Multi-stage builds
- Docker Compose
- Environment variables
- Health checks
- Amazon ECR
- ECS deployment
- Kubernetes readiness
- Logging
- Monitoring
- Production best practices
Docker is the foundation of modern cloud-native development. By packaging Spring Boot applications into optimized containers and storing them in Amazon ECR, developers can deploy consistently across AWS services such as ECS, EKS, and App Runner while improving scalability, portability, and operational efficiency.
Comments
Share a question, correction, or practical insight about this article.
Checking login status...
Loading approved comments...