This repository contains a minimal Flask app that can run queries against an IBM Db2 instance using the ibm_db package. It includes a Dockerfile for the Flask app and a docker-compose setup that can start a local Db2 container for development.
-
Copy
.env.exampleto.env(default values work with the included Db2 container) -
Build and run with Docker Compose:
docker-compose up --build
This will start:
- Flask app on port 5000
- Db2 container on port 50000 (exposed as 50000 in compose)
-
Wait for Db2 to initialize (can take a few minutes on first run)
-
Test the API:
Health check:
curl http://localhost:5000/ping
Query the test table:
curl -X POST http://localhost:5000/query ` -H "Content-Type: application/json" ` -d '{"sql": "SELECT * FROM USERS"}'
The repo includes init/01-init.sql. The compose file mounts the ./init directory into the Db2 container at /docker-entrypoint-initdb.d so the SQL file is available to the container. There are two ways to ensure the SQL is applied:
If the Db2 data volume is empty, the container's entrypoint will execute scripts in /docker-entrypoint-initdb.d during first initialization.
Steps:
-
If you've started the stack before and want a fresh initialization, remove the existing volume first (this deletes existing DB data):
docker-compose down # list volumes to confirm the name if different docker volume ls docker volume rm ibm-db-docker-demo_db2data
-
Start the stack and watch logs:
docker-compose up --build docker-compose logs -f db2
The init script will run during first-boot. Wait until logs show the DB is ready and healthcheck passes.
If the database already exists or you need to re-run the script, exec into the running Db2 container and run the file with the db2 CLI.
-
Start containers if not running:
docker-compose up -d
-
Get the container name (example name may vary):
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Ports}}"
-
Exec into the container and run the SQL as the Db2 instance user:
docker exec -it <db2-container-name> bash # inside the container (bash prompt) su - db2inst1 . /database/config/db2inst1/sqllib/db2profile db2 connect to users user db2inst1 using password db2 -tvf /docker-entrypoint-initdb.d/01-init.sql db2 connect reset exit exit
Replace <db2-container-name> with the container name shown by docker ps and change users, db2inst1, and password where appropriate if you modified .env.
Use the Flask API to query the USERS table:
curl -X POST http://localhost:5000/query -H "Content-Type: application/json" -d '{"sql":"SELECT * FROM USERS"}'Or run a db2 query inside the container (see manual exec above).
- Db2 first-time initialization can take several minutes; check logs with
docker-compose logs -f db2. - If init didn't run automatically, it's usually because the DB volume already existed; remove the volume to force first-boot behavior.
- For production, do not store credentials in
.env; use a secrets manager and stronger passwords.
This project ships .env.example. Copy it to .env and update values for your environment. The compose file is configured to load environment variables from .env for both the web and db2 services.
copy .env.example .env
# edit .env with your real values
notepad .env
docker-compose up --build.env is ignored by .gitignore that was added to this repo, so your local secrets won't be committed.