Docker Compose Recommended

This page provides instructions for installing and running SparkyFitness using Docker Compose. This method is recommended for most users as it simplifies the setup process.

Prerequisites

Before you begin, ensure you have the following installed:

Installation Steps

  1. Create a new folder and download Docker files: Create a new directory for SparkyFitness and navigate into it. Then, download the necessary docker-compose.yml and .env files.
    mkdir sparkyfitness && cd sparkyfitness
    curl -o docker-compose.yml https://raw.githubusercontent.com/CodeWithCJ/SparkyFitness/main/docker/docker-compose.prod.yml
    curl -o .env https://raw.githubusercontent.com/CodeWithCJ/SparkyFitness/main/docker/.env.example
    
  2. Configure Environment Variables: The docker-compose.yml file relies on environment variables defined in a .env file. Open the downloaded .env file in a text editor and update the variables to customize your setup. It's crucial to configure these variables correctly for the application to function as expected.
    For a complete list of all available variables and their detailed descriptions, please refer to the example environment file: .env.example on GitHub
    For a comprehensive list of all available environment variables and their detailed descriptions, please refer to the Environment Variables documentation.
  3. Start the Application: From within the sparkyfitness directory (where docker-compose.yml is located), pull the latest images and start the application services:
    docker compose pull && docker compose up -d
    
    • docker compose pull: Downloads the latest Docker images for the services.
    • docker compose up -d: Starts the services defined in the docker-compose.yml file in detached mode (in the background).

Services Overview

The docker-compose.prod.yml file defines three main services:

  • sparkyfitness-db:
    • Image: postgres:15-alpine
    • Purpose: The PostgreSQL database server for storing application data.
    • Data Persistence: Data is persisted in a Docker volume mapped to ../postgresql on your host, ensuring your data is not lost if containers are removed.
  • sparkyfitness-server:
    • Image: codewithcj/sparkyfitness_server:latest
    • Purpose: The backend Node.js application server.
    • Environment Variables: Configured with necessary database connection details, logging level, API encryption key, JWT secret, and frontend URL.
    • Dependencies: Depends on sparkyfitness-db to ensure the database is running before the server starts.
  • sparkyfitness-frontend:
    • Image: codewithcj/sparkyfitness:latest
    • Purpose: The frontend React application served by Nginx.
    • Ports: Maps host port 3004 to container port 80 (Nginx), making the frontend accessible via http://localhost:3004 (or your configured domain).
    • Dependencies: Depends on sparkyfitness-server to ensure the backend is running.

Accessing the Application

Once all services are up and running, you can access the SparkyFitness frontend in your web browser at the URL you configured for SPARKY_FITNESS_FRONTEND_URL (e.g., http://localhost:3004).

Stopping and Removing Services

To stop the running services without removing their data volumes:

docker compose stop

To stop and remove all services, networks, and volumes (this will delete your database data!):

docker compose down -v