Deploy container to use Host database

Portainer is a lightweight management UI which allows you to easily manage your Docker host or Swarm cluster.

Moderator: Lillian.W@AST

Post Reply
krunchynug8
Posts: 12
youtube meble na wymiar Warszawa
Joined: Thu May 28, 2020 1:30 pm

Deploy container to use Host database

Post by krunchynug8 »

Hi folks. I want to know how to get a docker container to access and use the host's MariaDB database?
For example a container like NextCloud using MariaDB of Host DB.
I have been trying to do this without much success.

I found that in Windows we can connect to the host simply using host.docker.internal.
However this does not work for me. Do I simply use the host local IP with Bridge network driver? or Host network driver?
Portainer has these fields for Network settings of a container while deploying -
Annotation 2020-06-15 132109.jpg
Annotation 2020-06-15 132109.jpg (94.84 KiB) Viewed 2754 times
- could you give me examples of what these will be for me to connect to the host database?

Does the host database user require any special permissions?
Thanks
User avatar
Nazar78
Posts: 2002
Joined: Wed Jul 17, 2019 10:21 pm
Location: Singapore
Contact:

Re: Deploy container to use Host database

Post by Nazar78 »

You can connect using two methods, TCP or Unix socket (I prefer this).

TCP:
Find out your host docker IP, use "ipconfig" command look for docker0 or at the Portainer's "Networks" bridge section, e.g. 172.17.0.1 so your client would connect to host's MySQL 172.17.0.1:3306. Issue here is that you'll not be able to connect if the host MySQL is set to bind to local-only 127.0.0.1 unless you change the host's MySQL config bind to 0.0.0.0. You can quickly check if this is local-only binding by using your laptop/PC or even the NAS itself try to connect (mysql client or telnet) on the same sub net i.e. 192.168.x.x:3306, or using command "netstat -natp|grep :3306". If the connection timed out or you see 127.0.0.1:3306 then your host MySQL is only listening to local connections, hence changes to the host's MySQL cnf is needed.

Socket:
Configure the NextCloud container to bind and use your host's MySQL Unix socket. Recreate your NextCloud container, can be done easily from Portainer itself, under Containers->NextCloud->Duplicate/Edit->Volumes->map additional volume. Bind the container's /run/mysqld/mysqld.sock with host's /run/mysqld/mysqld.sock. Make sure you click the "Bind" by default it's "Volume". Reconfirm your host socket path using command "netstat -na|grep mysql" as I remembered it's a bit different last time I use (now I'm using MariaDB in chroot) . Click "Deploy the container" and accept any warning prompts. Then configure NextCloud to use 127.0.0.1:/run/mysqld/mysqld.sock.
AS5304T - 16GB DDR4 - ADM-OS modded on 2GB RAM
Internal:
- 4x10TB Toshiba RAID10 Ext4-Journal=Off
External 5 Bay USB3:
- 4x2TB Seagate modded RAID0 Btrfs-Compression
- 480GB Intel SSD for modded dm-cache (initramfs auto update patch) and Apps

When posting, consider checking the box "Notify me when a reply is posted" to get faster response
Post Reply

Return to “Portainer”