如何从应用程序服务器容器连接到Rabbitmq容器

问题描述:

我是docker的新手,我正在尝试将我用Golang编写的此应用程序docker化.这是一个简单的Web服务器,可与Rabbitmq和mongodb进行交互

I am new to docker and I am trying to dockerize this application I have written in Golang. It is a simple web server that interacts with rabbitmq and mongodb

它将凭据形成一个toml文件并将其加载到配置结构中,然后在端口3000上启动应用程序服务器.这些是凭据

It takes the creadentials form a toml file and loads it into a config struct before starting the application server on port 3000. These are the credentials

mongo_server = "localhost"
database = "collect_db"
rabbitmq_server = "amqp://guest:guest@localhost:5672/"

如果无法连接到这些URL,它将失败并显示错误.以下是我的docker-compose.yml

If it can't connect to these urls it fails with an error. Following is my docker-compose.yml

version: '3'
services:
  rabbitmq:
    image: rabbitmq 
    ports:
      - 5672:5672
  mongodb:
    image: mongo
    ports:
      - 27017:27017
  web:
    build: .
    image: palash2504/collect
    container_name: collect_service
    ports:
      - 3000:3000
    depends_on:
      - rabbitmq
      - mongodb
    links: [rabbitmq, mongodb]

但是它无法与用于本地开发的URL上的Rabbitmq连接,即 amqp://guest:guest @ localhost:5672/

But it fails to connect with rabbitmq on the url used for local development i.e. amqp://guest:guest@localhost:5672/

我意识到,rabbitmq容器可能在配置文件中提供的地址以外的其他地址上运行.

I realise that the rabbitmq container might be running on some different address other than the one provided in the config file.

我想知道设置任何环境凭证的正确方法,以便能够将我的应用程序连接到Rabbitmq.

I would like to know the correct way for setting any env credentials to be able to connect my app to rabbitmq.

还有什么方法最适合更改我的应用程序代码以初始化与外部服务的连接?我当时正在考虑放弃config.toml文件,并使用os.Getenv和os.Setenv来获取连接的URL.

Also what approach would be best to change my application code for initializing connections to external services? I was thinking about ditching the config.toml file and using os.Getenv and os.Setenv to get the urls for connections.

本地主机地址可以在本地解析.因此,它们将无法在容器内部工作,因为它们将寻找本地地址(即在容器内部).

Localhost addresses are resolved, well, locally. They thus will not work inside containers, since they will look for a local address (i.e. inside the container).

服务可以通过使用服务名称作为地址来相互访问.因此,例如,您可以在网络容器中定位 mongodb .

Services can access each other by using service names as an address. So in the web container you can target mongodb for example.

您可以试一下:

mongo_server = mongodb
database = "collect_db"
rabbitmq_server = "amqp://guest:guest@rabbitmq/"

建议在撰写文件本身中设置服务目标环境变量:

It is advisable to set service target environment variables in the compose file itself:

#docker-compose.yml

#...other stuff...

web:
    #...other stuff...
    environment:
      RABBITMQ_SERVER: rabbitmq
      MONGO_SERVER: mongodb
    depends_on:
      - rabbitmq
      - mongodb

这使您可以在单个位置进行配置调整.

This gives you a single place to make adjustments to the configuration.

作为旁注,在我看来,链接:[rabbitmq,mongodb] 可以删除.而且我建议不要更改容器名称(除非必要,请删除 container_name:collect_service )

As a side note, to me it seems that links: [rabbitmq, mongodb] can be removed. And I would advise not to alter the container name (remove container_name: collect_service unless it is necessary)