[英]Communication between linked docker containers over http for api gateway
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. 我目前正在使用golang网络应用程序,该应用程序目前是一个由许多软件包组成的应用程序,并已部署在单独的docker容器中。 I have a redis instance and a mysql instance deployed and linked as separate containers.
我有一个redis实例和一个mysql实例作为单独的容器进行部署和链接。 In order to get their addresses, I pull them from the environment variables set by docker.
为了获取它们的地址,我将它们从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.
我想实现一种api网关模式,其中有一个服务公开了称为“ api”的HTTP端口(对于HTTP为80或对于HTTPs为443),该服务将请求转发给其他服务。 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. 因此,api将与除mysql和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.
任何需要验证用户会话信息的服务都将与用户服务等链接。我的问题是,如何使我的http服务器侦听docker链接到我的容器之间的端口上的http请求。
Simplest way to do this is Docker Compose . 最简单的方法是Docker Compose 。 You can simply define which services you want and Docker Compose automatically link them in a dedicated network.
您可以轻松定义所需的服务,然后Docker Compose会在专用网络中自动链接它们。 Suppose you have your
goapp
, redis
, and mysql
instance and want to use nginx
as your reverse proxy. 假设您有
goapp
, redis
和mysql
实例,并想使用nginx
作为反向代理。 Your docker-compose.yml
file looks as follows: 您
docker-compose.yml
文件如下所示:
services:
redis:
image: redis
mysql:
image: mysql
goapp:
image: myrepo/goapp
nginx:
image: nginx
volumes:
- /PATH/TO/MY/CONF/api.conf:/etc/nginx/conf.d/api.conf
ports:
- "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. 因此,您可以从
goapp
以主机名mysql
等访问MySQL服务器。 The only exposed ports (ie reachable from the host machine) are 443
and 80
of nginx
container. 唯一公开的端口(即从主机可访问的端口)是
nginx
容器的443
和80
。
You can start the whole system with docker-compose up
! 您可以使用
docker-compose up
启动整个系统!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.