2016-10-18 07:50
浏览 413

通过HTTP for API Gateway在链接的Docker容器之间进行通信

I'm currently working on a golang web app which is currently one application consisting of numerous packages and is deployed in an individual docker container. I have a redis instance and a mysql instance deployed and linked as separate containers. In order to get their addresses, I pull them from the environment variables set by docker. I would like to implement an api gateway pattern wherein I have one service which exposes the HTTP port (either 80 for http or 443 for https) called 'api' which proxies requests to other services. The other services ideally do not expose any ports publicly but rather are linked directly with the services they depend on.

So, api will be linked with all the services except for mysql and redis. Any service that need to validate a user's session information will be linked with the user service, etc. My question is, how can I make my http servers listen to http requests on the ports that docker links between my containers.

图片转代码服务由CSDN问答提供 功能建议

我目前正在使用golang网络应用程序,该应用程序目前是一个由许多软件包组成的应用程序,并且已部署在 单个Docker容器。 我有一个redis实例和一个mysql实例作为单独的容器进行部署和链接。 为了获取它们的地址,我将它们从docker设置的环境变量中拉出。 我想实现一种api网关模式,其中有一个服务公开了称为“ api”的HTTP端口(对于HTTP为80或对于HTTPs为443),该服务将请求转发给其他服务。 理想情况下,其他服务不会公开公开任何端口,而是直接与其所依赖的服务链接。

因此,api将与除mysql和redis之外的所有服务链接。 任何需要验证用户会话信息的服务都将与用户服务链接,等等。我的问题是,如何使我的http服务器侦听docker链接到我的容器之间的端口上的http请求。 \ n

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doupu0619 2016-10-18 08:58

    Simplest way to do this is Docker Compose. You can simply define which services you want and Docker Compose automatically link them in a dedicated network. Suppose you have your goapp, redis, and mysql instance and want to use nginx as your reverse proxy. Your docker-compose.yml file looks as follows:

        image: redis
        image: mysql
        image: myrepo/goapp
        image: nginx
          - /PATH/TO/MY/CONF/api.conf:/etc/nginx/conf.d/api.conf
          - "443:443"
          - "80:80"

    The advantage is that you can reference any service from other services by its name. So from your goapp you can reach your MySQL server under hostname mysql and so on. The only exposed ports (i.e. reachable from the host machine) are 443 and 80 of nginx container.

    You can start the whole system with docker-compose up!

    解决 无用
    打赏 举报

相关推荐 更多相似问题