Reputation: 3
I tried to connect to the redis server in a standalone container from another standalone app container in my local machine.
At first, it all worked fine. Then, I created a dockerfile for the redis container rather than straight using the official image and changed the redis config file to my own one, where I only changed appendonly from no to yes. And it stopped working suddenly, with error redis.exceptions.ConnectionError: Error 111 connecting to redis:6379. Connection refused.
Here is my docker-compose.yml.
version: '3'
services:
flask:
build: ./flask
container_name: flask
volumes:
- ./flask:/src
expose:
- "5000"
links:
- redis
tty: true
restart: always
redis:
build: ./data/redis
container_name: redis
expose:
- "6379"
volumes:
- ./data/redis:/data
Here is the dockerfile for redis:
FROM redis:alpine
COPY ./redis.config /usr/local/etc/redis/redis.conf
CMD [ "redis-server", "/usr/local/etc/redis/redis.conf" ]
And the dockerfile for the app:
FROM python:3.6.9-alpine
WORKDIR /src
ADD . /src
RUN apk add python3-dev build-base linux-headers pcre-dev
RUN addgroup -S uwsgi && adduser -S uwsgi -G uwsgi
RUN pip install --no-cache-dir -r requirements.txt
CMD ["uwsgi","app.ini"]
I tried to ping redis from the app container and it worked fine.
/src # ping redis
PING redis (172.18.0.3): 56 data bytes
64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.121 ms
64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.163 ms
64 bytes from 172.18.0.3: seq=2 ttl=64 time=0.116 ms
64 bytes from 172.18.0.3: seq=3 ttl=64 time=0.166 ms
64 bytes from 172.18.0.3: seq=4 ttl=64 time=0.158 ms
^C
--- redis ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.116/0.144/0.166 ms
And also tried ping the app container inside the redis container and it also worked.
/data # ping flask
PING flask (172.18.0.4): 56 data bytes
64 bytes from 172.18.0.4: seq=0 ttl=64 time=0.106 ms
64 bytes from 172.18.0.4: seq=1 ttl=64 time=0.162 ms
64 bytes from 172.18.0.4: seq=2 ttl=64 time=0.163 ms
64 bytes from 172.18.0.4: seq=3 ttl=64 time=0.166 ms
64 bytes from 172.18.0.4: seq=4 ttl=64 time=0.143 ms
^C
--- flask ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.106/0.148/0.166 ms
The code for my simple counter app is
from app import app
from redis import Redis
redis = Redis(host='redis', port=6379)
@app.route('/')
def hello():
return f"<h1>This Compose/Flask demo has been viewed {redis.incr('hits')} time(s)</h1>."
# return "hello, world"
I am totally lost and have no idea how to fix it. I tried to use the default image without using dockerfile to build my own one and it worked. However, the only difference between the current setup with the default image is that I used my own redis.config where the only change I made was to change appendonly from no to yes. And I could not connect to redis now.
Could someone help me on that?
Upvotes: 0
Views: 4548
Reputation: 59896
There are two possible reasons when you use custom redis.conf and if you have taken your redis.conf from http://download.redis.io/redis-stable/redis.conf
bind 127.0.0.1
protected-mode yes
Comment this line bind 127.0.0.1
and disable protected mode in your redis.conf protected-mode no
.
By default protected mode is disabled in offical Redis Dockerfile.
Upvotes: 3