If I start the official redis container, e.g. docker run -d -p 6379:6379 redis
, I can access it from host machine without any problem.
But my docker is based on alpine and I add redis like the following. This 4 lines Dockerfile will reproduce my problem. (It will add redis_version:6.2.6
as the current official redis container does)
FROM node:14-alpine3.14
RUN apk add redis bash
EXPOSE 6379
CMD ["redis-server"]
When I run it docker run -p 6379:6379 mydocker
and access redis-server from host, I can connect to it but got the error "Error: Server closed the connection"
127.0.0.1:6379> info
Error: Server closed the connection
The accepted answer in Connecting to Redis running in Docker Container from Host machine said I should bind 0.0.0.0
otherwise redis can only be access within container. But I feel that answer didn't explain the whole situation. For example why the normal redis container can be accessed from host? I didn't see redis.conf in the container, I can't find how it bind 0.0.0.0
.
How can I access redis running inside my alpine container?
--- update ---
I know if I have redis.conf
I can change the bind setting. For example on my mac I install redis at /opt/local/bin/
and redis.conf
at /opt/local/etc/
, in it has such setting,
# IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
# JUST COMMENT OUT THE FOLLOWING LINE.
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 127.0.0.1 -::1
But in the official redis docker container I can't find redis.conf
so I was confused how host can access it (One of the reasons I asked the question)