Docker helps you containerize single applications, while Docker Compose lets you manage multiple containers easily, perfect for full-stack apps like Angular, Spring Boot, MySQL, and many others.
February 27, 2025
Photo by Shubham Dhage on Unsplash
If you're getting into containerization, you’ve probably heard of Docker and Docker Compose, but what’s the difference?
While they both work with containers, they serve different purposes. Let’s break it down in a simple way.
Think of Docker as a shipping container for your app. It lets you package your code along with all dependencies into a single unit that runs anywhere.
If you have a basic application, like a simple Node.js or Python API, you can use Docker to package and run it easily.
Example: You have a backend API built with Spring Boot. With Docker, you create a container that includes the application, Java runtime, and all necessary libraries.
Below is a simple Dockerfile
to containerize a Spring Boot application:
# Use an official Java runtime as a parent image
FROM openjdk:17-jdk-slim
# Set the working directory inside the container
WORKDIR /app
# Copy the Spring Boot JAR file into the container
COPY target/myapp.jar app.jar
# Expose the application port
EXPOSE 8080
# Command to run the application
ENTRYPOINT ["java", "-jar", "app.jar"]
Once built, you can run it anywhere—on your laptop, a server, or the cloud by building and running the container with:
docker build -t myapp .
docker run -p 8080:8080 myapp
Now, what if your application needs more than one container?
Maybe you developed a full-stack application with Angular, Spring Boot, and MySQL, and all three need to run together seamlessly.
Instead of manually starting each container, Docker Compose lets you define everything in a single file and spin up the entire system with one command.
Example: Your full-stack app has:
Instead of running three separate docker run commands, you create a docker-compose.yml file that defines all three services.
Below is an example of a docker-compose.yml
file for an Angular, Spring Boot, and MySQL stack:
version: '3.8'
services:
backend:
image: my-spring-boot-app
build: ./backend
ports:
- "8080:8080"
depends_on:
- db
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/mydatabase
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: password
frontend:
image: my-angular-app
build: ./frontend
ports:
- "4200:4200"
depends_on:
- backend
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: mydatabase
ports:
- "3306:3306"
Then, a single command, 'docker-compose up --build
', brings everything to life in one go.
If you just need to run a single app, Docker alone is enough.
But when your project has multiple services that need to work together, Docker Compose is your best friend.
AWS and other cloud platforms rely on these tools, so understanding them helps you deploy and scale your projects efficiently.