Stuff & Nonsense

Docker – add host.docker.internal on linux

Just a quick note (for my own memory more than anything)…

If you need to connect from a docker container to something running on your local host, it can be a bit of a pain on Linux. On windows & Mac, there’s a special DNS entry ‘host.docker.internal’ which always points to the host machine. However that isn’t available on Linux (here’s a bug open for this as of May 2018: https://github.com/docker/for-linux/issues/264)

That bug report also contains a pretty good solution for this problem, so to save you wading through, simply add the following line to your docker file:

RUN ip -4 route list match 0/0 | awk '{print $3 "host.docker.internal"}' >> /etc/hosts

That adds an entry to the container’s Host file, so host.docker.internal will resolve to your host just like it does on Mac / Windows.

3 thoughts on “Docker – add host.docker.internal on linux

  1. missing a space before host.docker.internal
    RUN ip -4 route list match 0/0 | awk ‘{print $3 ” host.docker.internal”}’ >> /etc/hosts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.