如何为 Redis / RethinkDB(使用 Docker Compose)指定备用公开端口?

Nec*_*vil 1 redis node.js rethinkdb docker docker-compose

致力于 Docker 化一个 Nodejs 应用程序,我正在尝试对其进行设置,以便它能够从非标准端口进行响应,从而避免已经运行本地 Redis 容器或服务的团队成员发生潜在冲突。

Redis通常运行在6379上(无论是否有docker)。我希望它在 6380 上监听。即使我在 docker-compose 文件中没有它,我也想用 RethinkDB 做同样的事情。

我不想为 Redis 或 RethinkDB 创建新的 Dockerfile。

这是我的 Docker-Compose 文件。

  nodejsapp:
    image: some-node-container
    container_name: nodejsapp
    ports:
      - "5200:5200" #first number must match CHEAPOTLE_PORT env variable for the cheapotle service
    depends_on:
      - redis
      - rethinkdb
    volumes:
      - ./:/app
    environment:
      - NODEJSAPP_PORT=5200 #must match first port number for cheapotle service above.
      - REDIS_PORT=6380 #must match first number in redis service ->ports below.
      - RETHINKDB_PORT=28016 #must match first number in redis service ->ports below.

  redis:
    image: redis:3.2-alpine
    container_name: redis_cheapotle
    ports:
      - "6380:6379"
    expose:
      - "6380" # must match alternate "first" port above to avoid collisions

  rethinkdb:
    image: rethinkdb  
    container_name: rethinkdb_cheapotle
    ports:
      - "28016:28015" #The first number needs to match the RETHINKDB_PORT in environment variables for cheapotle above.  You must change both or none, they must always be the same.
      - "8090:8080" #this is where you will access the RethinkDB admin. If you have something on 8090, change the port to 8091:8080
    expose:
      - "28016" # must match alternate "first" port above to avoid collisions
Run Code Online (Sandbox Code Playgroud)

在做了一些dockerization之后,我认为这会很容易。我会设置我的环境变量,在我的 JS 文件中使用 proces.env.whatever,然后走出门,继续下一个。

错误的。

虽然我可以通过 0.0.0.0:8090 访问 RethinkDB 管理区域(注意 8090 备用端口),但我的容器都无法通过其指定端口相互通信。

起初,我在没有 YAML 的“expose”部分的情况下尝试了上述方法,但添加了“expose”YAML 后得到的结果与我得到的结果相同。

看起来 docker / 容器拒绝将进入主机->备用端口的流量转发到容器->标准端口。我在谷歌上搜索了一下,在前 20 分钟内没有找到任何东西,所以我想我会在继续搜索时发布此内容。

如果我自己在这个过程中找到答案,我会发布答案。

Nec*_*vil 6

好的。所以我能够解决这个问题,似乎官方 rethinkDB 和 Redis 容器处理端口转发的方式可能存在错误,因为正常端口:“XXXXX:YYYYY”YAML 规范被忽略,并且流量不是从修改后的主机发送的端口到标准 docker 端口。

解决方案是修改用于启动 Redis / RethinkDB 容器的命令,以使用命令行端口标志(每个系统不同)来更改为我的备用端口。

起初,我尝试使用环境变量 Env 文件(但显然这些在运行时不能立即可用)。我还希望用户能够直接在 Docker-Compose 文件中查看其堆栈的所有端口/设置,因此上述端口标志解决方案似乎有意义。

我仍然不知道为什么 docker 不会将备用主机端口流量转发到这两个服务的标准容器端口,而它将转发 rethinkDB 管理页面的备用主机端口(8090:8080)。

这是我最终得到的 docker-compose 文件:

version: "2"

services:

  nodejsapp:
    image: some-node-container
    container_name: cheapotle
    ports:
      - "5200:5200" #both numbers must match CHEAPOTLE_PORT env variable for the cheapotle service
    depends_on:
      - redis
      - rethinkdb
    volumes:
      - ./:/app
    environment:
      - CHEAPOTLE_PORT=5200 #must match cheapotle service ports above.
      - RETHINKDB_PORT=28016 #must match rethinkdb service->ports below.
      - REDIS_PORT=6380 #must match redis service ->ports below.
      - RESQUE_PORT=9292
    entrypoint: foreman start -f /app/src/Procfile

  redis:
    image: redis:3.2-alpine
    container_name: redis_cheapotle
    ports:
      - "6380:6380" #both numbers must match port in command below AND REDIS_PORT cheapotle service variable
    command: redis-server --port 6380 #must match above ports AND REDIS_PORT cheapotle service variable


  rethinkdb:
    image: rethinkdb  
    container_name: rethinkdb_cheapotle
    ports:
      - "28016:28016" #The both numbers must match the RETHINKDB_PORT in environment variables for cheapotle above + command below.  You must change allor none, they must always be the same.
      - "8090:8080" #this is where you will access the RethinkDB admin. If you have something on 8090, change the port to 8091:8080
    command: rethinkdb --driver-port 28016 --bind all #must match above ports AND REDIS_PORT cheapotle service variable
Run Code Online (Sandbox Code Playgroud)

RethinkDB 命令行实用程序的文档可以在这里找到: https: //www.rethinkdb.com/docs/cli-options/ 而 Redis 命令行的文档可以在这里找到:https: //redis.io /主题/配置

通过上述内容,我可以在备用端口上启动一切,这些端口不会在团队中的其他开发人员已经运行 rethinkDB 和/或 Redis 的情况下发生冲突。

这不是生产级设置,因此暂时使用需要您自担风险。显然,RethinkDB 需要额外的配置,以允许其他节点通过 29015 以外的其他端口加入集群。

还!作为任何使用 rethinkDB 命令行标志的人之前的警告:为了让 rethinkDB 接受端口更改,“--driver-port 28016”必须位于“--bind all”之前,否则就好像它不存在并被忽略。