подключение с использованием Flask-SQLAlchemy и mysql на докере - PullRequest
0 голосов
/ 31 мая 2019

У меня есть приложение, которое работает нормально, когда я использую среду рабочего стола. Однако, когда я помещаю в среду докера следующую ошибку:

BrokenFilesystemWarning)
172.19.0.1 - - [31/May/2019 12:43:18] "GET /data/ HTTP/1.1" 500 -
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/dist-packages/flask_socketio/__init__.py", line 43, in __call__
start_response)
File "/usr/local/lib/python3.6/dist-packages/engineio/middleware.py", line 67, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 35, in reraise
raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
 File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.6/dist-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 35, in reraise
 raise value
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.6/dist-packages/flask_cors/decorator.py", line 128, in wrapped_function
resp = make_response(f(*args, **kwargs))
File "/tmp/app/views/main.py", line 63, in data
bd_data = get_db_data()
File "/tmp/app/views/main.py", line 81, in get_db_data
conn = MySQLdb.connect(user="root", passwd="root", db="test")
File "/usr/local/lib/python3.6/dist-packages/MySQLdb/__init__.py", line 85, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

Вот файл для создания докера:

version: '3'
services:
    db:
        image: mysql
        container_name: container-db
        environment:
            - MYSQL_ROOT_PASSWORD=root
            - MYSQL_DATABASE=test
        ports:
            - "3306:3306"
        volumes:
            - ./mysql:/var/lib/mysql
        networks:
            - app-tier
        restart: always 
    service:
        image: service-image
        container_name: container-service
        ports:
            - "8080:8080"
        entrypoint: ["./wait-for-it.sh","db:3306","--timeout=0","--strict","--","python","run.py"]
        depends_on:
            - db
        networks:
            - app-tier
        restart: always
        environment:
            DB_HOST: db:3306
            YOLO_VERSION: v2
    app:
        image: view-image
        container_name: container-view-app
        ports:
            - "80:80"
        depends_on:
            - service
        networks:
            - app-tier
        restart: always
networks:
  app-tier:
    driver: bridge

и переменные, которые я передаю SQLAlchemy:

DB_USERNAME = 'root'
DB_PASSWORD = 'root'
DB_NAME = 'test'

DB_HOST = str(os.environ.get('DB_HOST'))
YOLO_VERSION = str(os.environ.get('YOLO_VERSION'))


SQLALCHEMY_DATABASE_URI = 'mysql://' + DB_USERNAME + ':' + DB_PASSWORD + '@'+DB_HOST+'/' + DB_NAME

Я подозревал, что между контейнерами может быть какая-то проблема с подключением, но когда я получаю доступ к служебному контейнеру и выполняю ручное соединение с помощью mysql.connect, он работает нормально.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...