首页 > 解决方案 > Docker redis 未授权访问

问题描述

Digital Ocean 告诉我,Redis 可能会无意中暴露数据或配置错误以允许未经授权的访问。

一个 telnet 命令证实了这一点。

但是,我需要允许我在同一网络上的 docker 容器访问 Redis;启用外部访问似乎是唯一的方法!

如何配置我的 docker-compose 以允许在 2 个容器之间安全地访问:

version: '2'

services:

  redis:
    networks:
      - youtube
    image: redis
    ports:
      - 6379:6379
    expose:
      - "6379"
    container_name: youtube_cache
    command: sh -c 'redis-server --bind 0.0.0.0'
    restart: always

  node:
    tty: true # Enables debugging capabilities when attached to this container.
    image: 'docker.io/bitnami/node:12-debian-10'
    command: sh -c 'npm install && npm rebuild && node app'
    volumes:
      - .:/app
    links:
      - redis
    environment:
      - REDIS_URL=redis://youtube_cache
    container_name: youtube_scraper
    networks:
      - youtube
    restart: always

networks:
  youtube:
    external: true

标签: dockersecuritydocker-composeredis

解决方案


问题是您的 docker-compose 文件配置为将youtube_cache容器的端口 6379 暴露给youtube网络,而external. 要解决此问题,您需要:

  1. 停止通过暴露端口portsexpose仅供参考);
  2. 不要附加youtube_cacheexternal网络,而只是将它留在默认(内部)网络 docker-compose 自动创建;
  3. youtube_scraper容器也添加到该默认网络,以便它可以与第一个网络通信。
version: '3'

services:
  redis:
    image: redis
    container_name: youtube_cache
    command: sh -c 'redis-server --bind 0.0.0.0'
    restart: always

  node:
    tty: true # Enables debugging capabilities when attached to this container.
    image: 'docker.io/bitnami/node:12-debian-10'
    command: sh -c 'npm install && npm rebuild && node app'
    volumes:
      - .:/app
    links:
      - redis
    environment:
      - REDIS_URL=redis://youtube_cache
    container_name: youtube_scraper
    networks:
      - youtube
      - default
    restart: always

networks:
  youtube:
    external: true

推荐阅读