I have run a redis container using this command: docker run -d --name redis -p 6379:6379 redis
.
and I have run my Django app using this command: docker run -it -p 8000:8000 voucher
.
and my connection redis url is redis://redis:6379/1
.
But i cannot seem to connect to redis here is my error:
redis.exceptions.ConnectionError: Error -2 connecting to redis:6379. Name or service not known.
PS: I’m not using docker-compose
5
Answers
I have fix that by changing my redis connection url to
redis://172.17.0.2:6379/1
.But why is that ? How can I use container names instead of IPs?
Did you defined redis info in .env file?
That could lead to connection error too.
/.env
This is your host IP. You have a few ways to solve your problem.
1 – Use the hardcoded IP as you did.
2 – Use docker link to connect applications like:
3 – You can create a redis.conf with the follow content:
and use it in the startup like:
4 – You can use docker-compose to link then better, like in #2.
Both Container have to be in the same
Docker network
to be able to communicate via the Container names.I think the better option is docker-compose. if you like to use docker-compose here is the answer.
docker-compose.yml
now you can connect to Redis using the service name "redis".