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

时间:2017-02-21 20:50:14

标签: node.js docker redis docker-compose rethinkdb

在Docker-izing一个nodejs App上工作,我正在尝试设置它,以便它从非标准端口响应,避免已经运行本地Redis容器或服务的团队成员发生潜在冲突。

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

我不想为EITHER 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

做了几个dockerizations后,我想到这很容易。我会设置我的环境变量,在我的JS文件中使用proces.env.whatever,然后出门到下一个。

错误。

  

虽然我可以在0.0.0.0:8090到达RethinkDB管理区域(注意   8090备用端口),我的容器都不能互相通话   超过他们指定的端口。

起初我尝试了上面没有YAML的'公开'部分,但是我得到的结果与'曝光'YAML一样。

似乎docker /容器拒绝将进入Host-> Alternate Port的流量转发到Container->标准端口。我做了一些谷歌搜索,并没有在前20分钟找到任何东西所以我想我会发布这个,而我继续我的搜索。

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

2 个答案:

答案 0 :(得分:1)

确定。所以我能够解决这个问题,似乎可能存在一个错误,官方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

可以在此处找到RethinkDB命令行实用程序的文档: https://www.rethinkdb.com/docs/cli-options/虽然可以在此处找到Redis命令行的文档:https://redis.io/topics/config

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

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

  

另外!作为任何使用rethinkDB命令行的人之前的警告   flags:用于rethinkDB接受端口更改" - driver-port   28016"必须在" - 绑定所有"之前否则它就好像不是   即使在那里也被忽视了。

答案 1 :(得分:0)

你应该link他们在一起:)

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

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

  rethinkdb:
    .
    .
    ports:
      - "28016:28015" 
      - "8090:8080"
    expose:
      - "28016"
相关问题