Count
Development Mode

# 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

# 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:
      - 5000:5000
    volumes:
      - .:/code # enable hot reloading
    environment:
      - MYREDIS_HOST=redisserver
      - FLASK_ENV=development
    depends_on:
      - redisserver
  redisserver:
    image: "redis:alpine"
        
# 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

docker-compose up

# 6. Access the app
http://localhost:5000/

# 7. Continue to edit app.py with hot reloading
        
# end containers
docker-compose down
        
Deployment Mode

# 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
        
# 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"]
		
# Developer Side
# 1. Create app.py
# 2. Create requirements.txt
# 3. Create Dockerfile
# 4. Create an image docker.io/library/lchenlangley/count, lchenlangey is the account at Docker Hub

docker build -t lchenlangley/count .

# 5. Push the image to public registry (Docker Hub), or private registry (e.g. AWS ECR, Azure ACR)

docker login
docker push lchenlangley/count
        
# Deployment on Host

# compose.yaml
version: "3.9"
services:
  webapp:
    image: lchenlangley/count
    ports:
      - 8000:5000
    environment:
      - MYREDIS_HOST=redisserver
      - FLASK_ENV=development
    depends_on:
      - redisserver
  redisserver:
    image: "redis:alpine"
        
# Start containers
docker-compose up
        
# User Side

http://localhost:8000/
        
# Stop the app
docker-compose down