This project is a template for creating Flask applications configured to work with HTTPS using Docker, Miniconda, and DigitalOcean Secrets Manager.
myproject/
├── backend/
│ ├── app/
│ │ ├── init.py
│ │ ├── api/
│ │ │ ├── init.py
│ │ │ ├── routes.py
│ ├── config.py
│ ├── run.py
│ ├── tests/
│ │ ├── test_routes.py
├── docker/
│ ├── development/
│ │ ├── Dockerfile
│ │ ├── docker-compose.yml
│ │ ├── nginx/
│ │ │ ├── nginx.conf
│ │ │ ├── certs/
│ │ │ │ ├── server.crt
│ │ │ │ ├── server.key
│ ├── production/
│ │ ├── Dockerfile
│ │ ├── docker-compose.yml
│ │ ├── .env
│ │ ├── get_secrets.py
│ │ ├── nginx/
│ │ │ ├── nginx.conf
│ │ │ ├── certs/
├── environment-dev.yml
├── environment-prod.yml
├── .gitignore
└── README.md
- Docker
- Docker Compose
git clone https://github.com/richardesp/flask-conda-rest-template
cd flask-conda-rest-template
Navigate to the development directory and bring up the services with Docker Compose.
cd docker/development
docker-compose up --build
Changes in the code within the backend/ directory will automatically reflect due to volume mounting.
To execute commands inside the web container:
docker-compose exec web /bin/sh
To run the tests:
docker-compose exec web pytest
Add new dependencies in the environment-dev.yml file. Rebuild the image to install the new dependencies.
docker-compose build web
docker-compose up -d
Store your certificates (server.crt and server.key) in DigitalOcean Secrets Manager.
Create the .env file in docker/production/ and add your environment variables (you have an example to fill in directly).
Navigate to the production directory and bring up the services with Docker Compose.
cd docker/production
docker-compose up --build
To execute commands inside the web container:
docker-compose exec web /bin/sh
Add new dependencies in the environment-prod.yml file. Rebuild the image to install the new dependencies.
docker-compose build web
docker-compose up -d
Nginx is used to handle HTTPS traffic. The configuration can be found in:
- docker/development/nginx/nginx.conf
- docker/production/nginx/nginx.conf
The certificates for the development environment should be located at:
docker/development/nginx/certs/server.crt
docker/development/nginx/certs/server.key
For production, they are retrieved from DigitalOcean Secrets Manager.
- Do not upload certificates or sensitive files to the repository. Ensure that .env files and certificates are listed in .gitignore.
- Keep dependencies up to date. Update the environment-dev.yml and environment-prod.yml files when adding new dependencies and rebuild the images.
- Use a virtual environment. In local development environments, consider using a virtual environment (venv) to manage dependencies.
- Documentation: Maintain comprehensive documentation for your project, including setup instructions, API documentation, and examples of usage. This helps new developers understand and contribute to the project quickly.
- CI/CD Integration: Implement Continuous Integration and Continuous Deployment (CI/CD) pipelines to automate testing, building, and deployment processes. This ensures code quality and streamlines deployment.
- Code Quality: Use linters and formatters to maintain code quality and consistency. Tools like flake8, black, and pylint can be integrated into your development workflow.
- Security: Regularly review and update your dependencies to mitigate security vulnerabilities. Use tools like Dependabot to automate this process.
- Monitoring and Logging: Implement monitoring and logging to track the performance and errors in your application. Tools like Prometheus, Grafana, and ELK stack (Elasticsearch, Logstash, Kibana) can be useful.
- Scalability: Design your application with scalability in mind. Use Docker Swarm or Kubernetes for orchestration and consider using cloud services to handle scaling.
For any questions or support, contact me via GitHub (richardesp).