2

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

3
  • why you don't use docker-compose. its really easy to setup Commented Oct 12, 2021 at 10:17
  • 1
    I wanted to know both sides. @S.Sachith Commented Oct 12, 2021 at 10:54
  • oh, thats nice :) Commented Oct 12, 2021 at 11:15

5 Answers 5

2

Both Container have to be in the same Docker network to be able to communicate via the Container names.

docker run -d --name redis --network your-network -p 6379:6379 redis

docker run -it --network your-network -p 8000:8000 voucher
Sign up to request clarification or add additional context in comments.

2 Comments

correct, but maybe you could mention that your-network has to be created with docker network create ... before it can be referenced (for completeness)
connected them to bridge network but didn't work
1

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?

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:

docker run --name my-redis-application --link my-redis-container:redis -d centos

3 - You can create a redis.conf with the follow content:

bind 0.0.0.0

and use it in the startup like:

docker run -v $(pwd):/usr/local/etc/redis/ --name redis -p 6379:6379 -d redis redis-server /usr/local/etc/redis/redis.conf

4 - You can use docker-compose to link then better, like in #2.

Comments

1

I think the better option is docker-compose. if you like to use docker-compose here is the answer.

docker-compose.yml

version: "3.8"
services :
  redis:
    image : redis
    container_name : redis
    restart : unless-stopped
    ports: 
      - 6379:6379 
  voucher:
    image : voucher
    ports:
      - 8000:8000
    depends_on:
      - redis

now you can connect to Redis using the service name "redis".

Comments

0

Did you defined redis info in .env file? That could lead to connection error too.

/.env

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=yourpassword/blank
REDIS_PORT=6379

2 Comments

yeah I have defined them in a .env file but that was not the problem.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

I have fixed 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?

3 Comments

How where you using the container name?
my container name was redis and my connection url was redis://redis:6379/1
In order to use the DNS of docker your need to put the containers in the default bridge network. Did you check that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.