构建redis镜像加入compose

自己构建了redis镜像,并尝试将其加入compose,发现无法连接redis服务。
这是我的Dockerfile

FROM        ubuntu:18.04
RUN         apt-get update
RUN         apt-get -y install redis-server
EXPOSE      6379

修改redis.conf

#bind 0.0.0.0
#protected-mode no

修改django项目中redis的连接设置

redis.ConnectionPool(host='redis',port=6379,decode_responses=True)

docker-compose.yml文件redis以及需要redis服务的部分

version: "3"
services:

    redis:
      image: redis:01
      command: /bin/bash -c "/usr/bin/redis-server"
      restart: always
      user: root
      volumes:
        - ./redis.conf:/etc/redis/redis.conf
      networks:
        jyt-net:
            aliases:
                - redis
  store:
    image: jyt:0
    command: python store_server.py
    working_dir: /code/start/
    volumes:
      - ./jyt:/code
    user: root
    restart: always
    depends_on:
      - redis
      - db
    networks:
        jyt-net:
            aliases:
                - store
networks:
    jyt-net:
        external:
           name: jyt-net

compose的输出日志:

redis_1  | 1:C 27 Feb 08:21:48.057 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 27 Feb 08:21:48.057 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 27 Feb 08:21:48.057 # Warning: no config file specified, using the default config. In order to specify a config file use /usr/bin/redis-server /path/to/redis.conf
redis_1  | 1:M 27 Feb 08:21:48.059 * Running mode=standalone, port=6379.
redis_1  | 1:M 27 Feb 08:21:48.059 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1  | 1:M 27 Feb 08:21:48.059 # Server initialized
redis_1  | 1:M 27 Feb 08:21:48.059 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis_1  | 1:M 27 Feb 08:21:48.059 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1  | 1:M 27 Feb 08:21:48.059 * DB loaded from disk: 0.000 seconds
redis_1  | 1:M 27 Feb 08:21:48.059 * Ready to accept connections
store_1  | Traceback (most recent call last):
store_1  |   File "store_server.py", line 34, in <module>
store_1  |     msg_to_db=mesg_redis.rpop('data')
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 1655, in rpop
store_1  |     return self.execute_command('RPOP', name)
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 775, in execute_command
store_1  |     return self.parse_response(connection, command_name, **options)
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 789, in parse_response
store_1  |     response = connection.read_response()
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/connection.py", line 642, in read_response
store_1  |     raise response
store_1  | redis.exceptions.ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.
store_1  | Traceback (most recent call last):
store_1  |   File "store_server.py", line 34, in <module>
store_1  |     msg_to_db=mesg_redis.rpop('data')
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 1655, in rpop
store_1  |     return self.execute_command('RPOP', name)
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 772, in execute_command
store_1  |     connection = pool.get_connection(command_name, **options)
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/connection.py", line 1003, in get_connection
store_1  |     raise ConnectionError('Connection not ready')
store_1  | redis.exceptions.ConnectionError: Connection not ready
store_1  | Traceback (most recent call last):
store_1  |   File "store_server.py", line 34, in <module>
store_1  |     msg_to_db=mesg_redis.rpop('data')
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 1655, in rpop
store_1  |     return self.execute_command('RPOP', name)
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 775, in execute_command
store_1  |     return self.parse_response(connection, command_name, **options)
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/client.py", line 789, in parse_response
store_1  |     response = connection.read_response()
store_1  |   File "/usr/local/lib/python3.6/dist-packages/redis/connection.py", line 642, in read_response
store_1  |     raise response
store_1  | redis.exceptions.ResponseError: DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

如果有头绪还请指点一二
(注:1、由于需要将多个服务整合到一个镜像中,所以不考虑从官方拉取镜像。
2、已验证过,使用官方镜像能够正常连接服务)

已找到原因,修改redis配置文件之后需要执行redis-server --protected-mode no使配置生效

1 Like