Setup a Docker container to work with a local database
Solution 1
There are three ways:
-
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
-
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 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 example172.17.0.1:3306
NOTE: ... means any other options that you may already be using
Solution 2
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.
Solution 3
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
Related videos on Youtube
Comments
-
Sekhemty over 1 year
I'm trying to setup a Docker container to work with a local database.
The image is this one https://hub.docker.com/r/tuxgasy/dolibarr/ and it suggests to also create a
mariadb
container, and link it to that.I would like to configure the Dolibarr container to instead use the
mariadb
database that I already have on my main system, that was installed from my distro's main repo.It's the first time that I try to setup a working Docker application, and I'm not that expert about database maintenance, so I'm a bit lost.
How can I do this? Please keep the instructions ad clear and detailed as possible.
My system is a fully updated openSUSE Tumbleweed.
-
executable almost 4 yearsHey, I'm having the same issue on CentOS 7, can you share the command you use to create a container for
tuxgasy/dolibarr
using the host database ?
-
-
Chris Vandevelde almost 4 yearsNote that
net=host
isn't available for macOS, so option 1 won't work.