Skip to content

Instantly share code, notes, and snippets.

@EliasJorgensen
Last active December 26, 2020 00:35
Show Gist options
  • Save EliasJorgensen/874dc6278a1df438a5e9c897923ee1f1 to your computer and use it in GitHub Desktop.
Save EliasJorgensen/874dc6278a1df438a5e9c897923ee1f1 to your computer and use it in GitHub Desktop.
Send data from docker container to local machine

Send data from Docker container to local machine

In some cases you may need to send data from your Docker containers to your local machine. This could be for sending data to a local webserver or something else entirely, but as it isn’t something that Docker directly supports, we need a bit of a workaraound to achieve it. Luckily, this is why this gist exists. No need to scour the web for hours and contemplate your existance in search for a solution, i’ve already done that part for you!

So there are two ways of achieving this, depending on if you’re running Ubuntu or macOS, as Docker runs inside of a VM on macOS.

  • On Ubuntu:

    • Add the following alias to your .bashrc (or whatever shell you’re using):

      • alias hostip="ip route show 0.0.0.0/0 | grep -Eo 'via \S+' | awk '{ print \$2 }'"
    • Next you need to add the following to your docker-compose.yml on all the containers needing access:

      extra_hosts:
          - "LOCAL_DOMAIN:$(hostip)"
      

      Where LOCAL_DOMAIN is the domain you need it to access.

  • On macOS:

    • Add the following alias to your .bashrc (or whatever shell you’re using):

      • alias hostip="sudo ifconfig lo0 alias 172.16.123.1 && echo '172.16.123.1'"
    • Next you need to add the following to your docker-compose.yml on all the containers needing access:

      extra_hosts:
          - "LOCAL_DOMAIN:$(hostip)"
      

      Where LOCAL_DOMAIN is the domain you need it to access.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment