Overview
This guide covers deploying the Electronic Invoice Processing API using Docker containers. Docker provides a consistent, isolated environment for running the application across different platforms.Prerequisites
Before starting, ensure you have:Docker
Docker Engine 20.10+ installedDownload Docker
Docker Compose
Docker Compose v2.0+ installedInstall Compose
Project Files
Dockerfile
The project includes a Dockerfile that defines the container image:dockerfile
Dockerfile Breakdown
Dockerfile Breakdown
Base Image:
python:3.9-slim- Lightweight Python 3.9 image
- Minimal size for faster builds and deployments
/app- All application files stored here
- Copies
requirements.txtfirst (layer caching) - Installs Python packages with
--no-cache-dirflag
- Copies all source files to container
8000- FastAPI/Uvicorn default port
- Runs Uvicorn ASGI server
- Binds to all network interfaces (0.0.0.0)
Docker Compose Configuration
Thedocker-compose.yml file simplifies container orchestration:
docker-compose.yml
Configuration Breakdown
Configuration Breakdown
Version:
3.8- Docker Compose file format version
web- Single service for the API
. (current directory)- Builds from local Dockerfile
8000:8000- Host port 8000 → Container port 8000
.:/app- Live code synchronization (development mode)
- Changes reflect immediately without rebuild
- Uses same Uvicorn command as Dockerfile
Deployment Methods
- Docker Compose (Recommended)
- Docker CLI
Using Docker Compose
Docker Compose is the recommended method for local development and simple deployments.Test the API
Managing the Service
Port Configuration
Default Port Mapping
The application uses port8000 by default:
Changing the Host Port
If port 8000 is already in use, modify the port mapping:Accessing on Custom Port
After changing to port 8080:- API Docs: http://localhost:8080/docs
- Endpoint:
http://localhost:8080/process_excel
Volume Configuration
Development Volume
The default volume mount enables live code updates:Development Mode Benefits:
- Code changes reflect immediately
- No need to rebuild container
- Faster iteration cycle
Production Volume
For production, remove the volume mount or use specific volumes:Environment Variables
Adding Environment Variables
You can configure the application using environment variables:Example .env File
.env
Health Checks and Monitoring
Adding Health Checks
Enhance your docker-compose.yml with health checks:docker-compose.yml
Viewing Health Status
(healthy) status.
Viewing Logs
Container Resource Usage
Production Considerations
Remove Development Volume
Remove or restrict volume mounts to prevent code modification in production.
Configure CORS Properly
Restrict CORS origins to specific domains. See CORS Configuration.
Use Environment Variables
Store sensitive configuration in environment variables, not in code.
Enable Health Checks
Implement health check endpoints for container orchestration.
Set Resource Limits
Define CPU and memory limits to prevent resource exhaustion.
Use Specific Tags
Avoid
latest tag in production. Use specific version tags.Resource Limits Example
docker-compose.yml
Troubleshooting
Port Already in Use
Port Already in Use
Error:
Bind for 0.0.0.0:8000 failed: port is already allocatedSolution:-
Find process using the port:
-
Kill the process or change port mapping:
Build Fails on Dependencies
Build Fails on Dependencies
Error:
ERROR: Could not find a version that satisfies the requirement...Solution:- Ensure
requirements.txtis present and valid - Verify Python version compatibility
- Try building with no cache:
Container Exits Immediately
Container Exits Immediately
Error: Container status shows
Exited (1)Solution:-
Check logs for errors:
-
Verify
main.pyexists and is valid - Test locally without Docker first
Cannot Access API
Cannot Access API
Error:
Connection refused or timeoutSolution:-
Verify container is running:
- Check port mapping is correct
- Ensure firewall allows port 8000
-
Try accessing from container:
Code Changes Not Reflected
Code Changes Not Reflected
Error: Updated code doesn’t run in containerSolution:
-
Verify volume mount is configured:
-
Restart the container:
-
If still not working, rebuild:
Next Steps
CORS Configuration
Configure CORS for production security
API Reference
Learn how to use the API endpoints
Excel Format
Understand required Excel file format
Docker Documentation
Official Docker documentation