我有一个烧瓶应用程序,可以成功初始化并连接到Postgresql数据库.但是,当我尝试将此应用程序停靠时,我收到以下错误消息. “sqlALCHEMY_DATABASE_URI”是正确的,我可以连接到它,所以我无法弄清楚我哪里出错了.
docker-compose日志
app_1 | File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/url.py",line 60,in __init__
app_1 | self.port = int(port)
app_1 | ValueError: invalid literal for int() with base 10: 'None'
Postgres数据库在Docker容器中成功连接
postgres_1 | LOG: database system is ready to accept connections
config.py
from os import environ
import os
RDS_USERNAME = environ.get('RDS_USERNAME')
RDS_PASSWORD = environ.get('RDS_PASSWORD')
RDS_HOSTNAME = environ.get('RDS_HOSTNAME')
RDS_PORT = environ.get('RDS_PORT')
RDS_DB_NAME = environ.get('RDS_DB_NAME')
sqlALCHEMY_DATABASE_URI = "postgresql+psycopg2://{username}:{password}@{hostname}:{port}/{dbname}"\
.format(username = RDS_USERNAME,password = RDS_PASSWORD,\
hostname = RDS_HOSTNAME,port = RDS_PORT,dbname = RDS_DB_NAME)
flask_app.py(入口点)
def create_app():
app = Flask(__name__,static_folder="./static",template_folder="./static")
app.config.from_pyfile('./app/config.py',silent=True)
register_blueprint(app)
register_extension(app)
with app.app_context():
print(db) -> This prints the correct path for sqlALCHEMY_DATABASE_URI
db.create_all()
db.session.commit()
return app
def register_blueprint(app):
app.register_blueprint(view_blueprint)
app.register_blueprint(race_blueprint)
def register_extension(app):
db.init_app(app)
migrate.init_app(app)
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0',port=8080,debug=True)
Dockerfile
FROM ubuntu
RUN apt-get update && apt-get -y upgrade
RUN apt-get install -y python-pip && pip install --upgrade pip
RUN mkdir /home/ubuntu
WORKDIR /home/ubuntu/celery-scheduler
ADD requirements.txt /home/ubuntu/celery-scheduler/
RUN pip install -r requirements.txt
COPY . /home/ubuntu/celery-scheduler
EXPOSE 5000
CMD ["python","flask_app.py","--host","0.0.0.0"]
泊坞窗,compose.yml
version: '2'
services:
app:
restart: always
build:
context: .
dockerfile: Dockerfile
volumes:
- .:/app
depends_on:
- postgres
postgres:
restart: always
image: postgres:9.6
environment:
- POSTGRES_USER=${RDS_USERNAME}
- POSTGRES_PASSWORD=${RDS_PASSWORD}
- POSTGRES_HOSTNAME=${RDS_HOSTNAME}
- POSTGRES_DB=${RDS_DB_NAME}
ports:
- "5432:5432"
最佳答案
您需要在Dockerfile中使用ENV键值设置环境变量RDS_USERNAME,RDS_PASSWORD,RDS_HOSTNAME,RDS_PORT和RDS_DB_NAME,例如
ENV RDS_PORT 5432