Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

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

$
0
0

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 commanddocker 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, redirectfrom jinja2 import Environment, FileSystemLoaderimport os, mysql.connector, stripe, json, jsonifyfrom dotenv import load_dotenvload_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-alpineEXPOSE 5000/tcpWORKDIR /appCOPY requirements.txt .RUN pip install -r requirements.txtCOPY 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.0python-dotenv==1.0.1stripe==7.13.0jsonify==0.5

Viewing all articles
Browse latest Browse all 13891

Trending Articles