Docker Compose Setup
Docker Compose simplifies the deployment process by defining all container configuration in a single YAML file. Instead of remembering complexdocker run commands, you can start the entire application with one command.
Complete Configuration
Here’s the completedocker-compose.yml file for this project:
Configuration Breakdown
Docker Compose Version
- Modern features and syntax
- Compatibility with Docker Engine 19.03.0+
- Support for all features used in this project
Service Definition
app. In Docker Compose:
- A service is a container configuration
- You can define multiple services (databases, caching layers, etc.)
- This project has a single service containing both frontend and backend
Build Context
- Tells Docker Compose to build an image from the Dockerfile in the current directory (
.) - Automatically runs the multi-stage build process
- Creates an image that can be used to start containers
The build uses the multi-stage Dockerfile with three stages: frontend build, backend build, and production assembly.
Port Mapping
"HOST_PORT:CONTAINER_PORT"
What this does:
- Maps port
5000on your host machine to port5000in the container - Makes the application accessible at
http://localhost:5000 - The Express server listens on port 5000 inside the container
- The React app is served as static files from the Express server
Container Name
- Assigns a friendly name
miniprojectto the container - Makes it easy to reference:
docker logs miniproject,docker stop miniproject - Without this, Docker generates a random name like
project_app_1
Using Docker Compose
Start the Application
Build and start the container in detached mode:What happens:
- Docker Compose reads
docker-compose.yml - Builds the image using the Dockerfile (if not already built)
- Creates and starts the container named
miniproject - Maps port 5000 to your host machine
- Runs in the background (
-dflag)
Access the Application
Once running, access the application:The Express server will serve the React frontend and handle API requests.
View Logs
Check application logs:Or view logs for just the app service:The
-f flag follows the logs in real-time.Common Commands
Rebuilding After Code Changes
When you modify the application code:--build flag forces a rebuild of the image before starting the container.
Docker uses layer caching, so rebuilds are usually fast if dependencies haven’t changed. Only modified layers are rebuilt.
Advantages of Docker Compose
Simple Commands
Start everything with one command instead of complex
docker run argumentsVersion Control
Configuration is stored in
docker-compose.yml and tracked in GitConsistency
Everyone on the team uses the same configuration
Easy Scaling
Add databases, Redis, or other services easily
Troubleshooting
Port Already in Use
If port 5000 is already in use:Container Won’t Start
Check the logs:Force Clean Rebuild
Remove everything and rebuild:Next Steps
Docker Multi-Stage Build
Understand the Dockerfile used by Docker Compose
Jenkins Pipeline
Automate deployment with CI/CD