Skip to main content

Docker Compose Setup

Docker Compose simplifies the deployment process by defining all container configuration in a single YAML file. Instead of remembering complex docker run commands, you can start the entire application with one command.

Complete Configuration

Here’s the complete docker-compose.yml file for this project:
version: '3.8' 
services: 
  app: 
    build: . 
    ports: 
      - "5000:5000" 
    container_name: miniproject

Configuration Breakdown

Docker Compose Version

version: '3.8'
Specifies the Docker Compose file format version. Version 3.8 provides:
  • Modern features and syntax
  • Compatibility with Docker Engine 19.03.0+
  • Support for all features used in this project

Service Definition

services: 
  app:
Defines a service named 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

build: .
What this does:
  • 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

ports: 
  - "5000:5000"
Format: "HOST_PORT:CONTAINER_PORT" What this does:
  • Maps port 5000 on your host machine to port 5000 in 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
Make sure port 5000 is not already in use on your host machine. If it is, change the host port: "8080:5000" would make the app available at http://localhost:8080.

Container Name

container_name: miniproject
What this does:
  • Assigns a friendly name miniproject to 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

1

Start the Application

Build and start the container in detached mode:
docker-compose up -d
What happens:
  1. Docker Compose reads docker-compose.yml
  2. Builds the image using the Dockerfile (if not already built)
  3. Creates and starts the container named miniproject
  4. Maps port 5000 to your host machine
  5. Runs in the background (-d flag)
Output:
Creating network "project_default" with the default driver
Building app
[Build output...]
Creating miniproject ... done
2

Access the Application

Once running, access the application:
http://localhost:5000
The Express server will serve the React frontend and handle API requests.
3

View Logs

Check application logs:
docker-compose logs -f
Or view logs for just the app service:
docker-compose logs -f app
The -f flag follows the logs in real-time.
4

Stop the Application

Stop the running container:
docker-compose down
What happens:
  • Stops the miniproject container
  • Removes the container
  • Removes the default network
  • Preserves the built image for faster restarts

Common Commands

docker-compose up -d

Rebuilding After Code Changes

When you modify the application code:
docker-compose up -d --build
The --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 arguments

Version Control

Configuration is stored in docker-compose.yml and tracked in Git

Consistency

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:
ports: 
  - "8080:5000"  # Access at localhost:8080

Container Won’t Start

Check the logs:
docker-compose logs app

Force Clean Rebuild

Remove everything and rebuild:
docker-compose down
docker-compose build --no-cache
docker-compose up -d

Next Steps

Docker Multi-Stage Build

Understand the Dockerfile used by Docker Compose

Jenkins Pipeline

Automate deployment with CI/CD

Build docs developers (and LLMs) love