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

How can I specify an Alternate Exposed Port for Redis / RethinkDB (using Docker Compose)?

正在 Docker-izing 一个 nodejs 应用程序,我正在尝试设置它,以便它从非标准端口响应,从而避免已经 [=43= 的团队成员发生潜在冲突] 本地 Redis 容器或服务。

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

我不想为 Redis 或 RethinkDB 创建新的 Docker 文件。

这是我的 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

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

错了。

While I can get to the RethinkDB admin area at 0.0.0.0:8090 (notice the 8090 alternate port), none of my containers can talk to each other over their specified ports.

起初我在没有 YAML 的 'expose' 部分的情况下尝试了上述操作,但是我得到了与添加的 'expose' YAML 相同的结果。

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

如果我自己在这个过程中找到答案,post 会得到答案吗?

你应该 link 他们在一起:)

  nodejsapp:
    .
    .
    ports:
      - "5200:5200"
    .
    .
    links:
        - redis
        - rethinkdb

  redis:
    .
    .
    ports:
      - "6380:6379"
    expose:
      - "6380"

  rethinkdb:
    .
    .
    ports:
      - "28016:28015" 
      - "8090:8080"
    expose:
      - "28016"

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

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

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

I still don't know why docker wont forward alternate host port traffic to the standard container port for these two services while it WILL forward an alternate host port for the rethinkDB admin page (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

可在此处找到 RethinkDB 命令行实用程序的文档: https://www.rethinkdb.com/docs/cli-options/ While the docs for the Redis command line can be found here: https://redis.io/topics/config

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

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

Also! As a warning before anyone working with rethinkDB command line flags: for rethinkDB to accept a change in port the "--driver-port 28016" MUST be before the "--bind all" otherwise it's as if it is not even there and ignored.