hrgui
5/3/2020

Docker: How to connect from container to Host

Note 6/6/2020: This only works on Macs and Windows, but if you know how to make it work on Linux leave a comment on Github

Yes, I don’t just do frontend development. I know some Docker. Just recently, I wanted to connect the APIs inside my docker-containers to what was outside my docker containers, the host.

I tried doing http://localhost:8080, but I realized I am a silly goose! That’s within the container, so that will not work.

Turns out docker has a special keyword 1 :

host.docker.internal

Now, in docker-compose, whenever I want to setup a Hasura Event Trigger, and I haven’t dockerize my app yet, all I need to do is the following things:

  1. Never hardcode the event trigger, use a environment varaible.
  2. In docker-compose, I can use the following:
EVENT_TRIGGER: http://host.docker.internal:8080

Footnotes

  1. https://docs.docker.com/docker-for-mac/networking/#there-is-no-docker0-bridge-on-macos

hrgui

Harman Goei (hrgui) is a developer that loves to make cool and awesome web applications. His strength is in HTML, CSS, JavaScript, but he is willing to code anywhere in the stack to make the web be awesome.

© 2024 Harman Goei