I have this code in Python:
import redis
r = redis.Redis(host="redis-db", password="foobared")
r.get("some-key")
But I got this error:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 611, in connect
sock = self.retry.call_with_retry(
File "/usr/local/lib/python3.10/site-packages/redis/retry.py", line 46, in call_with_retry
return do()
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 612, in <lambda>
lambda: self._connect(), lambda error: self.disconnect(error)
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 677, in _connect
raise err
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 665, in _connect
sock.connect(socket_address)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.10/site-packages/redis/commands/core.py", line 1705, in get
return self.execute_command("GET", name)
File "/usr/local/lib/python3.10/site-packages/redis/client.py", line 1235, in execute_command
conn = self.connection or pool.get_connection(command_name, **options)
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 1387, in get_connection
connection.connect()
File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 617, in connect
raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 111 connecting to redis-db:6379. Connection refused.
Here's my docker-compose.yml file:
services:
redis:
image: redis:7
container_name: redis-db
volumes: # I set my password in the conf file
- ./redis/redis.conf:/usr/local/etc/redis/redis.conf
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
ports:
- '6379:6379'
networks:
app_net:
ipv4_address: 10.10.0.6
depends_on:
- app
And I have another SEPARATE docker-compose.yml file in another directory (Don't ask me why I separated these):
services:
app:
build:
context: .
dockerfile: ./Dockerfile
container_name: socketio-sv
ports:
- '8000:8000'
volumes:
- ./:/app
networks:
- some_app_net
tty: true
networks:
some_app_net:
external: true
Then, I used docker network inspect app_net
to check both redis and socketio containers are in the same network:
"Containers": {
"415933ecbc99a05d28c35e34c183c860c7d96572e1b4d9473f0d7352fdaf8e29": {
"Name": "redis-db",
"EndpointID": "538c288d0292acfa7c6245a94ee5016a4568d10acf0bc818d23a758bc799cb47",
"MacAddress": "02:42:0a:0a:00:06",
"IPv4Address": "10.10.0.6/24",
"IPv6Address": ""
},
"dd1e4a70b0825bea8d1793d7f25cb95a41637c1424824627fff78df54a36af35": {
"Name": "socketio-sv",
"EndpointID": "0cb6f118226c4e22c45670fdc88be7dc2eec9e90e6e77271e64166016c19f4d4",
"MacAddress": "02:42:0a:0a:00:03",
"IPv4Address": "10.10.0.3/24",
"IPv6Address": ""
}
},
And as you can see, the both redis and socketio servers are in the same network.
I am not sure why this error happened. The password and the hostname are right, and I also exposed the right port.