Access database that is outside the Docker environment

Asked

Viewed 1,090 times

0

I created an Microservices environment, more precisely 5 services, where they are connected to each other and access the same database (Postgresql). After development, I started creating the Docker images for the services. All images have been created, however, I cannot put postgresql in the Docker environment, as it is already running on the machine on localhost, and other applications depend on it, so I cannot migrate to the Docker environment. I wonder 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?

  • Docker is a VM, correct? Wouldn’t it be the case to point the access to the bank to your machine on the network, by IP? Or it doesn’t see anything on the network?

  • @Daniel then, he doesn’t see anything unless it is configured. However, it is in this configuration that I am unable to do. But it’s what you said, it’s like a VM.

  • @Cristianobombazar managed to solve? Using the --network="host" you can access your localhost quietly and access your bank

1 answer

1

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

Yes, it is possible. The moment you run your container with the docker run, you use the --network="host", thus, the Docker will see the local address 127.0.0.1, so you can make the connection normally with your bank.

Obs¹: If you’re using Docker for Mac or Docker for Windows 18.03+ can connect using host host.docker.internal.

bs²: There are other (some better) ways to do this, that reply details very well other forms.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.