Access host database from a docker container

If I have a mysql database running on some host machine, and that host is also running a docker container: How would I access the mysql database from within the docker container that is running on the host?.

For instance, is there a way to publish a hosts port to the container (the inverse of what docker run -p does)?

  • Most appropriate container for a data only container?
  • Can you use nginx reverse proxy to docker containers without exposing any ports?
  • Meaning of docker-compose exit code?
  • connecting to a docker-compose mysql container denies access but docker running same image does not
  • How to set custom context for docker.build in jenkinsfile
  • docker run with ENTRYPOINT containing a variable
  • Docker commit without running
  • Dynamically assigns different value for the environment variable upon a Docker service scale
  • Kubernetes pod fails while making call to google cloud pub/sub with unknown certificate autority
  • What is the proper command to restart a Dokku app from SSH?
  • Installing OpenFOAM through Docker
  • Cannot start mysql or mysqld_safe on ubuntu
  • One Solution collect form web for “Access host database from a docker container”

    There are several long standing discussions about how to do this in a consistent, well understood and portable way. No complete resolution but I’ll link you to the discussions below.

    In any event you many want to try using the –add-host option to docker run to add the ip address of the host into the container’s /etc/host file. From there it’s trivial to connect to the host on any required port:

    Adding entries to a container hosts file

    You can add other hosts into a container’s /etc/hosts file by using
    one or more –add-host flags. This example adds a static address for a
    host named docker:

     $ docker run --add-host=docker:10.180.0.1 --rm -it debian
        $$ ping docker
        PING docker (10.180.0.1): 48 data bytes
        56 bytes from 10.180.0.1: icmp_seq=0 ttl=254 time=7.600 ms
        56 bytes from 10.180.0.1: icmp_seq=1 ttl=254 time=30.705 ms
        ^C--- docker ping statistics ---
        2 packets transmitted, 2 packets received, 0% packet loss
        round-trip min/avg/max/stddev = 7.600/19.152/30.705/11.553 ms
    

    Note: Sometimes you need to connect to the Docker host, which means
    getting the IP address of the host. You can use the following shell
    commands to simplify this process:

     $ alias hostip="ip route show 0.0.0.0/0 | grep -Eo 'via \S+' | awk '{ print \$2 }'"
     $ docker run  --add-host=docker:$(hostip) --rm -it debian
    

    Documentation:

    https://docs.docker.com/engine/reference/commandline/run/

    Discussions on accessing host from container:

    https://github.com/docker/docker/issues/1143

    https://github.com/docker/docker/issues/10023

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.