Count

# app.py
import os
import time
import redis
from flask import Flask

def get_ip():
    return os.getenv('MYREDIS_HOST', 'localhost')

app = Flask(__name__)
cache = redis.Redis(host=get_ip(), 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 from Docker! I have been seen {} times.\n'.format(count)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
        
# Create the dependent package list
# pip freeze > requirements.txt
flask
redis
        
# Dockerfile
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["python", "app.py"]
        
# compose.yaml
version: "3.9"
services:
  webapp:
    image: lchenlangley/count
    build: .
    ports:
      - 8000:5000
    volumes:
      - voltemp:/code
        #- .:/code
    environment:
      - MYREDIS_HOST=redisserver
      - FLASK_ENV=development
    depends_on:
      - redisserver
  redisserver:
    image: "redis:alpine"
volumes:
  voltemp:
        
# Add directories to allow bind mount in "FILE SHARING" under "Resources" of Docker Desktop
# 1. Create app.py
# 2. Create requirements.txt
# 3. Create Dockerfile
# 4. Create compose.yaml
# 5. Start containers

podman-compose up

# 6. Access the app
http://localhost:8000/
        
# end containers
podman-compose down