Setup a Docker container to work with a local database

There are three ways:

  1. Use the --net=host option. This network mode essentially means that the container has direct access to localhost and you can now access localhost:3306. Here's the command

    docker run --net=host ... tuxgasy/dolibarr

    Then connect to mariadb with localhost:3306

  2. Mount the mariadb socket to the docker container and connect to mariadb via socket. For example if you configure the socket's location to be /var/run/mysqld/mysqld.sock then you could mount and use that as your connection point.

    docker run -v /var/run/mysqld:/mariadb_socket ... tuxgasy/dolibarr

    Then connect to mariadb via the socket /mariadb_socket/mysqld.sock from your app

  3. Use the docker host's ip. First get the host ip address on the docker network (in linux type ip addr show and look for the docker0 ip). This is usually something like 172.17.0.1 (your mileage may vary). Then you should be able to use that ip address to connect to mariadb for example 172.17.0.1:3306

NOTE: ... means any other options that you may already be using


I've created a docker container for doing exactly that https://github.com/qoomon/docker-host

You can then simply use container name dns to access host system from inside a container e.g. curl http://dockerhost:9200


As of Docker v18.03+ you can use the host.docker.internal hostname to connect to your Docker host.

This is for development purpose and will not work in a production environment outside of Docker Desktop.

link