访问docker环境之外的数据库

时间:2018-08-24 20:11:30

标签: java docker docker-compose microservices

我创建了一个微服务环境,更确切地说是5个服务,它们在这里相互连接并访问相同的数据库(PostgreSQL)。开发之后,我开始为服务创建docker映像。所有映像均已创建,但是,我无法将postgreSQL放在docker环境中,因为它已经在localhost的机器上运行,并且其他应用程序也依赖它,因此我无法迁移到docker环境。我想知道我的应用程序是否可以访问环境之外的数据库?

下面,我的docker-compose

version: '2'
services:
    server:
        image: microservices/server:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        expose:
          - "8080"
        ports:
          - "8080:8080"
        networks:
          - microservices
    security-server:
        image: microservices/security-server:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
        expose:
          - "8081"
        ports:
          - "8081:8081"
        networks:
          - microservices
        restart: "always"
    api-gateway:
        image: microservices/api-gateway:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
          - security-server          
        expose:
          - "9999"
        ports:
          - "9999:9999"
        networks:
          - microservices
        restart: "always"         
    imovel:
        image: microservices/imovel:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
          - security-server
          - api-gateway        
        expose:
          - "8082"
        ports:
          - "8082:8082"
        networks:
          - microservices          
        restart: "always" 
    imovel2:
        image: microservices/imovel:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
          - security-server
          - api-gateway         
        expose:
          - "9098"
        ports:
          - "9098:9098"
        networks:
          - microservices          
        restart: "always"           
    cliente:
        image: microservices/cliente:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
          - security-server
          - api-gateway          
        expose:
          - "8083"
        ports:
          - "8083:8083"
        networks:
          - microservices
        restart: "always"            
networks:
  microservices:
    driver: bridge    

在引用的link中,他的问题是postgres不接受来自外部的连接。我的问题更多是从一开始,我应该在哪里开始配置连接?

1 个答案:

答案 0 :(得分:0)

您可以以撰写格式指定extra_hosts并将主机的IP地址作为环境变量传递。

extra_hosts:
       - "my_host:${HOST_IP}"

https://docs.docker.com/compose/compose-file/#extra_hosts