- FastAPI
- MySQL
- Docker
Please install Docker
and Docker compose
first.
After installation, run the following command to create a local Docker container.
docker-compose up
If Docker is running successfully, the API and DB server will be launched as shown in the following:
- API server: http://localhost:8000
- API Docs: http://localhost:8000/v1/docs
- DB server: http://localhost:3306
Be careful, it won't work if the port is occupied by another application.
The above setup can be used for development, but you can also setup dev env with using the VS Code Dev Containers extension.
- Install VS code and the Dev Containers extension.
- Run the
Dev Containers: Open Folder in Container...
command from the Command Palette or quick actions Status bar item, and select the project folder. - Wait until the building of the application is finished, then access the application url
If you're VS Code user, you can easily setup Python code formatter (black) and linter (flake8) by simply installing the extensions.
Automatic formatting settings have already been defined .vscode/settings.json
.
Just install following:
If you are using the Dev Container, this configuration is already done in the Dev Container settings, so you can skip it.
Use following command to go inside of docker container:
docker-compose exec mysql sh
Then use mysql
command to execute a query:
mysql -u root -p
mysql> USE fastapi_app;
mysql> SHOW TABLES;
mysql> SELECT * FROM user;
Your initial MySQL password is defined in mysql/local.env
.
Python libraries used in this app are defined in api/requirements.txt
.
Also you may want to add libraries such as requests, in which case follow these steps:
- Add the library to requirements.txt
e.g., if you want to add requests
:
requests==2.30.0
Then try a re-build and see.
docker-compose down
docker-compose build
docker-compose up
Some of environment variable, like a database name and user is defined in docker-compose.yml
or Dockerfile
.
When creating DB docker container, docker will create predefined tables in mysql/db
folder.
That help your team to control versions of database.
The sample table definition has already been created with the name create_user_table.sql
.
If you need to share local DB changes with other developers, you can use mysqldump
to create a backup and share it with them.
To create a `dump.sql', run the following command:
docker-compose exec database mysqldump -u root -p fastapi_app > mysql/db/dump.sql
Then, to reinitialize the DB, remove the named volumes declared in the "volumes" section of the Compose file.
https://docs.docker.com/engine/reference/commandline/compose_down/
docker-compose down -v
Then, run docker-compose up
to launch the development environment.
And confirm that your DB changes are reflected.