- Python 3.10.12 installed
- Flask 3.0.3 installed (already added in requirements.txt)
- Clone the Repository:
git clone https://github.com/rakeshbasnet/docker-compose-example.git cd docker-compose-example
- Copy .env.example File to Create .env File:
cp .env.example .env
- Set Up Virtual Environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
- Install Dependencies:
pip install -r requirements.txt
- Run the Development Server:
python main.py
- Access the Application:
Open a browser and go to:
http://127.0.0.1:5000
- Docker installed
- Docker Compose installed
- Build Application Docker Image from Dockerfile:
docker build -t image-name:tag-name . //replace your flask-app-image-name and tag name
- Create Docker Network:
docker network ls // to list all the networks in docker docker network create flask-app-network //create new network
- Run Docker Compose:
docker compose up -d // -d: runs the containers in detached mode
- To Stop the running Conatainers:
docker compose down
- Before Accessing the Application, Access pgAdmin in
http://localhost:8080
. - Login using the credential passed during container creation.
- On right top, right click
Servers
, ClickCreate->Server Group
and Save. - Right Click on recently created Server, Click
Register->Server-> Connection Tab
, Add Postgres Credentials and Connect. If Connection is successful, the docker network is working. Note: For host section in Connection tab, add the db container name and other credentials passed via env files. - Look for mydb inside Server and add
users table
with some data. - Now, Access the Flask Application in
http://localhost:5000
- To make sure it is correctly connected with db:
Check URL:
http://localhost:5000/users
, a list of users will retun. - To check whether, it is correctly connected with redis cache server,
Check URL:
http://localhost:5000/cache
, it will return a message:Cache Hit: b'Hello from Redis Cache!'