stripe.api_key = os.environ['stripe_key'] error in Docker container

84 Views Asked by At

I am having trouble deploying a flask app to aws lightsail. I am following this tutorial. I am on step 2b. I added a few of my own files, such as index.html, /templates, /static.

My directory structure is

myproject
|
|--static
|--templates   
|   |--my_templates
|   |--index.html         
|   
|--.env 
|--.gitignore
|--app.py
|--containers.json
|--Dockerfile
|--public-endpoints.json
|--requirements.txt

I run the command docker run -p 5000:5000 flask-container and receive

  File "/app/./app.py", line 7, in <module>
    stripe.api_key = os.environ['stripe_key']
                     ~~~~~~~~~~^^^^^^^^^^^^^^
  File "<frozen os>", line 685, in __getitem__
KeyError: 'stripe_key'

My .env file has

...
stripe_key='sk_*****'

The top of my app.py is

from flask import Flask, render_template, request, redirect
from jinja2 import Environment, FileSystemLoader
import os, mysql.connector, stripe, json, jsonify
from dotenv import load_dotenv

load_dotenv()
stripe.api_key = os.environ['stripe_key']

app = Flask(__name__)

Interestingly, I was trying to deploy this app to google cloud a while ago, and received the same error. The question is still unanswered here.

When I run the app locally with python app.py, it works. I am able to navigate, and can even access the stripe hosted page, so the stripe key is there locally.

I read that you should be able to access environment variables in a docker container in python using os.environ['my_key'], and that is what I have in app.py. I tried adding the .env file in the command line by using

docker run --env-file .env -p 5000:5000 flask-container

and this did not produce any command line errors, however, when I went to localhost:5000, the browser showed local host didn't send any data and below it ERR_EMPTY_RESPONSE.

I tried to research how to add environment variables to lightsail and found this. I updated my containers.json as

{
    "flask": {
        "image": ":flask-service.flask-container.5",
        "ports": {
            "5000": "HTTP"
        },
        "Environment": ["stripe_key"]
    }
}

After running docker run -p 5000:5000 flask-container I received the same stripe_key error on the command line above. I know I'd have to somehow insert the value.

I think this ultimately comes down to how to insert environment variables in production. The error first showed up in google cloud, and now it is here in Docker / lightsail. Any help is appreciated.

Dockerfile

FROM python:3.12-alpine
EXPOSE 5000/tcp
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY app.py .
CMD [ "python", "./app.py" ]

public-endpoints.json

{
    "containerName": "flask", 
    "containerPort": 5000
}

requirements.txt

Flask==3.0.0; python_version > '3.6'
Flask==2.3.3; python_version < '3.7'
Werkzeug==3.0.1; python_version > '3.6'
Werkzeug==2.3.7; python_version < '3.7'
gunicorn==20.1.0
python-dotenv==1.0.1
stripe==7.13.0
jsonify==0.5
1

There are 1 best solutions below

0
Krabs On

I ended up fixing my Dockerfile, and now it reads

...
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY app.py .
COPY /static ./static
COPY /templates ./templates
COPY .env .
CMD [ "python", "./app.py" ]

The local host sent no data was an issue with my ports, not with my dockerfile; the solution to which can be found here