-1

I have a Ubuntu server and I have Mariadb installed locally as I plan to run multiple instances which will need its own separate db and running 30 or so different database containers seem kinda useless.

However, when I do so, My containers can't find 'localhost' as the database hostname. I also tried entering my local IP (192.168.0.xxx) with no luck Do things change when I try to input db details to docker container? What do I need to do to make it work?

update: I created my docker container by pasting the docker compose templates then editing the necessary details like ports, volumes etc. I do this through portainer.

I haven't done anything to connect the container host with the server host as I don't know how to and probably the main reason this isn't working.

update 2: tried method mentioned by Hans Kilian
Here's a temporary Wordpress dockercompose file I created Here's a temporary Wordpress dockercompose file I created

and this is my Wordpress database details: enter image description here

It still refuses connection

RitZz
  • 13
  • 6
  • As you haven't shared how you've configured your containers, it's impossible to provide help. I would assume that your database container is not configured to listen on localhost? – Nico Haase Dec 16 '22 at 10:48
  • 1
    I tried searching for "*docker access host*" and found so many answers here already, did you try those answers? https://stackoverflow.com/questions/31324981/how-to-access-host-port-from-docker-container, https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach, https://stackoverflow.com/questions/70006997/access-host-from-within-a-docker-container, https://stackoverflow.com/questions/72140321/access-host-database-from-service-located-in-a-docker-container ... – Don't Panic Dec 16 '22 at 11:15
  • Sorry I have updated some details so it might be helpful. – RitZz Dec 16 '22 at 11:46

1 Answers1

2

To access services on the host, you need to add the gateway (the gateway is the host machine) as a known host. You do that by adding --add-host=host.docker.internal:host-gateway to your docker run command, like this

docker run -d --add-host=host.docker.internal:host-gateway <myimage>

Then, from inside the container, you can access the host by using the hostname host.docker.internal instead of localhost.

If you use docker-compose, you can add the hostname by adding

extra_hosts:
  - host.docker.internal:host-gateway

to the service definition instead.

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • So If I understand you correctly. I have a docker container named nextcloud so I should do docker run -d --add-host=host.docker.internal:host-gateway nextcloud? Sorry I mostly use dockercompose file and portainer to create my instances so not familiar with docker run – RitZz Dec 16 '22 at 11:11
  • Yes. And change the hostname of the database from `localhost` to `host.docker.internal` in the container – Hans Kilian Dec 16 '22 at 11:12
  • What if I have the instance running already? docker run doesn't work on existing containers... – RitZz Dec 16 '22 at 11:15
  • That's true. You have to stop the container first. btw., I updated the answer to include the docker-compose way of adding the host – Hans Kilian Dec 16 '22 at 11:16
  • hmm, just tried it out. It still says connection refused. :/ – RitZz Dec 16 '22 at 11:27
  • updated the main post with detailed images. – RitZz Dec 16 '22 at 11:50