- FastAPI application
- FAISS vector index
- Embedding model
- Database
- LLM service
1. Why Docker Compose?
A single Docker container can be started using:- Build images
- Create containers
- Create networks
- Create volumes
- Manage dependencies
- 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:3. What is Docker Compose?
Docker Compose uses a YAML configuration file. Common filenames:- Services
- Images
- Build instructions
- Ports
- Volumes
- Networks
- Environment variables
- Dependencies
- Health checks
4. Main Docker Compose Concepts
4.1 Services
A service represents an application component. Example:- Dockerfile
- Dependencies
- Ports
- Environment variables
- Volumes
4.2 Build
Thebuild instruction tells Docker Compose how to build an image.
4.3 Port Mapping
Ports expose a container application to the host machine. Example: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:5.1 Shared Volumes
A shared volume allows multiple containers to access the same data. Example:6. depends_on
The depends_on instruction defines service dependencies.
Example:
7. Docker Compose Networking
Docker Compose automatically creates a network for services. Example:8. Environment Variables
Environment variables store configuration values. Example:- API keys
- Database URLs
- Application configuration
- Model names
- File paths
9. Restart Policies
Docker Compose can automatically restart containers. Example:10. Health Checks
Health checks monitor whether a container is functioning correctly. Example: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
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:- Docker Compose
- Multi-container applications
- Shared volumes
- Service dependencies
13. Project Structure
14. Example: FastAPI Application
api/llm_api.py
Explanation
15. API Requirements
api/requirements.txt
Explanation
16. API Dockerfile
api/Dockerfile
Step-by-Step Explanation
Base Image
Working Directory
/app as the working directory.
Copy Requirements
Install Dependencies
Copy Application
Expose Port
Start Application
17. Example: FAISS Index Builder
vector_store/build_index.py
Step 1: Import Libraries
oshandles directoriesfaisscreates the vector indexSentenceTransformercreates embeddings
Step 2: Create Documents
Step 3: Load Embedding Model
Step 4: Create Embeddings
Step 5: Get Vector Dimension
Step 6: Create FAISS Index
IndexFlatIP performs inner-product similarity search.
Because embeddings are normalized:
Step 7: Add Embeddings
Step 8: Create Directory
Step 9: Save FAISS Index
18. Vector Store Requirements
vector_store/requirements.txt
19. Vector Store Dockerfile
vector_store/Dockerfile
20. Complete Docker Compose File
docker-compose.yml
21. Understanding the Docker Compose File
API Service
Build Context
Container Name
Port Mapping
Volume
Dependency
Vector Store Service
22. Load the FAISS Index in FastAPI
The FastAPI application should load the generated index.Updated api/llm_api.py
23. Start the Application
Run from the project root:Run in Background
-d means detached mode.
24. Check Running Containers
faiss-index container exiting is expected.
Its job is only:
25. View Logs
View all logs:26. Test the API
Open:27. Docker Compose vs Dockerfile
Relationship:
28. Docker Compose vs docker run
Without Docker Compose:
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
- Docker Compose manages multiple containers using one configuration file.
- Services represent different components of an application.
- The
buildoption builds Docker images. - Ports expose applications outside containers.
- Volumes provide persistent storage.
- Shared volumes allow containers to access common data.
depends_oncontrols service dependencies.- Docker Compose automatically creates networks.
- Environment variables provide configuration.
- Health checks monitor container availability.
- FastAPI can load a FAISS index stored in a shared Docker volume.
- Docker Compose simplifies multi-service deployment.