- python 3.9 or above
- Create a venv inside the project
python3 -m venv venv/
- Source the venv activation script
source venv/bin/activate
- Install all python dependencies
pip install -r requirements.txt
- Run the app using uvicorn
python3 -m uvicorn src.main:app --host 127.0.0.1 --port 8000
- In the browser go to
127.0.0.1:8000/docs
to view the Swagger docs
This is the recommended method as the container, network and volume configurations are provided once. And the application can be started and stopped by just single commands.
- Perform
docker-compose up
. Wait for the application to startup. You can check the logs to verify this. - Go to
127.0.0.1:8000/docs
to view the Swagger docs. - If you make any changes to the code, you need to perform
docker-compose down
and thendocker-compose up --build
to build the docker image with new changes and to up the application. - If the containerized application is not required anymore do
docker-compose down
to bring down the container.
Note:
docker-compose down
will not delete the volume that was attached to themysqldb
container. This is not a bug, but a well thought feature. By not deleting the volume, the volume will be reused when the application is started again. And you won't lose any data stored in the database. If you want to delete the volume, you can do it by runningdocker volume rm <volume_name>
The docker image, network, volume and containers have to created manually by separate commands in this method.
docker pull mysql:latest
docker network create --driver bridge app-network
docker volume create db-data
docker run -dit --name mysqldb --network app-network --volume db-data:/var/lib/mysql --publish 3306:3306 --restart always --platform linux/x86_64 --env MYSQL_DATABASE=dev --env MYSQL_USER=admin --env MYSQL_ROOT_PASSWORD=<provide-root-password-here> --env MYSQL_PASSWORD=<provide-password-here> mysql:latest
docker build -t=pythonwebbase .
docker run -dit --name app --network app-network --publish 8000:8000 --env MYSQL_DATABASE=dev --env MYSQL_USER=admin --env MYSQL_PASSWORD=<provider-password-here> pythonwebbase:latest
- Now go to
127.0.0.1:8000/docs
to view the Swagger docs.
- Perform
docker stop app && docker rm app
to stop and destroy theapp
container. Similarly, for themysqldb
container, dodocker stop mysqldb && docker rm mysqldb
. - To remove the network
docker network rm app-network
. - Finally remove the created volume by doing
docker volume rm db_data
.