- 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-network-example.git cd docker-network-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
- 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 PostgreSQL DB Container:
docker run --rm -d \ --name db \ --env-file .env \ -p 5433:5432 \ --network flask-app-network \ postgres:13
- Run pgAdmin Container:
docker run --rm -d \ --name pgadmin \ -e PGADMIN_DEFAULT_EMAIL=admin@admin.com \ -e PGADMIN_DEFAULT_PASSWORD=admin \ -p 8080:80 \ --network flask-app-network \ dpage/pgadmin4:latest
- Create Redis Container:
docker run --rm -d \ --name redis \ --network flask-app-network \ redis:latest
- Create Flask App Container:
docker run --rm -d \ --name flask-app \ --env-file .env \ -p 5000:5000 \ --network flask-app-network \ image-name:tag-name //replace your flask-app-image-name and tag name
- 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 Groupand 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 tablewith 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!'