python flask env变量导入错误应用

时间:2019-02-28 00:40:18

标签: python docker flask environment-variables migrate

我有一个Dockercontainer,它的某些功能正常工作。我复制了它并进行了一些更改,并通过docker-compose都提供了它们。它工作正常,但突然我启动了一个新的容器,并收到以下错误:

ra-offerservice    | Usage: flask db migrate [OPTIONS]
ra-offerservice    | 
ra-offerservice    | Error: Could not import "code.app".

(docker-compose --verbose up)

compose.cli.verbose_proxy.proxy_callable: docker wait <- ('2d5df6e050d58a205a1d059493d2e3e99c06dee5ab70c45a0110db4dd18eb9c2')
compose.cli.verbose_proxy.proxy_callable: docker inspect_container <- ('2d5df6e050d58a205a1d059493d2e3e99c06dee5ab70c45a0110db4dd18eb9c2')
urllib3.connectionpool._make_request: http://localhost:None "POST /v1.25/containers/2d5df6e050d58a205a1d059493d2e3e99c06dee5ab70c45a0110db4dd18eb9c2/wait HTTP/1.1" 200 30
compose.cli.verbose_proxy.proxy_callable: docker wait -> {'Error': None, 'StatusCode': 2}
ra-offerservice exited with code 2
urllib3.connectionpool._make_request: http://localhost:None "GET /v1.25/containers/2d5df6e050d58a205a1d059493d2e3e99c06dee5ab70c45a0110db4dd18eb9c2/json HTTP/1.1" 200 None
compose.cli.verbose_proxy.proxy_callable: docker inspect_container -> {'AppArmorProfile': '',
 'Args': ['-c', '/usr/local/bin/boot.sh'],
 'Config': {'ArgsEscaped': True,
            'AttachStderr': False,
            'AttachStdin': False,
            'AttachStdout': False,
            'Cmd': None,
            'Domainname': '',
            'Entrypoint': ['/bin/sh', '-c', '/usr/local/bin/boot.sh'],
            'Env': ['SERVICE_NAME=ra-offerservice',

不知道为什么不再找到app.py。我没有进行任何更改,但仍未生效。

.env文件

FLASK_APP=app.py
DATABASE_URL='postgresql://offer:offer@offerdb:5432/offerdb'

boot.sh

#!/bin/sh -e
FLASK_APP=app.py

#FOR DATABASE init
#flask db init
#For DATABASE migration
flask db migrate -m "update"

# sync database to latest migration
flask db upgrade
exec gunicorn --log-level info --log-file=/gunicorn.log --workers 4 --name app -b 0.0.0.0:8080 --reload app:app &
envoy -c /etc/service-envoy.yaml --service-cluster service${SERVICE_NAME}

Dockerfile

FROM envoyproxy/envoy:latest
RUN apt-get update && apt-get -q install -y \
    curl \
    software-properties-common \
    python-software-properties

RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get update && apt-get -q install -y \
    python3.7 \
    python3-pip

RUN curl https://bootstrap.pypa.io/get-pip.py | python3.7
RUN python3.7 --version && pip3.7 --version
COPY requirements.txt .
RUN pip3.7 install --no-cache-dir -r ./requirements.txt
RUN pip3.7 install gunicorn
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
RUN mkdir /code
WORKDIR /code
COPY . /code

ADD ./boot.sh /usr/local/bin/boot.sh
RUN chmod u+x /usr/local/bin/boot.sh

ENTRYPOINT /usr/local/bin/boot.sh

app.py

from flask import Flask
from flask_script import Manager
from flask_restful import Api
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_jwt_extended import JWTManager
import config
from models.offer import OfferModel
from resources.offer import Offer, OfferList
from resources.offer_admin import ActivateOffer
from resources.test import TestKlasse
from db import db
from ma import ma

app = Flask(__name__)

############################
### LOAD CONFIGRUATION
############################
app.config.from_object(config.DevelopmentConfig)

db.init_app(app)
ma.init_app(app)

api = Api(app)
migrate = Migrate(app, db)
jwt = JWTManager(app)

############################
### ADD REST API ENDPOINTS
############################
#USER Endpoints
api.add_resource(Test, '/api/v1/test/<int:page>')

if __name__ == '__main__':
    app.run()

我绝对不知道发生了什么事。

2 个答案:

答案 0 :(得分:0)

确保退出 init .py 显示文件目录

答案 1 :(得分:0)

好,我有2个错误。

我在上面的ENV文件中的FLASK_APP=app.py之前缺少.env ->但我仍然收到错误消息

ra-offerservice    | Usage: flask db upgrade [OPTIONS]
ra-offerservice    | 
ra-offerservice    | Error: Could not import "code.app".

我在与__init__.py相同的文件夹中有一个app.py

ra-demandservice
|-- models
|   |-- __init__.py
|   `-- models.py
|-- resources
|   |-- resources.py
|   `-- __init__.py
|-- __init__.py
|-- app.py
|-- boot.sh
|-- .env
`-- Dockerfile