Access database that is outside the docker environment

0

I created a microservice environment, more precisely 5 services, where they are connected to each other and access the same database (PostgreSQL). After development, I started to create the docker images for the services. All images have been created, however, I can not put postgreSQL in the docker environment, since it is already running on the machine in localhost, and other applications depend on it, so I can not migrate to the docker environment. I would like to know if it is possible for my applications to access the database that is outside the environment?

Below, my 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"
    imovel3:
        image: microservices/imovel:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
          - security-server
          - api-gateway         
        expose:
          - "9097"
        ports:
          - "9097:9097"
        networks:
          - microservices          
        restart: "always"
    imovel3:
        image: microservices/imovel:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
          - security-server
          - api-gateway         
        expose:
          - "9096"
        ports:
          - "9096:9096"
        networks:
          - microservices          
        restart: "always"
    imovel4:
        image: microservices/imovel:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
          - security-server
          - api-gateway         
        expose:
          - "9095"
        ports:
          - "9095:9095"
        networks:
          - microservices          
        restart: "always"
    imovel5:
        image: microservices/imovel:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
          - security-server
          - api-gateway         
        expose:
          - "9094"
        ports:
          - "9094:9094"
        networks:
          - microservices          
        restart: "always" 
    imovel6:
        image: microservices/imovel:latest
        mem_limit: 1073741824 # RAM 1GB
        environment:
          - SPRING_PROFILES_ACTIVE=docker
        depends_on:
          - server
          - security-server
          - api-gateway         
        expose:
          - "9093"
        ports:
          - "9093:9093"
        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    

Someone?

    
asked by anonymous 13.08.2018 / 20:48

1 answer

0

As your bank is installed on your machine host , you need to tell the container to access the network your machine is in, so it will see your host (the famous 127.0.0.1 ).

Yes, it is possible. When you run your container with docker run , you use --network="host" , so docker will see the 127.0.0.1 local address, so you can connect normally your bank.

obs¹: If you are using Docker for Mac or Docker for Windows 18.03+ can connect using the host host.docker.internal .

obs²: There are other ways (some better) to do this, this

18.09.2018 / 22:27