A template repository with an API developed in Python using FastAPI framework
-
Clone the repository
-
With
HTTP
# With HTTPS git clone https://github.com/LucasVmigotto/python-fastapi.git
-
With
SSH
git clone git@github.com:LucasVmigotto/python-fastapi.git
-
-
Open the project with Visual Studio Code:
code <project folder>
If the project not already open inside a container, use
CTRL
+Shift
+P
and run the Dev Containers: Rebuild Container Without Cache command -
Happy coding 😁
-
List all Docker containers
docker ps -a
-
Remove Docker Compose containers
docker compose rm --stop -f
-
Prune containers
docker container prune --force
-
List all Docker images
docker ls -a
-
Remove Docker dangling images
docker image rm -f $(docker image ls --filter "dangling=true" -aq)
WARNING: If you want to remove ALL Docker images, just remove the
--filter
flag and argumentdocker image rm -f $(docker image ls -aq)
-
List all Docker volumes
docker volume ls
-
Prune Docker volumes
docker volume prune --force
-
Run the following command, inside the Visual Studio Code terminal, to start the application:
poetry run python \ -m uvicorn \ --host 0.0.0.0 \ --port 8001 \ --log-level debug \ --reload
Be careful after running this command with the port in use to not get any conflict using the Docker Compose service
To make sure, run the application only with the Docker Compose service, and use the Visual Studio Code dev container exclusive as code environment.
-
Run the following command to start the container service:
docker compose up api
You can use
docker compose -d up api
to run in detached mode. Although, to be able to see any server logs, you will need to rundocker compose logs -f api
Once successfully started, the service will be available in localhost:8000
-
Run the following command in the first level of the project's folder:
docker build \ -f Dockerfile --tag python-fastapi \ --no-cache \ .
If necessary, add the --progress plain to see all build output
-
You can test the container with:
docker run \ --publish 8000:80 \ --rm \ python-fastapi
Give it a try in localhost:8000
-