Docker Compose for Full-Stack Apps: Node, React and Postgres

Managing multiple services—such as a frontend client, backend API server, and relational database—locally can often lead to environment discrepancies, dependency conflicts, and the infamous "it works on my machine" syndrome. Docker Compose solves this by allowing developers to define and run multi-container Docker applications using a single declarative YAML file.

This comprehensive guide walks through orchestrating a production-ready, full-stack application consisting of a React frontend, a Node.js/Express backend, and a PostgreSQL database, complete with persistent volumes and custom bridge networks.

1. Project Directory Structure

Before configuring Docker Compose, organize your repository into separate service directories for the client, server, and root configuration files.

C
my-fullstack-app/
├── client/              # React frontend (Vite or Create React App)
│   ├── Dockerfile
│   └── package.json
├── server/              # Node.js Express backend
│   ├── Dockerfile
│   └── package.json
└── docker-compose.yml   # Root orchestration file

2. Writing Dockerfiles for Client and Server

Each containerized service requires its own optimized Dockerfile. For development and production, multi-stage builds or simple development setups ensure fast iteration.

  • Node.js Server Dockerfile: Uses an official Node Alpine base image, installs production dependencies, and exposes port 5000.
  • React Client Dockerfile: Builds the static frontend bundle or runs the Vite dev server with polling enabled for hot module replacement.
C
# server/Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5000
CMD ["npm", "run", "dev"];

# client/Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "run", "dev", "--", "--host"];

3. Orchestrating Services with docker-compose.yml

The docker-compose.yml file defines our three core services: postgres, server, and client, along with shared volumes and network configurations.

C
version: '3.8'

services:
  postgres:
    image: postgres:15-alpine
    container_name: postgres_db
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: secretpassword
      POSTGRES_DB: app_db
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - app-net

  server:
    build: ./server
    container_name: express_server
    restart: always
    ports:
      - "5000:5000"
    environment:
      - PORT=5000
      - DATABASE_URL=postgres://admin:secretpassword@postgres:5432/app_db
    volumes:
      - ./server:/app
      - /app/node_modules
    depends_on:
      - postgres
    networks:
      - app-net

  client:
    build: ./client
    container_name: react_client
    restart: always
    ports:
      - "3000:3000"
    volumes:
      - ./client:/app
      - /app/node_modules
    depends_on:
      - server
    networks:
      - app-net

volumes:
  pgdata:

networks:
  app-net:
    driver: bridge;

4. Running and Managing the Full-Stack Stack

Once your configuration files are in place, spin up the entire multi-container environment with a single terminal command:

C
# Build and start all services in detached mode
docker compose up --d --build

# View live container logs
docker compose logs -f

# Stop and remove containers and networks
docker compose down;

Conclusion

Using Docker Compose to orchestrate full-stack applications—such as React, Node.js, and PostgreSQL—eliminates environment setup friction, ensures consistent behavior across development and production, and simplifies multi-container scaling.