从链接容器访问环境变量

时间:2016-12-10 13:56:48

标签: node.js docker docker-compose

我想了解如何从链接的docker容器访问环境变量。我想从链接的rethinkdb容器访问我的节点应用程序中的主机/端口。使用docker compose(bluemixservice和rethinkdb):

version: '2'
services:
  twitterservice:
    build: ./workerTwitter
    links:
      - mongodb:mongolink
      - rabbitmq:rabbitlink
    ports:
      - "8082:8082"
    depends_on:
      - mongodb
      - rabbitmq
  bluemixservice:
    build: ./workerBluemix
    links: 
      - rabbitmq:rabbitlink
      - rethinkdb:rethinkdb
    ports:
      - "8083:8083"
    depends_on:
      - rabbitmq
      - rethinkdb
  mongodb:
    image: mongo:latest
    ports:
      - "27017:27017"
    volumes:
      - mongo-data:/var/lib/mongo
    command: mongod
  rabbitmq:
    image: rabbitmq:management
    ports:
      - '15672:15672'
      - '5672:5672'
  rethinkdb:
      image: rethinkdb:latest
      ports:
        - "8080:8080"
        - "28015:28015"
volumes:
  mongo-data:
    driver: local
  rethink-data:
    driver: local

我想在pm2 processes.json中访问它们:

{
    "apps": [
        {
            "name": "sentiment-service",
            "script": "./src",
            "merge_logs": true,
            "max_restarts": 40,
            "restart_delay": 10000,
            "instances": 1,
            "max_memory_restart": "200M",
            "env": {
                "PORT": 8080,
                "NODE_ENV": "production",
                "RABBIT_MQ": "amqp://rabbitlink:5672/",
                "ALCHEMY_KEY": "xxxxxxx",
                "RETHINK_DB_HOST": "Rethink DB Container Hostname?",
                "RETHINK_DB_PORT": "Rethink DB Container Port?",
                "RETHINK_DB_AUTHKEY": ""
            }
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

这曾经是可能的(参见here),但现在建议只使用链接服务名称作为主机名,就像你在rabbitmq的例子中所做的那样。关于端口号,我不认为它为此使用变量增加了很多;我只是按照普通号码去做。但是,如果您希望能够从外部快速更改值,则可以parameterize the whole docker-compose.yml using variables

请注意,不需要别名链接,我发现只使用服务名称会更清楚。

此外,links已暗示depends_on

答案 1 :(得分:0)

我通过使用consul和registrator来解决它,以检测我的所有容器。

result.Score