Skip to main content
Docker Compose is a tool used to define and run multiple Docker containers as a single application stack. A RAG application may contain multiple components such as:
  • FastAPI application
  • FAISS vector index
  • Embedding model
  • Database
  • LLM service
Docker Compose manages these services using a single YAML configuration file.
This command can build and start the entire application stack.

1. Why Docker Compose?

A single Docker container can be started using:
However, applications often require multiple containers. Example:
Managing every container manually becomes difficult. Docker Compose simplifies this process.
Docker Compose can:
  1. Build images
  2. Create containers
  3. Create networks
  4. Create volumes
  5. Manage dependencies
  6. Start multiple services together

2. What is a Multi-Service Stack?

A multi-service stack is an application consisting of multiple services running independently. Example:
Each service has a specific responsibility.

3. What is Docker Compose?

Docker Compose uses a YAML configuration file. Common filenames:
or:
The file can define:
  • Services
  • Images
  • Build instructions
  • Ports
  • Volumes
  • Networks
  • Environment variables
  • Dependencies
  • Health checks
Basic structure:

4. Main Docker Compose Concepts

4.1 Services

A service represents an application component. Example:
This creates two services:
Each service can have its own:
  • Dockerfile
  • Dependencies
  • Ports
  • Environment variables
  • Volumes

4.2 Build

The build instruction tells Docker Compose how to build an image.
The context points to the directory containing the application files. Example:
Flow:

4.3 Port Mapping

Ports expose a container application to the host machine. Example:
Format:
Architecture:

5. Docker Volumes

Containers are temporary. Data stored inside a container can be lost when the container is removed. Docker volumes provide persistent storage. Example:
This creates a named volume:

5.1 Shared Volumes

A shared volume allows multiple containers to access the same data. Example:
Architecture:
The FAISS index can be stored as:

6. depends_on

The depends_on instruction defines service dependencies. Example:
This means:
This is useful when the API requires the FAISS index before starting.

7. Docker Compose Networking

Docker Compose automatically creates a network for services. Example:
Containers can communicate using service names. Example:
The service name acts as a hostname. Example:

8. Environment Variables

Environment variables store configuration values. Example:
Python can access them using:
Useful for:
  • API keys
  • Database URLs
  • Application configuration
  • Model names
  • File paths
Sensitive values should not be hardcoded in source code.

9. Restart Policies

Docker Compose can automatically restart containers. Example:
Common policies:

10. Health Checks

Health checks monitor whether a container is functioning correctly. Example:
Possible states:
or:

11. FastAPI + FAISS Architecture

This example contains two services.

Service 1: FastAPI

Responsible for:
  • Receiving HTTP requests
  • Loading the FAISS index
  • Providing API endpoints

Service 2: Vector Store

Responsible for:
  • Loading documents
  • Creating embeddings
  • Creating a FAISS index
  • Saving the index

Shared Volume

Responsible for:
  • Storing the FAISS index
  • Sharing the index between containers
Architecture:

12. Important Note About FAISS

FAISS is a vector similarity search library. It is not typically used as a standalone database server. For this Docker Compose example, the architecture is:
This architecture is useful for learning:
  • Docker Compose
  • Multi-container applications
  • Shared volumes
  • Service dependencies

13. Project Structure


14. Example: FastAPI Application

api/llm_api.py

Explanation

Imports the FastAPI framework.
Creates the FastAPI application.
Creates a GET endpoint for the root URL.
Creates a health-check endpoint. Endpoints:

15. API Requirements

api/requirements.txt

Explanation


16. API Dockerfile

api/Dockerfile

Step-by-Step Explanation

Base Image

Uses Python 3.11 as the base image.

Working Directory

Sets /app as the working directory.

Copy Requirements

Copies the dependency file.

Install Dependencies

Installs required Python packages.

Copy Application

Copies the application files.

Expose Port

Documents that the application uses port 8000.

Start Application

Starts the FastAPI server.

17. Example: FAISS Index Builder

vector_store/build_index.py


Step 1: Import Libraries

  • os handles directories
  • faiss creates the vector index
  • SentenceTransformer creates embeddings

Step 2: Create Documents

Documents represent the knowledge source.

Step 3: Load Embedding Model

The model converts text into numerical vectors.

Step 4: Create Embeddings

Each document becomes a vector.

Step 5: Get Vector Dimension

Gets the number of values in each embedding.

Step 6: Create FAISS Index

Creates a FAISS similarity index. IndexFlatIP performs inner-product similarity search. Because embeddings are normalized:

Step 7: Add Embeddings

Stores embeddings inside the FAISS index.

Step 8: Create Directory

Creates the directory used for storing the FAISS index.

Step 9: Save FAISS Index

Saves the vector index. The resulting file:
This file will be stored inside the shared Docker volume.

18. Vector Store Requirements

vector_store/requirements.txt


19. Vector Store Dockerfile

vector_store/Dockerfile

Flow:

20. Complete Docker Compose File

docker-compose.yml


21. Understanding the Docker Compose File

API Service

Defines the FastAPI service.

Build Context

Builds the API image using:

Container Name

Assigns a custom name to the API container.

Port Mapping

Maps:

Volume

Mounts the shared volume. Both services access:

Dependency

Ensures the FAISS index builder completes before the API starts.

Vector Store Service

Builds the vector store container. Its job is:

22. Load the FAISS Index in FastAPI

The FastAPI application should load the generated index.

Updated api/llm_api.py

Flow:

23. Start the Application

Run from the project root:
Docker will:

Run in Background

-d means detached mode.

24. Check Running Containers

Expected:
The faiss-index container exiting is expected. Its job is only:

25. View Logs

View all logs:
Follow logs:
View API logs:
View vector store logs:

26. Test the API

Open:
Expected:
Health endpoint:
Expected:
FastAPI documentation:

27. Docker Compose vs Dockerfile

Relationship:

28. Docker Compose vs docker run

Without Docker Compose:
With Docker Compose:
Docker Compose stores the infrastructure configuration in one file.

29. Important Docker Compose Commands

Start Services

Build and Start

Run in Background

Stop Services

Check Services

View Logs

Follow Logs


30. Important Concepts Summary


31. Complete Deployment Flow


32. Key Takeaways

  1. Docker Compose manages multiple containers using one configuration file.
  2. Services represent different components of an application.
  3. The build option builds Docker images.
  4. Ports expose applications outside containers.
  5. Volumes provide persistent storage.
  6. Shared volumes allow containers to access common data.
  7. depends_on controls service dependencies.
  8. Docker Compose automatically creates networks.
  9. Environment variables provide configuration.
  10. Health checks monitor container availability.
  11. FastAPI can load a FAISS index stored in a shared Docker volume.
  12. Docker Compose simplifies multi-service deployment.

Final Architecture