Skip to content

Commit 7e283e6

Browse files
committed
Fix Docker networking: use environment variables for Redis connection
The application was hardcoded to connect to localhost:6379, which doesn't work in Docker Compose where Redis runs in a separate container. Changes: - Updated chat.py to read REDIS_HOST and REDIS_PORT from environment variables * Defaults to localhost:6379 for local development * Can be overridden for Docker or other deployment scenarios - Updated docker-compose.yml to set REDIS_HOST=redis for container networking - Replaced deprecated 'links' with 'depends_on' in docker-compose.yml - Updated README.md with: * Modern Python 3.11+ and Redis 7+ requirements * Separate sections for local development and Docker Compose usage * Documentation of environment variables This fixes the "Connection refused" error when running via docker-compose.
1 parent 211e5ba commit 7e283e6

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
11
# redis-streams-fastapi-chat
22
A simple demo of Redis Streams backed Chat app using Websockets, Python Asyncio and FastAPI/Starlette.
33

4-
Requires Python version >= 3.6 and Redis
4+
Requires Python version >= 3.11 and Redis 7+
55

66
# Overview
77
This project has been created to help understand some related concepts. Python standard library asyncio, websockets (which are often cited as a classic use case for async python code), also Redis Streams. It is very much inteded to be an intentionally simple starting point rather than a usable product as is.
88

99
# Installation
1010

11+
## Local Development
12+
1113
```shell
1214
$ pip install -r requirements.txt
1315
```
1416

17+
Make sure you have Redis running locally:
18+
```shell
19+
$ redis-server
20+
```
21+
1522
# Usage
1623

24+
## Local Development
25+
1726
```shell
1827
$ python chat.py
1928
```
2029

21-
# Docker compose
22-
If you don't have redis installed you can use the docker-compose.yml file to set up a
23-
working environment.
30+
Then open http://localhost:9080 in your browser.
31+
32+
## Docker Compose
33+
34+
The easiest way to run the application with all dependencies:
35+
36+
```shell
37+
$ docker-compose up
38+
```
39+
40+
This will start both the chat application and Redis in containers. The app will be available at http://localhost:9080
41+
42+
## Environment Variables
43+
44+
The following environment variables can be configured:
45+
46+
- `REDIS_HOST` - Redis server hostname (default: `localhost`, set to `redis` in Docker)
47+
- `REDIS_PORT` - Redis server port (default: `6379`)
2448

chat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from websockets.exceptions import ConnectionClosedError, ConnectionClosedOK
1616
from redis.exceptions import ConnectionError as ServerConnectionClosedError
1717

18-
REDIS_HOST = 'localhost'
19-
REDIS_PORT = 6379
18+
REDIS_HOST = os.getenv('REDIS_HOST', 'localhost')
19+
REDIS_PORT = int(os.getenv('REDIS_PORT', '6379'))
2020
XREAD_TIMEOUT = 0
2121
XREAD_COUNT = 100
2222
NUM_PREVIOUS = 30

docker-compose.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ services:
88
- "8082:8082"
99
volumes:
1010
- .:/code
11-
links:
12-
- "redis"
11+
environment:
12+
- REDIS_HOST=redis
13+
- REDIS_PORT=6379
14+
depends_on:
15+
- redis
1316
redis:
1417
image: "redis:7-alpine"
1518
ports:

0 commit comments

Comments
 (0)