Tuesday, October 22, 2019

How to connect from docker container to host local service

Elasticsearch is running on localhost MacOS.
run image with curl and test connection to host local webservice.
Some interesting steps are at https://dev.to/bufferings/access-host-from-a-docker-container-4099 , it explains why networks are not wokrigin MacOS (it is not supported).

custom docker net creation - https://nickjanetakis.com/blog/docker-tip-35-connect-to-a-database-running-on-your-docker-host

$ docker container run --rm -it curlimages/curl sh
Unable to find image 'curlimages/curl:latest' locally
latest: Pulling from curlimages/curl
9d48c3bd43c5: Pull complete
9fbab065c978: Pull complete
Digest: sha256:4c7b687d607d7f5f397db1ca877a626fedc79c7a537a3f145482083bd68dc115
Status: Downloaded newer image for curlimages/curl:latest

/ $ curl 'localhost:9200/_cat/indices?v'
curl: (7) Failed to connect to localhost port 9200: Connection refused
#### This is expected as elastic search in NOT in docker container

# base on https://docs.docker.com/docker-for-mac/networking/
/ $ ping host.docker.internal
PING host.docker.internal (192.168.65.2): 56 data bytes
ping: permission denied (are you root?)

/ $ curl 'host.docker.internal:9200/_cat/indices?v'
health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   conv_v12 tIP0ha4USEG3Wr5cKJNzGw   5   1     837120            0      1.2gb          1.2gb
yellow open   conv_v11 0e-Y9pnlQHGyFTicGfQHbg   5   1    3917501           27      5.6gb          5.6gb
yellow open   geota    lSixJug2QDqtR8sjtXWdKg   5   1      69070            0      1.6gb          1.6gb
yellow open   conv_v10 9WX0fuMgRTCem0OgHn9fPw   5   1      67706            0    127.6mb        127.6mb

/ $ curl '192.168.65.2:9200/_cat/indices?v'
health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   conv_v12 tIP0ha4USEG3Wr5cKJNzGw   5   1     832120            0      1.2gb          1.2gb
yellow open   conv_v11 0e-Y9pnlQHGyFTicGfQHbg   5   1    3917501           27      5.6gb          5.6gb
yellow open   geota    lSixJug2QDqtR8sjtXWdKg   5   1      69070            0      1.6gb          1.6gb
yellow open   conv_v10 9WX0fuMgRTCem0OgHn9fPw   5   1      67706            0    127.6mb        127.6mb

No comments:

Post a Comment