Strange MySQL container connectivity issue from Flask

I created a simple Flask-based API to the WorldDB on MySQL. They are two separate containers on the same network.

When I invoke the RESTful-API to list the citiies, using curl or SOAPUI, I get a 500 ERROR.

"
UnboundLocalError: local variable ‘cursor’ referenced before assignment
cityService | 172.18.0.1 - - [28/Nov/2022 23:37:36] “GET /listCities HTTP/1.1” 500 -
cityService | [2022-11-28 23:37:38,821] ERROR in app: Exception on /listCities [GET]
cityService | Traceback (most recent call last):
cityService | File “/usr/local/lib/python3.6/site-packages/flask/app.py”, line 2073, in wsgi_app
cityService | response = self.full_dispatch_request()
cityService | File “/usr/local/lib/python3.6/site-packages/flask/app.py”, line 1518, in full_dispatch_request
cityService | rv = self.handle_user_exception(e)
cityService | File “/usr/local/lib/python3.6/site-packages/flask/app.py”, line 1516, in full_dispatch_request
cityService | rv = self.dispatch_request()
cityService | File “/usr/local/lib/python3.6/site-packages/flask/app.py”, line 1502, in dispatch_request
cityService | return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
cityService | File “CityController.py”, line 55, in cities
cityService | cursor.close()
cityService | UnboundLocalError: local variable ‘cursor’ referenced before assignment
"

However, when I log into MySQL instance using MySQL-Workbench once using the ROOT-ID/PASSWORD,I can see the worldDB tables. Now I invoke the API again and I get the list of Cities just fine in the response.

" UnboundLocalError: local variable ‘cursor’ referenced before assignment
cityService | 172.18.0.1 - - [28/Nov/2022 23:37:39] “GET /listCities HTTP/1.1” 500 -
cityService | 172.18.0.1 - - [28/Nov/2022 23:41:12] “GET /listCities HTTP/1.1” 200 -"

Both containers are on my Windows-Docker-Desktop.

Any ideas please?

Thanks
Kind regards
-Ram

My docker-compose file is below:

–version: ‘3.8’

services:
db:
image: mysql
container_name: worldDB
networks:
- default
cap_add:
- SYS_NICE
restart: always
environment:
- MYSQL_DATABASE=world
- MYSQL_DATABASE_USER=root
- MYSQL_ROOT_PASSWORD=password
ports:
- ‘13306:3306’
command: --init-file /data/application/world.sql
volumes:
- db:/var/lib/mysql
- ./database/world.sql:/data/application/world.sql

city-service:
build: ./services/cityService
container_name: cityService
volumes:
- ./services/cityService:/usr/src/app
networks:
- default
cap_add:
- SYS_PTRACE
environment:
- MYSQL_DATABASE_USER=root
- MYSQL_DATABASE_PASSWORD=password
- MYSQL_DATABASE=world
- MYSQL_DATABASE_HOST=worldDB
ports:
- ‘8090:5000’

volumes:
db:
driver: local

networks:
default:
name: mynetwork
external: true

Please, format your post as it is mentioned in the following topic so we can understand your source code

Always check your post after sending it to make sure it looks like what you wanted to share (with the indentation and special characters)

It also helps to get more response to your question.