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 | flask |
File app.py
1 | import os |
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 | FROM python:3.9-slim |
File docker-compose.yml:
1 | services: |
We set the env variable REDIS_HOST to be redis-cache.
Run
Start:
1 | $ docker-compose build |
Then visit our app at “http://127.0.0.1:5000"
Stop:
1 | $ docker-compose down |