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

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

Redis通常运行在6379(不pipedocker或no)。 我希望它听6380.即使我没有在docker-compose文件中,我想用RethinkDB做同样的事情。

我不想为Redis或RethinkDB创build一个新的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 

在做了几次docker之后,我觉得这很容易。 我会设置我的环境variables,使用proces.env.whatever在我的JS文件,并出门,然后到下一个。

错误。

虽然我可以在0.0.0.0:8090(注意8090备用端口)到达RethinkDBpipe理区,但是我的任何一个容器都不能通过指定的端口相互通信。

起初,我尝试了上述没有YAML的“暴露”部分,但我有同样的结果,我与'揭露'YAML添加。

看起来像docker/容器拒绝将进入主机 – >备用端口的stream量转发到容器 – >标准端口。 我做了一些Googlesearch,在前20分钟没有find任何东西,所以我想我会继续我的search。

如果我在这个过程中find答案,我会发布一个答案。

你应该把它们连在一起:)

  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规范被忽略,stream量不是从修改的主机发送端口转换为标准docker端口。

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

起初,我尝试使用环境variablesEnv文件(但显然这些在运行时不可用)。 我也希望用户能够直接在Docker-Compose文件中看到他们堆栈的所有端口/设置,所以上面的端口标志解决scheme似乎是有道理的。

我仍然不知道为什么docker不会为这两个服务将备用主机端口stream量转发到标准容器端口,而是为rethinkDBpipe理页面(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的命令行工具的文档可以在这里find: https ://www.rethinkdb.com/docs/cli-options/虽然Redis命令行的文档可以在这里find: https ://redis.io /主题/configuration

通过上面的介绍,我可以在备用端口上启动所有其他端口,这些端口不会在团队中的其他开发者已经在运行rethinkDB和/或Redis的情况下发生冲突。

这不是一个生产等级设置,所以暂时用你自己的风险。 显然,RethinkDB需要额外的configuration来允许其他节点join群集,而不是在29015以外的其他端口。

也! 作为在使用rethinkDB命令行标志之前的警告,为了让rethinkDB接受端口的改变,“–driver-port 28016”必须在“–bind all”之前,否则就好像它不在那里并被忽略。