How to connect external service from docker service

孤街浪徒 提交于 2019-12-24 10:12:10

问题


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?

Many thanks in advance


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/49148920/how-to-connect-external-service-from-docker-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!