https://github.com/thiskevinwang/rust-redis-docker/tree/for-stackoverflow
Context:
Cargo.toml
:
[dependencies]
redis = "0.16.0"
hyper = "0.13"
Things that work:
bitnami/redis
, published with -p 6379:6379
redis-cli
can connect to this docker container successfullylocalhost:3000
GET /
displays some text ✅GET /redis
increments a counter in redis, and displays it ✅The rust docker container fails to connect to the redis docker container
bitnami/redis
,localhost:3000
GET /
displays some text, as before ✅GET /redis
causes rust code to panic ❌
Connection refused (os error 111)
I'm not sure if this is a problem with me incorrectly handling "docker networking" or if I'm using the redis crate incorrectly (although documentation is pretty sparse).
You have this in docker-compose.yaml
:
services:
hyper:
build: .
ports:
- "3000:3000"
expose:
- "3000"
links:
- redis # links this container to "redis" container
redis:
image: "bitnami/redis:latest"
ports:
- "6379:6379"
expose:
- "6379"
From the Docker Compose docs on links:
Containers for the linked service are reachable at a hostname identical to the alias, or the service name if no alias was specified.
So therefore the error is on line 70 in main.rs
:
let client = redis::Client::open("redis://127.0.0.1:6379")?;
That doesn't work since the redis instance is not running in the same container as your Rust code. You have to connect to it through the link established in your docker compose file, which in this case would be:
let client = redis::Client::open("redis://redis:6379")?;
Once you make this fix fetching GET localhost:3000/redis
returns successfully.