DescriptionA pre-configured FastAPI project template designed to streamline development with Docker, PostgreSQL, and Visual Studio Code DevContainers. This template is perfect for developers who want to quickly get started with a scalable FastAPI application using a PostgreSQL database, all within a containerized, reproducible environment.
- FastAPI: High-performance framework for building APIs with Python 3.7+.
- PostgreSQL: Robust, open-source relational database for data management.
- Docker Compose: Simplified multi-container management for easy setup and isolated development environments.
- DevContainers: Seamless integration with Visual Studio Code, offering a consistent and reproducible development environment.
- Standard Docker Setup: Supports non-VS Code users with a straightforward Docker Compose configuration.
Before you begin, make sure you have the following installed:
- Docker & Docker Compose (for containerization)
- Visual Studio Code (optional, for DevContainer integration)
- Git (to clone the repository)
-
Clone the repository:
git clone https://github.com/junior92jr/fastapi-docker-postgres-devcontainer-seed.git cd fastapi-docker-postgres-devcontainer-seed -
Open the project in VS Code: Open the project folder in Visual Studio Code.
-
Automatically configure your DevContainer: Once inside VS Code, it will automatically prompt you to reopen the folder inside the DevContainer (defined in
.devcontainer/devcontainer.json). -
Run the application: Use Docker Compose to start the FastAPI app along with the PostgreSQL database by running:
docker-compose up --build
The application will be running and available at
http://localhost:<port>(where<port>is defined in your.envfile).
-
Clone the repository:
git clone https://github.com/junior92jr/fastapi-docker-postgres-devcontainer-seed.git cd fastapi-docker-postgres-devcontainer-seed -
Set up the
.envfile: Copy the example.env_samplefile to create your own.envfile.cp .env_sample .env
-
Edit the
.envfile (Optional): Open and edit the.envfile to update any environment variables (e.g., database configurations, API keys). -
Build and start the Docker containers:
For older Docker versions (using
docker-compose):docker-compose up --build
For newer Docker versions (using
docker compose):docker compose up --build
This will build the necessary images and start the containers, creating the FastAPI app connected to a PostgreSQL database.
-
Access the application: Once the containers are running, open a browser and go to
http://localhost:<port>(replace<port>with the port configured in.envordocker-compose.yml). -
Run in detached mode (Optional): To run the containers in the background, use the
-dflag:For older Docker versions:
docker-compose up --build -d
For newer Docker versions:
docker compose up --build -d
-
Stop the containers: To stop the containers when you're done, use:
For older Docker versions:
docker-compose down
For newer Docker versions:
docker compose down
-
Access the running container:
Use the following command to access the app container:
docker exec -it <container_name> /bin/bash
Replace
<container_name>with the name of your app container, which you can find by running:docker ps
-
Navigate to the project directory (if not already there):
cd /path/to/your/project -
Run the tests:
Execute the tests using
pytest:pytest
You can also specify specific test files or directories:
pytest path/to/test_file.py
-
Exit the container:
Once done, exit the container:
exit
- If you're using Docker Desktop, you can easily manage containers through the GUI interface.
- This template uses Docker Compose to manage multi-container environments for both development and production.
- The
.envfile includes various environment variables for configuration—make sure to customize it according to your needs. - To customize the FastAPI application, simply modify the code in the
appdirectory.
Feel free to fork this project and submit pull requests! Contributions are always welcome.