My Docker-compose file looks like this, with the web service being the Flask Server, and the mysqldb service referencing a mysql server running in a separate container:
version: '3'
services:
mysqldb:
image: mysql:8.0.17
environment:
MYSQL_ROOT_PASSWORD: example
MYSQL_DATABASE: test
MYSQL_USER: testing
MYSQL_PASSWORD: testing
ports:
- "3308:3306"
web:
restart: always
build:
context: .
dockerfile: Dockerfile
expose:
- "5000"
links:
- mysqldb
ports:
- "5000:5000"
According to documentation, I should be able to write mysqldb in my flask-service.py file as a variable like this:
...
import mysql.connector
...
print(mysqldb)
mysqlcnx = mysql.connector.connect(user='testing', password='testing', host= mysqldb,
port = '3306',database='test' )
...
But instead upon running docker-compose up --build -d, my logs for the web service show
NameError: name `mysqldb` is not defined
I looked through several stack overflow questions and docker examples, but I can't find a solid reason that the reference does not work. I also tried importing os and calling it as if it was an environment variable, to no avail.
Thanks.