title: Google Cloud Run does not load .env file
body: <p>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 <code>flask run</code> and also using Docker using <code>docker-compose up --build</code>. <a href="https://github.com/Pierre-Alexandre35/messaging-service-mousset" rel="noreferrer">Source code is here</a></p>
  <p>My issue is my Cloud Run deployment is successful but Service Unavailable when I am clicking on the <a href="https://zero-leay6xemga-ey.a.run.app/" rel="noreferrer">URL</a>. I checked the logs and seems my environment variables are not correctly loaded:</p>
  <pre><code>line 7, in &lt;module&gt; from web_messaging.blueprints.user import user File 
  &quot;/web_messaging/web_messaging/blueprints/user/__init__.py&quot;, line 1, in &lt;module&gt; from 
  web_messaging.blueprints.user.views import user File 
  &quot;/web_messaging/web_messaging/blueprints/user/views.py&quot;, line 3, in &lt;module&gt; from 
  web_messaging.extensions import mongo, login_manager, c, bc File 
  &quot;/web_messaging/web_messaging/extensions.py&quot;, line 18, in &lt;module&gt; twilio_client = Client(TWILIO_SID,
  TWILIO_TOKEN) File &quot;/usr/local/lib/python3.9/site-packages/twilio/rest/__init__.py&quot;, line 54, in __init__
  raise TwilioException(&quot;Credentials are required to create a TwilioClient&quot;) 
  twilio.base.exceptions.TwilioException: Credentials are required to create a TwilioClient    
  </code></pre>
  <p>I have a <code>config/.env</code> file and a <code>config/settings.py</code>. I am loading the env variables from <code>.env</code> using  <code>load_dotenv()</code> on my <code>config/settings.py</code>. I decided to add some print and try/expect statements in my <code>config/settings.py</code> to see the value of variables.</p>
  <p><strong>settings.py</strong></p>
  <pre><code>import os
  from dotenv import load_dotenv
  BASEDIR = os.path.abspath(os.path.dirname(__file__))
  
  try:
      load_dotenv(os.path.join(BASEDIR, '.env'))
      print(&quot;OK&quot;)
      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)
  </code></pre>
  <p>When I am running with flask run, docker-compose or on cloud-run:</p>
  <ul>
  <li>The <code>BASEDIR</code> value is <code>/web_messaging/config</code></li>
  <li>There is no exceptions during the <code>load_dotenv()</code> call</li>
  </ul>
  <p>However, there is one major difference, it is the value of my env variables such as <code>MONGO_URI</code>, <code>TWILIO_SID</code>. Those variables have correct values when using <code>flask run</code> and <code>docker-compose</code> but not on the Cloud Run logs. On Cloud Run, those variables are equal to <code>None</code>.</p>
  <p>When I don't use a <code>.env</code> and directly put the value of my variables inside <code>/config/settings.py</code>, there is no issues and my Cloud Run link is working correctly. I also tried to moved <code>.env</code> outside of the config file and in few other locations but I still got the same issue.</p>
  <pre><code>.
  ├── 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                     
  │   └── ...                 
  └── ...
  </code></pre>
  <p><strong>Dockerfile</strong></p>
  <pre><code>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 - &quot;web_messaging.app:create_app()&quot;
  </code></pre>
  <p><strong>docker-compose.yml</strong></p>
  <pre><code>version: '2'
  
  services:
    website:
      build: .
      command: &gt;
        gunicorn -b 0.0.0.0:8080
          --access-logfile -
          --reload
          &quot;web_messaging.app:create_app()&quot;
      environment:
        PYTHONUNBUFFERED: 'true'
      volumes:
        - '.:/web_messaging'
      ports:
        - '8080:8080'
  </code></pre>
  <p><strong>config/.env</strong></p>
  <pre><code>COMPOSE_PROJECT_NAME=web_messaging
  FLASK_SECRET=xxx
  MONGO_URI=mongodb+srv://xxx
  MONGO_DB=xxx
  TWILIO_SID=xxx
  TWILIO_TOKEN=xxx 
  </code></pre>
  <p><strong>config/settings.py</strong></p>
  <pre><code>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')
                                      
  </code></pre>
  <p><strong>config/gunicorn.py</strong></p>
  <pre><code>bind = '0.0.0.0:8080'
  accesslog = '-'
  access_log_format = '%(h)s %(l)s %(u)s %(t)s &quot;%(r)s&quot; %(s)s %(b)s &quot;%(f)s&quot; &quot;%(a)s&quot; in %(D)sµs'
                                
  </code></pre>