Docker compose: flask + redis

File structure

4 files here:

  • app.py (app needed)
  • requirements.txt (app needed)
  • Dockerfile
  • docker-compose.yml

App

File requirements.txt:

1
2
flask
redis

File app.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import os

from flask import Flask
import redis

app = Flask(__name__)

redis_host = os.environ.get("REDIS_HOST", "localhost")
rclient = redis.Redis(host=redis_host, port=6379)

@app.route('/')
def hello():
rclient.incr("counter", 1)
count = rclient.get("counter")
return f"You visited this page for {count} times"

To connect to redis, we use the env variable REDIS_HOST here if it exists, otherwise, use localhost. In the docker-compose.yml below you will set the REDIS_HOST to be redis-cache, so within the same docker network, our application can connect to the redis container. (Docker will auto point the DNS “redis-cache” to the redis-container IP)

Docker

File Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM python:3.9-slim

RUN useradd -m webapp
WORKDIR /home/webapp
COPY app.py ./
COPY requirements.txt ./
RUN pip install -r requirements.txt
RUN rm requirements.txt
RUN chown -R webapp:webapp .
USER webapp

EXPOSE 5000

# make sure it listens on 0.0.0.0
ENTRYPOINT ["flask", "run", "--host", "0.0.0.0"]

File docker-compose.yml:

1
2
3
4
5
6
7
8
9
10
11
services:
redis-cache:
image: redis
app:
build: .
environment:
REDIS_HOST: redis-cache
ports:
- "5000:5000"
depends_on:
- redis-cache

We set the env variable REDIS_HOST to be redis-cache.

Run

Start:

1
2
$ docker-compose build
$ docker-compose up -d

Then visit our app at “http://127.0.0.1:5000"

Stop:

1
$ docker-compose down