当前位置: 动力学知识库 > 问答 > 编程问答 >

Use consul to export IP and Port to another Docker container dynamically

问题描述:

The goal: Make an XHR call to http://$IP:$PORT/api/container.json from a web page served by the phpapp container without hardcoding environment variables for IP and Port of the microservice api container.

http://CONSULHOST:8500/v1/catalog/service/api will expose config for my api microservice.

Example:

[{"Node":"node",

"Address":"123.1.2.3",

"ServiceID":"163b5be3-5450-4cac-8416-c1630723bc42-n1:microlith_micro-hipchat_1:443",

"ServiceName":"hipchat",

"ServiceTags":["primary"],

"ServiceAddress":"123.234.234.234",

"ServicePort":32772

}]

I'm looking to get the ServiceAddress and ServicePort port listed above from that API endpoint by making an API call in my phpapp Docker container dynamically without hardcoding the information. I can then use this information in the front-end web browser as a Javascript call to http://IP_OF_APP_CONTAINER:PUBLIC_PORT/api/container.json

I have Consul installed with Registrator, both working in a Docker Swarm.

  • I don't want to hardcode config for my microservices.It must be created dynamically using Consul or environment.
  • I don't want to harcode Consul IP. It can be passed in somehow, but I must be able to do this via a bash build script.
  • The phpapp container is linked to the api container, but this does not seem to expose the front end port, only IP.

I've made a diagram of my use case:

docker-compose

-> swarm ->

phpapp container

api container

consul container

registrator container

The javascript that will make the XHR call will be client side, served from my php container. Both the phpapp and api containers are showing in consul, and I see them in Consul API, but I'm wondering on how to map this info into a config dynamically on the phpapp container, so that I can make an XHR call from the web client for the phpapp container.

分享给朋友:
您可能感兴趣的文章:
随机阅读: