Summary
One of the priorities of a system administrator is the performance optimization of the deployments and the underlying infrastructure. I have a deployed and running mariadb on a linux server. It is optimized for performance and database storage. Now I am planning deploying a dolibarr docker container on top of it.
How can I connect from the dolibarr docker container to the local mariadb database?
There are 3 ways you can connect your container.
Let’s go through them.
1. Use the --net=host
option
Important note: this option does not work on Mac OS X. If you are running OS X, jump to the second option.
When you run your docker container, you can pass the option --net=host
like this:
docker run --net=host ... tuxgasy/dolibarr
When you use that option, docker uses the hosts network stack for your container. That means that the container will have access to the whole host network stack. The container shares the services and ports available on the host.
Once in that mode the container has direct access to localhost. You can now access localhost:3306.
Note: This configuration does not provide any network isolation to the container.
2. Mount the service socket into the container
The idea here is to use the MariaDb database socket available on the host and mount it into the container.
What is a socket? A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. Source: Oracle docs
On the host, the socket is installed in the directory /var/run/mysqld
. We are going to mount that socket into the container.
docker run -v /var/run/mysqld:/mariadb_socket ... tuxgasy/dolibarr
Then, to access the database from the container, connect to the socket located at /mariadb_socket/mysqld.sock
3. Connect to the docker host IP
Docker has a network stack called docker0
.
The idea in this method is to find the IP address of that network and connect to it.
Start a command prompt and type ip addr
Look for the docker0
network. The IP address looks like 172.17.0.1
.
The result on your command line prompt might be different
Now that you have the IP address, you can use it in your docker container by connecting to 172.17.0.1:3309
.
Conclusion
We are detailed 3 ways to connect our dolibarr frontend container to the database located into your local server. Let me know in the comments if you have other ways.
If you have local docker images you want to use locally, head to this tutorial. You will be in good hands.