Sunder
Sunder

Reputation: 223

How to connect external service from docker service

I am a newbie to docker. I am running Postgresql running on my local machine (i.e. On Mac OS). My application is running in docker. What I want is my application should be able to access PostgreSQL (has got a lot of production data, which can't be run on docker) service from docker. How to do it? Could anyone give me an example of doing so?

Upvotes: 7

Views: 15796

Answers (3)

Isuru
Isuru

Reputation: 722

Another solution is that you can run your app in the host network. For example in your compose file, under the service, set network-mode: host. Intead of creating a network the container app will join the local machine network, so all services can talk without port mapping.

services:
  my_app:
    build: .
    image: my_app:latest
    network_mode: host

Upvotes: 0

Sunder
Sunder

Reputation: 223

Finally, I found a solution to link external services inside docker. Added below lines in the docker-compose.yaml:

    extra_hosts:
       my-local-host: 10.0.0.10 #Local machine IP
     environment:
       - SERVER=http://my-local-host:3000

Upvotes: 5

tgogos
tgogos

Reputation: 25152

At my case (I have docker running on Ubuntu) there is a docker0 network interface and the host takes the IP 172.17.0.1. Every container that will attach to the default network will get an IP within the 172.17.0.X range. This way, containers can access services running on the host by talking to 172.17.0.1[:port] because they're on the same network...

But, things are different for Docker for Mac...

From: Networking features in Docker for Mac

Known limitations, use cases, and workarounds

Following is a summary of current limitations on the Docker for Mac networking stack, along with some ideas for workarounds. There is no docker0 bridge on macOS

Because of the way networking is implemented in Docker for Mac, you cannot see a docker0 interface in macOS. This interface is actually within HyperKit.

Use cases and workarounds

I want to connect from a container to a service on the host

The Mac has a changing IP address (or none if you have no network access). From 17.12 onwards our recommendation is to connect to the special Mac-only DNS name docker.for.mac.host.internal, which resolves to the internal IP address used by the host.

Upvotes: 2

Related Questions