简体   繁体   中英

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. 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.

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:

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. The only exposed ports (ie reachable from the host machine) are 443 and 80 of nginx container.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM