Docker-compose Flask, MYSQL PAIN - PullRequest
       5

Docker-compose Flask, MYSQL PAIN

0 голосов
/ 23 февраля 2019

Я новичок в Docker, и у меня есть простое приложение фляги, которое подсчитывает, сколько раз посетитель просматривал html-страницу.

Я пытаюсь сделать docker-compose с mysql внутри контейнера, но когда я открываю браузерон получает ошибку 255. Без докера, если я храню данные MYSQ на локальном хосте, он работает как положено.Что не так с моим docker-compose?

builtins.KeyError
KeyError: 255 

init.py:

from flask import Flask
from config import Config

from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object(Config)

db = SQLAlchemy(app)

from app import views

models.py:

from app import db

class Counter_v(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    count = db.Column(db.Integer)

    def __init__(self,count):
        self.count=count


    def __repr__(self):
        return "<Count {}>".format(self.count)

views.py:

from app import app, db
from app.models import Counter_v

@app.before_request
def before_request():
    db.create_all()

@app.route('/')
def index():
    v = Counter_v.query.first()
    print(v)
    if not v:
        v = Counter_v(count=0)
        v.count +=1
        db.session.add(v)
        db.session.commit()
    v.count +=1
    db.session.commit()
    return "Counter is " + str(v.count)

config.py:

import os

class Config(object):
    SECRET_KEY = 'you-will-never-guess'
    DEBUG = True
    DB_USERNAME = 'root'
    DB_PASSWORD = 'rootpass'
    DATABASE_NAME = 'counter'
    DB_HOST = 'db'
    DB_URI = "mysql+pymysql://%s:%s@%s/%s" % (DB_USERNAME, DB_PASSWORD, DB_HOST, DATABASE_NAME)

    MYSQL_ROOT_PASSWORD = 'rootpass'
    MYSQL_USER = 'root'
    MYSQL_ALLOW_EMPTY_PASSWORD = True

    SQLALCHEMY_DATABASE_URI = DB_URI
    SQLALCHEMY_TRACK_MODIFICATIONS = True

app_main.py:

from app import app

мои требования.py

Flask==0.12
PyMysql==0.7.4
Flask-SQLAlchemy==2.1

.env:

COMPOSE_PROJECT_NAME=web_test_3
PYTHONBUFFERED=true
FLASK_APP=app_main.py
FLASK_DEBUG=1

Dockerfile:

FROM python:3.7-slim
RUN apt-get update && apt-get install -y default-libmysqlclient-dev

RUN mkdir /app
WORKDIR /app

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

COPY . .


CMD flask run --host=0.0.0.0 --port=5000

Docker-compose.yml:

version: '3'

services:
  web:
    build: .
    env_file:
      - '.env'
    ports:
      - '5000'
    volumes:
      - '.:/app'

  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: rootpass


  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

Полный возврат:

builtins.KeyError
KeyError: 255

Traceback (most recent call last)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1610, in full_dispatch_request
rv = self.preprocess_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1831, in preprocess_request
rv = func()
File "/app/app/views.py", line 6, in before_request
db.create_all()
File "/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 972, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "/usr/local/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 964, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/sql/schema.py", line 4200, in create_all
ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2010, in _run_visitor
with self._optional_conn_ctx_manager(connection) as conn:
File "/usr/local/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2002, in _optional_conn_ctx_manager
with self.contextual_connect() as conn:
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2192, in contextual_connect
self._wrap_pool_connect(self.pool.connect, None),
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/base.py", line 2228, in _wrap_pool_connect
return fn()
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool.py", line 425, in connect
return _ConnectionFairy._checkout(self)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool.py", line 822, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool.py", line 554, in checkout
rec = pool._do_get()
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool.py", line 1250, in _do_get
self._dec_overflow()
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 67, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/util/compat.py", line 277, in reraise
raise value
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool.py", line 1247, in _do_get
return self._create_connection()
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool.py", line 370, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool.py", line 499, in __init__
self.__connect(first_connect_check=True)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/pool.py", line 701, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python3.7/site-packages/sqlalchemy/engine/default.py", line 437, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python3.7/site-packages/pymysql/__init__.py", line 88, in Connect
return Connection(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 688, in __init__
self.connect()
File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 905, in connect
self._get_server_information()
File "/usr/local/lib/python3.7/site-packages/pymysql/connections.py", line 1231, in _get_server_information
self.server_charset = charset_by_id(lang).name
File "/usr/local/lib/python3.7/site-packages/pymysql/charset.py", line 34, in by_id
return self._by_id[id]
KeyError: 255
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...