Skip to content

Instantly share code, notes, and snippets.

@Vishal1297
Created August 16, 2022 01:31
Show Gist options
  • Save Vishal1297/20fba6b56bc03858a8cae5d8d9aaa624 to your computer and use it in GitHub Desktop.
Save Vishal1297/20fba6b56bc03858a8cae5d8d9aaa624 to your computer and use it in GitHub Desktop.
Python flask dockerfile
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')
version: "3.9"
services:
web:
build: .
ports:
- "8000:5000"
environment:
FLASK_DEBUG: development
redis:
image: "redis:alpine"
# 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"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment