0

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.

1 Answer 1

1

You are trying to refer the name mysqldb as python variable which ofcourse would not be found because you have not defined it anywhere in your code. You will be able to connect to DB using mysqldb as hostname because docker-compose sets up the network between the services. So change the line host= mysqldb to host='mysqldb'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.