简体   繁体   English

Google Cloud Run 不加载 .env 文件

[英]Google Cloud Run does not load .env file

I spend the last couple of days trying to find what I have done wrong but I am still not able to figure out because I am able to run the app locally using flask run and also using Docker using docker-compose up --build .我花了最后几天试图找出我做错了什么,但我仍然无法弄清楚,因为我能够使用flask run在本地运行应用程序,并且还使用 Docker 使用docker-compose up --build Source code is here 源代码在这里

My issue is my Cloud Run deployment is successful but Service Unavailable when I am clicking on the URL .我的问题是我的 Cloud Run 部署成功,但单击URL时服务不可用。 I checked the logs and seems my environment variables are not correctly loaded:我检查了日志,似乎我的环境变量没有正确加载:

line 7, in <module> from web_messaging.blueprints.user import user File 
"/web_messaging/web_messaging/blueprints/user/__init__.py", line 1, in <module> from 
web_messaging.blueprints.user.views import user File 
"/web_messaging/web_messaging/blueprints/user/views.py", line 3, in <module> from 
web_messaging.extensions import mongo, login_manager, c, bc File 
"/web_messaging/web_messaging/extensions.py", line 18, in <module> twilio_client = Client(TWILIO_SID,
TWILIO_TOKEN) File "/usr/local/lib/python3.9/site-packages/twilio/rest/__init__.py", line 54, in __init__
raise TwilioException("Credentials are required to create a TwilioClient") 
twilio.base.exceptions.TwilioException: Credentials are required to create a TwilioClient    

I have a config/.env file and a config/settings.py .我有一个config/.env文件和一个config/settings.py I am loading the env variables from .env using load_dotenv() on my config/settings.py .我正在使用config/settings.py上的load_dotenv().env加载环境变量。 I decided to add some print and try/expect statements in my config/settings.py to see the value of variables.我决定在我的config/settings.py中添加一些 print 和 try/expect 语句来查看变量的值。

settings.py设置.py

import os
from dotenv import load_dotenv
BASEDIR = os.path.abspath(os.path.dirname(__file__))

try:
    load_dotenv(os.path.join(BASEDIR, '.env'))
    print("OK")
    print(BASEDIR)
except Exception as e:
    print(str(e))

# Mongo Database
MONGO_URI = os.getenv('MONGO_URI')
TWILIO_SID = os.getenv('TWILIO_SID')
TWILIO_TOKEN = os.getenv('TWILIO_TOKEN')
print(MONGO_URI)
print(TWILIO_SID)

When I am running with flask run, docker-compose or on cloud-run:当我使用 flask 运行时,docker-compose 或云运行:

  • The BASEDIR value is /web_messaging/config BASEDIR值为/web_messaging/config
  • There is no exceptions during the load_dotenv() call load_dotenv()调用期间没有异常

However, there is one major difference, it is the value of my env variables such as MONGO_URI , TWILIO_SID .但是,有一个主要区别,它是我的环境变量的值,例如MONGO_URITWILIO_SID Those variables have correct values when using flask run and docker-compose but not on the Cloud Run logs.这些变量在使用flask rundocker-compose时具有正确的值,但在 Cloud Run 日志中却没有。 On Cloud Run, those variables are equal to None .在 Cloud Run 上,这些变量等于None

When I don't use a .env and directly put the value of my variables inside /config/settings.py , there is no issues and my Cloud Run link is working correctly.当我不使用.env并直接将变量的值放入/config/settings.py时,没有问题并且我的 Cloud Run 链接正常工作。 I also tried to moved .env outside of the config file and in few other locations but I still got the same issue.我还尝试将.env移到配置文件之外和其他几个位置,但我仍然遇到同样的问题。

.
├── requirements.txt
├── Dockerfile
├── Docker-compose.yml
├── config    
│   ├── .env                           
│   ├── settings.py            
│   ├── gunicorn.py 
│   └── __init__.py 
├── web_messaging                   
│   ├── app.py      # where I am calling create_app() - factory pattern         
│   ├── blueprints              
│   ├── static                     
│   └── ...                 
└── ...

Dockerfile Dockerfile

FROM python:3.9-slim

ENV INSTALL_PATH /web_messaging
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

CMD gunicorn -b 0.0.0.0:8080 --access-logfile - "web_messaging.app:create_app()"

docker-compose.yml docker-compose.yml

version: '2'

services:
  website:
    build: .
    command: >
      gunicorn -b 0.0.0.0:8080
        --access-logfile -
        --reload
        "web_messaging.app:create_app()"
    environment:
      PYTHONUNBUFFERED: 'true'
    volumes:
      - '.:/web_messaging'
    ports:
      - '8080:8080'

config/.env配置/.env

COMPOSE_PROJECT_NAME=web_messaging
FLASK_SECRET=xxx
MONGO_URI=mongodb+srv://xxx
MONGO_DB=xxx
TWILIO_SID=xxx
TWILIO_TOKEN=xxx 

config/settings.py配置/设置.py

import os
from dotenv import load_dotenv
BASEDIR = os.path.abspath(os.path.dirname(__file__))


load_dotenv(os.path.join(BASEDIR, '.env'))

DEBUG = True
PYTHONDONTWRITEBYTECODE=1
#SERVER_NAME = '127.0.0.1:5000'


# Mongo Database
MONGO_DBNAME = os.getenv('MONGO_DB')
MONGO_URI = os.getenv('MONGO_URI')


# Twilio API 
FLASK_SECRET = os.getenv('FLASK_SECRET')
TWILIO_SID = os.getenv('TWILIO_SID')
TWILIO_TOKEN = os.getenv('TWILIO_TOKEN')
                                    

config/gunicorn.py配置/gunicorn.py

bind = '0.0.0.0:8080'
accesslog = '-'
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" in %(D)sµs'
                              

Fixed, I found exactly what went wrong but I do not know why.已修复,我确切地发现出了什么问题,但我不知道为什么。

  • It worked when I build my own image before to push the image on GCP container registry following those steeps:当我之前构建自己的图像以将图像推送到 GCP 容器注册表中时,它起作用了:

     docker-compose up --build docker tag 52e6159b6b13 gcr.io/mousset005/zoro gcloud auth configure-docker docker push gcr.io/mousset005/zoro

However, what I was doing is building my Image using GCP API (which is what they recommend in the Cloud Run Python quickstart) using that command:但是,我正在做的是使用 GCP API(这是他们在 Cloud Run Python 快速入门中推荐的)使用该命令构建我的图像:

gcloud run deploy --image gcr.io/mousset005/zoro --platform managed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM