How to connect to Host from Docker Container?

Where there are many solutions as well as applications or a dedicated docker container which you can run, but all of these set up will require a lot of extra configuration. The solution I have found is really simple.


Idea is to find the docker host location IP address and send it as environment variable. For one system, the host IP address doesn’t change so it is restart proof. We generally use file or similar to send UID and GID along with this, you can add host IP as following:


export DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v | awk '{ print $2 }' | cut -f2 -d: | head -n1)
export UID=${UID}
export GID=${GID}
docker-compose up -d


Now you can pass $DOCKERHOST as environment variable in docker-compose.yml file as folloing:


Sample output

➜ ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | grep -v | awk '{ print $2 }' | cut -f2 -d: | head -n1

Use Case

My personal observation is that when I use MySQL inside the docker, it is three times slower than when I have it in native machine. Using MySQL on host machine gives me a great performance as well as it has full support of security related layers integrated solutions developed specially for host machines which is still not available inside docker.

Worth Sharing?


  1. Nice. I think that we still have to listen to mysql as because we can’t have envrionment variable inside mysql conf file.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>