I’m building a small app divided in 3 services using docker I have redisjson service a node server and a python application.
I always run the redis inside a container for development, in the first stages I run the python script and the node app natively I simply used to connect to the redis using localhost.
To standardize and deploy the environment I have set up containers for each service, but the moment I tried to connect to redis from the other containers it refuses the connection.
Below is my compose file, indentation might have got spooky during the copy pasta hopefully not.
I had to switch ports from 6379 to 7000 for redis because it was giving me connection problems as well.
version: '3.7'
networks:
app-tier:
driver: bridge
services:
redis:
image: 'redislabs/rejson:latest'
command: ["redis-server", "--bind", "redis", "--port", "6379", "--requirepass", "password"]
environment:
- appendonly
ports:
- '7000:6379'
volumes:
- redis:/data
networks:
- app-tier
node:
build: ./tickingServer
environment:
TZ: Europe/Rome
depends_on:
- redis
# - mariadb
links:
- "redis"
# - "mariadb"
ports:
- 3000:3000
restart: always
networks:
- app-tier
wsdaemon:
build: ./socketServer
environment:
TZ: Europe/Rome
depends_on:
- redis
links:
- "redis"
volumes:
- ./logs:/usr/src/websocketdaemon/logs
ports:
- 8000:8000
restart: always
networks:
- app-tier
volumes:
redis:
on my system the both the native port and the 7000 were free and now they correctly result in use from the docker-proxy.
According to the docker networking I’m using as hostname for the connection ‘redis’ as I named the service so. The python testing snippet for the connection is the following:
import log
import configuration
import redis
config = configuration.configuration()
logger = log.Log().get_logger(
__name__, config['logFolder'], config['logFormat'])
redis_client = redis.Redis(host='redis', port=7000, db=0, password='password')
the nodejs connector looks like this:
var Redis = require('ioredis');
var logger = require('../helpers/logHelper');
var JSONCache = require('redis-json');
let modName = "RedisConnector";
var redis = new Redis({
'host': 'redis',
'family': 4,
'db': 0,
'port': 7000,
'password': 'password'
});
2
Answers
I've managed to connect to redis from another container on the 6379 port instead of the 7000 despite mapping it as such, in the compose file.
I had to load a redis.conf file and I had to comment out the
Switch off the protected-mode
I had to remove all the keys from the default config that were malformed one by one manually.
Now, I'm running into the same issue while I'm trying to use the pubsub to propagate channel changes from a websocket python implementation using the rejson library.
Naturally code snippets from the official documentation channel don't actually work. I've been regretting using redis everyday to save and propagate counting data and I really hope I'll never have to use it again
Ultimately I solved it refactoring the config file down to this settings:
which is still probably just a stub and absurd amount of work to setup a development environment.
The service definition in the docker-compose file now looks as follows.