I have a redis cluster running on my MacBook (localhost), I am easing my way into docker as part of my developer environment.
I also have things like MongoDB running on my host and am able to successfully connect to via host.docker.internal
, at this time i'm not looking to containerize redis, or my other services
Im specifically getting errors that my server is somehow trying to connect using 127.0.0.1
when my code is definitely using host.docker.internal
this is happening on the current nodejs container, but also another ruby container - so something about redis is unhappy
oauth | 2022-07-22T21:57:58.186Z app:cache:client FATAL Redis Cluster Error Error: connect ECONNREFUSED 127.0.0.1:30003
oauth | 2022-07-22T21:57:58.188Z app:cache:client FATAL Redis Cluster Error Error: connect ECONNREFUSED 127.0.0.1:30001
oauth | 2022-07-22T21:57:58.189Z app:cache:client FATAL Redis Cluster Error Error: connect ECONNREFUSED 127.0.0.1:30002
Dockerfile
# syntax=docker/dockerfile:1
FROM node:16.16.0-buster as base
WORKDIR /app
COPY package.json package.json
COPY yarn.lock yarn.lock
FROM base as dev
RUN yarn install
COPY . .
CMD ["node", "src/index.js"]
docker-compose.dev.yml
version: '3.8'
services:
oauth:
build:
context: .
container_name: oauth
ports:
- 5050:5050
environment:
- RACK_ENV=docker
- PORT=5050
volumes:
- ./:/app
command: yarn run nodemon ./src/index.js
docker-compose -f docker-compose.dev.yml up --build
[+] Building 6.8s (17/17) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 245B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> resolve image config for docker.io/docker/dockerfile:1 0.8s
=> [auth] docker/dockerfile:pull token for registry-1.docker.io 0.0s
=> CACHED docker-image://docker.io/docker/dockerfile:1@sha256:443aab4ca21183e069e7d8b2dc68006594f40bddf1b15bbd83f5137bd93e80e2 0.0s
=> [internal] load .dockerignore 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> [internal] load metadata for docker.io/library/node:16.16.0-buster 0.5s
=> [auth] library/node:pull token for registry-1.docker.io 0.0s
=> [internal] load build context 0.9s
=> => transferring context: 3.52MB 0.9s
=> [base 1/4] FROM docker.io/library/node:16.16.0-buster@sha256:2e1b4542d4a06e0e0442dc38af1f4828760aecc9db2b95e7df87f573640d98cd 0.0s
=> CACHED [base 2/4] WORKDIR /app 0.0s
=> CACHED [base 3/4] COPY package.json package.json 0.0s
=> CACHED [base 4/4] COPY yarn.lock yarn.lock 0.0s
=> CACHED [dev 1/2] RUN yarn install 0.0s
=> [dev 2/2] COPY . . 2.2s
=> exporting to image 2.0s
=> => exporting layers 2.0s
=> => writing image sha256:a297a1937c12a7403b12dec58a71c28df60caa0ee387daec51af2ffb0dc5968e 0.0s
=> => naming to docker.io/library/oauth_oauth 0.0s
[+] Running 1/1
⠿ Container oauth Recreated 0.1s
Attaching to oauth
oauth | yarn run v1.22.19
oauth | $ NODE_ENV=docker yarn run nodemon ./src/index.js
oauth | $ /app/node_modules/.bin/nodemon ./src/index.js
oauth | [nodemon] 2.0.16
oauth | [nodemon] to restart at any time, enter `rs`
oauth | [nodemon] watching path(s): *.*
oauth | [nodemon] watching extensions: js,mjs,json
oauth | [nodemon] starting `node ./src/index.js`
oauth | 2022-07-22T21:57:58.161Z app:index INFO ⚡️ Successfully Started Express Server
oauth | 2022-07-22T21:57:58.162Z app:index INFO ⚡️ Environment: docker
oauth | 2022-07-22T21:57:58.163Z app:index INFO ⚡️ Node Version: v16.16.0
oauth | 2022-07-22T21:57:58.163Z app:index INFO ⚡️ Listening on: http://localhost:5050
oauth | 2022-07-22T21:57:58.163Z app:index INFO ⚡️ OS linux
oauth | 2022-07-22T21:57:58.186Z app:cache:client FATAL Redis Cluster Error Error: connect ECONNREFUSED 127.0.0.1:30003
oauth | 2022-07-22T21:57:58.188Z app:cache:client FATAL Redis Cluster Error Error: connect ECONNREFUSED 127.0.0.1:30001
oauth | 2022-07-22T21:57:58.189Z app:cache:client FATAL Redis Cluster Error Error: connect ECONNREFUSED 127.0.0.1:30002
More info:
Redis Cluster is ran by doing the following: (i've been using this for several years, it works) https://developer.redis.com/explore/redisinsight/cluster/
$ cd ~/Documents/dev/redis-6.2.6; ./utils/create-cluster/create-cluster start;
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
$ redis-cli -c -p 30001
127.0.0.1:30001> ping
PONG
i connected to the container to prove that host.docker.internal resolves to host
$ docker exec -it <container id> /bin/bash
$ ping host.docker.internal
PING host.docker.internal (192.168.65.2) 56(84) bytes of data.
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=1 ttl=37 time=0.182 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=2 ttl=37 time=0.275 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=3 ttl=37 time=0.230 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=4 ttl=37 time=0.516 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=5 ttl=37 time=0.540 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=6 ttl=37 time=0.560 ms
64 bytes from 192.168.65.2 (192.168.65.2): icmp_seq=7 ttl=37 time=0.566 ms
^C
--- host.docker.internal ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 138ms
rtt min/avg/max/mdev = 0.182/0.409/0.566/0.161 ms
However, netstat doesnt return anything for 30001
only 30003
$ netstat -a | grep 30001
(nothing)
$ netstat -a | grep 30003
tcp4 0 0 localhost.30003 localhost.51793 ESTABLISHED
tcp4 0 0 localhost.51793 localhost.30003 ESTABLISHED
tcp6 0 0 *.30003 *.* LISTEN
tcp4 0 0 *.30003 *.* LISTEN
the default config for redis when it starts is here
# Settings
BIN_PATH="/Users/aronlilland/Documents/dev/redis-6.2.6/src"
CLUSTER_HOST=127.0.0.1
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""
# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.
if [ -a config.sh ]
then
source "config.sh"
fi
# Computed vars
ENDPORT=$((PORT+NODES))
if [ "$1" == "start" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Starting $PORT"
$BIN_PATH/redis-server --port $PORT --protected-mode $PROTECTED_MODE --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes ${ADDITIONAL_OPTIONS}
done
exit 0
fi