DEV Community

Discussion on: Docker Tip - How to use the host's IP Address inside a Docker container on macOS, Windows, and Linux

Collapse
 
waterloomatt profile image
Matt Skelton • Edited

Running WSL (Ubuntu 20.04 LTS), I added this to my .bashrc file.

export DOCKER_GATEWAY_HOST="`/sbin/ip route|awk '/dev eth0 proto kernel/ { print  $9}'`"
Enter fullscreen mode Exit fullscreen mode

This runs /sbin/ip route, searches for the line containing "dev eth0 proto kernel", then grabs the 9th element from that line which corresponds to the IP address.

Then, in docker-compose.yml I referenced it here,

environment:
    - ...
    - PUBLIC_HOST=$DOCKER_GATEWAY_HOST
    - ...
Enter fullscreen mode Exit fullscreen mode