Created
August 16, 2022 01:31
-
-
Save Vishal1297/20fba6b56bc03858a8cae5d8d9aaa624 to your computer and use it in GitHub Desktop.
Python flask dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from flask import Flask | |
import redis | |
app = Flask(__name__) | |
cache = redis.Redis(host='redis', port=6379) | |
def get_hit_count(): | |
retries = 5 | |
while True: | |
try: | |
return cache.incr('hits') | |
except redis.exceptions.ConnectionError as exc: | |
if retries == 0: | |
raise exc | |
retries -= 1 | |
time.sleep(0.5) | |
@app.route('/') | |
def hello(): | |
count = get_hit_count() | |
return 'Hello World! I have been seen {} times.\n'.format(count) | |
if __name__ == "__main__": | |
app.run(debug=True, host='0.0.0.0') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.9" | |
services: | |
web: | |
build: . | |
ports: | |
- "8000:5000" | |
environment: | |
FLASK_DEBUG: development | |
redis: | |
image: "redis:alpine" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# syntax=docker/dockerfile:1 | |
FROM python:3.7-alpine | |
WORKDIR /code | |
RUN apk add --no-cache gcc musl-dev linux-headers | |
COPY ./requirements.txt requirements.txt | |
COPY ./app.py /code/app.py | |
RUN pip install -r requirements.txt | |
EXPOSE 5000 | |
CMD [ "python3", "-m" , "flask", "run", "--host=0.0.0.0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flask | |
redis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment