Can't get multiple DB connections working for Docker dev target. Please help?

Hey All,

Hoping to get some help here as i’m a bit stuck.

Ultimately i’m trying to set up dynamic database connections for a multi-tenant architecture (DB per tenant) much like @bpj got working in this post:

My first problem is just setting up a 2nd DB connection in Wappler that would in theory act as the DB that holds the client DB credentials etc.

I’m working in a Node.JS on a Docker target with the standard Docker DB set up.

I can easily add the DBs via direct connection the Wappler DB manager and the connection works:

image

This connection works and i can add/delete tables and fields etc.

When i go to run a server action using a query with this connection:

I get the following error:

{"status":"500","code":"ECONNREFUSED","message":"connect ECONNREFUSED 127.0.0.1:9906","stack":"Error: connect ECONNREFUSED 127.0.0.1:9906\n at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)"}

The actual DB connection details look like:

Things i have tried:

  • Changing ‘localhost’ to the ip address 127.0.0.1
  • Opening and saving the Target settings
  • Closing and redeploying the target and Docker multiple times

Is this an issue that i’m trying to do a Direct Connection to the DB in the docker container?

Would this have been fine if it was a standard remote DB?

How else can i set up multiple DB connections in a docker dev environment to be able to achieve the desired outcome?

Open to any info or ideas :slight_smile:

Where’s this 2nd DB located? Is it inside or outside Docker?

Each Docker container only runs one service. So, the NodeJS container only runs NodeJS, there’s nothing else running at localhost, so you cannot connect to a database running on localhost.

When Wappler creates a NodeJS and Database inside Docker, those are two containers. The NodeJS doesn’t talk to “localhost”, it talks to “db” (which is DNS resolved to the container’s IP address that runs the database)

Are you running this database inside Docker? If yes, they you most likely modified the docker-compose file (e.g.: added a service called “db2”), so you use “db2” instead of localhost.

Are you running this database outside Docker? If yes, this is a more difficult scenario as it depends if you’re running Windows/MacOS/Linux, there are different methods to connect from a container to the host system. If possible, I suggest running everything inside Docker for simplification purposes

You can edit the docker-compose.yml file in your project folder, e.g.:

.wappler/targets/Development/docker-compose.yml

Yes, just for testing purposes, the 2nd DB is located inside the same docker container. It is the the same database connection, i.e. its the same database instance in Docker, but within that instance you can clearly create multiple databases.

And here is that same database instance with multiple DBs created inside it when i connect through Navicat (or indeed Wappler DB manager:

image

So the 2nd database has indeed been created in the same docker container and does work, just not with any server connect action.

I’ll look into what else you’ve said as i’m sure it’s an error on my end and perhaps not how the docker and Wappler are supposed to work in regards to multiple DBs

Ah, I understand it: you have two databases inside the same one database server

I was thinking you had two PostgreSQL containers, but it’s just one.

So, the issue is, you cannot use “localhost”, because on localhost it’s just the NodeJS server running, not the PostgreSQL. The PostgreSQL is in another container, whose name is “db” (or “db_1”)

So you need to use “db” instead of “localhost” (Docker will take care of resolving “db” to the container’s IP address running Postgres)

This may sound a bit confusing:

  1. You can use “localhost” on the Wappler UI, for the database manager, because the Postgres container has a published port 9906 (so you can connect using Navicat to localhost:9906)
  2. You cannot use “localhost” inside the NodeJS app, because in the NodeJS Docker container it’s just NodeJS and nothing else running - NodeJS has to talk to another machine (container): db

You need to think like having one computer running NodeJS, and another computer running PostgreSQL. On NodeJS computer you cannot use localhost for accessing PostgreSQL - it’s not running there

You are best setting a second DB up remotely if you can. As @Apple suggests trying to connect to 2 localhost DBs can cause problems.

The solution I came up with on the attached post works fine and I continue to use it. There is no dynamic picking in the Wappler UI so you do have to edit the Global DB connection file in code view to add the {{hostname}}, {{dbpassword}} etc. values.

One thing that I have done, is set the static ‘Direct’ DB connection for DB Manager to a fixed address of one of the DBs. This allows creating queries and working with Wappler without having to have separate Test and Dynamic connections under Globals.

Just to clarify, there’s no problem using 2 localhost DBs :slight_smile: It’s just an issue of understanding how Docker container works and their network specificities (why using “localhost” doesn’t work, but “db” works)

On Wappler database manager it’s not possible to use “db” - because that only exists within a Docker container, and Wappler database manager is not running inside a Docker container. In that case, “localhost” needs to be used instead

Agreed, sorry I meant that it is simpler to use an external one because you can have obstacles (that can be overcome) when using 2 local

1 Like

Great, thanks for this…I had started to think this was the case but I think you’ve clarified it for me and helped make a bit of sense out of it.

One more thing though, because your understanding is correct in how I’ve got it set up, where in Wappler should I be editing the config details to connect to host db?

Presumably not in the db manager ui?

There are a number of other files that have the details, sucha as the db connection files or in server connect db connection settings etc?

Yes, Wappler organizes files a bit weirdly. When you change the DB details through “Database Direct Connect” in the Wappler database manager, it’s not changing for the NodeJS app

There’s a .json file for the Wappler database manager, and there’s another .json file for the NodeJS app. These are the ones for NodeJS:

app/modules/connections/

I don’t remember if it’s possible to change through the UI - I think so, but there you have the JSON you have to edit

1 Like

Just right click and choose Open in Code View

1 Like

Great…will look into to it tomorrow morning (bed time for me now :slight_smile: ). Should be able to get it working.

Thanks again @bpj and @Apple

Ok… If anyone comes back to read this, this is how i was able to get it work easily using just the Wappler UI.

Step 1.

Using the Database Manager, create new connection.

Enter the relevant database name and user credentials etc

KEY = Make sure that the host is set to db and the port is set to 5432

DB and port 5432 may change based on the docker settings but this is what Wappler would create by default.

This will create a new DB connection but it WONT actually be able to connect to the DB, it will however create the two JSON files with relevant DB connection info.

Step 2

Once the above has saved, open the connection details and change the host to localhost and the port to 9906 (again, these are just the default details, yours may vary)

Hit save and this will then only update one of the JSON files, the one with the DB connection details for DB manager.

The JSON file used for server connection actions will still have the connection details with host db and port 5432

Hope this helps someone else