1

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)

Qiulang
  • 10,295
  • 11
  • 80
  • 129
  • Is the Dockerfile you show your complete dockerfile? If not, then please show enough of it to let us reproduce your error. – Hans Kilian Mar 30 '22 at 12:57
  • Check my update, easy to reproduce. – Qiulang Mar 30 '22 at 13:11
  • The file you are looking for is under _/etc/redis.conf_ and the line you are looking for is the line 75. – β.εηοιτ.βε Mar 30 '22 at 18:22
  • @β.εηοιτ.βε As the "normal" redis docker image, I don't have that. – Qiulang Mar 31 '22 at 01:38
  • @β.εηοιτ.βε as in https://github.com/docker-library/redis/blob/15ed0a0c1cb60c5193db45d8b59a8707507be307/6.2/Dockerfile 6.2.6 – Qiulang Mar 31 '22 at 01:47
  • You do have it, the package in the package managers of the OS do install it. And I tested it with the Dockerfile you provided. If you still cannot find it, that means your are doing something you are not showing in this question, like mounting a volume. Please provide a [mre]. – β.εηοιτ.βε Mar 31 '22 at 05:40
  • I have found the answer. Also you misunderstand about what I said I don't have redis.conf in official image (I think I said it clearly) and you seem to jump to the conclusion too fast. – Qiulang Mar 31 '22 at 07:28
  • I am not jumping to conclusion, you don't seems to understand how an OS package manager is working. It does not just install binaries, it can also install configuration file, which it does in this case, see the content of the package `redis`: https://pkgs.alpinelinux.org/contents?file=&path=&name=redis&branch=v3.14. So you do have it, as soon as you are doing `apk add redis`. Try it: `docker run --rm -ti alpine:3.14 ash`, then `apk add --no-cache redis && ls /etc/redis.conf`. – β.εηοιτ.βε Mar 31 '22 at 19:06
  • So if you mean _I don't have it because I removed it_, then that is a totally other kind of fish and you are not showing that clearly in your question at all. And your premises are _This 4 lines Dockerfile will reproduce my problem_. If you did `rm` the file, it should be either explicitly state in your question or be part of the `RUN` instruction of your Dockerfile. – β.εηοιτ.βε Mar 31 '22 at 20:07
  • Please check my comments. I said "As the "normal" redis docker image, I don't have that. " I also listed the github dockerfile to show I don't have redis.conf in normal/official. NOT the 4 lines Docerkfile I created by myself. – Qiulang Apr 01 '22 at 03:18
  • I will stop. What is point of arguing this? I really don't understand. Is it that it hurts your ego so you have turned it into a win/lost argument? Anyway I will stop. I have figured out the reason myself and I thank you for being interested in my question and spending time for it. – Qiulang Apr 01 '22 at 03:21

1 Answers1

2

I have found the reason why I can't access redis from my alpine container, the protected-mode is yes. So I don't think it is about --bind 0.0.0.0.

So if I start the redis official docker image, the protected-mode is no, so I can access it from host.

127.0.0.1:6379> config get protected-mode
1) "protected-mode"
2) "no"

And I don't have redis.conf in the official image (I use find to search it and I can't find it)

But for my own docker, its protected-mode is yes, so I can't access from host. The redis.conf is indeed etc/redis.conf but redis-server doesn't use it by default.

So instead of using this dockerfile

FROM node:14-alpine3.14
RUN apk add redis bash
RUN sed -i 's/bind 127.0.0.1 -::1/# bind 127.0.0.1 -::1/g; s/protected-mode no/protected-mode yes/' /etc/redis.conf
EXPOSE 6379
CMD ["redis-server","/etc/redis.conf"]

It is easier to just use following and I can access it from host!

FROM node:14-alpine3.14
RUN apk add redis bash
EXPOSE 6379
CMD ["redis-server","--protected-mode no"]

I further check the redis official Dockerfile, https://github.com/docker-library/redis/blob/15ed0a0c1cb60c5193db45d8b59a8707507be307/6.2/Dockerfile#L55

# disable Redis protected mode [1] as it is unnecessary in context of Docker

So that explains it!

Qiulang
  • 10,295
  • 11
  • 80
  • 129