Solved: cant connect to local database using external IP:Port


I have 2 Servers, A and B.
They both run the same replicated server-application (rest-API) written in go. On Server B, I also have a database running. Both server and database in docker-containers.
Inside the Server, I connect to the database like so:
note, that the bbb.bbb.... is the public IP of my Server B.
Now the problem:
Running th server-application on Server A (which doesn’t have the database), works fine. I can also connect to the database from my development machine using this command:
ssh -L 3307: [email protected]
But when using dockerized version on the machine which holds the database, I can’t connect to the database? This only happens, when running inside docker, when I just run the executable like ./goRest on Server B, it also works fine. I am really confused.
In case my explanation was confusing:
OS: Ubuntu 20.04LTS
The error that occurs looks like this:

Best Answer:

I now found a solution.
So the problem was with ufw. I had rules like this:
That’s why the acces from my local development computer (aka. prod.local.ip.notebook) and the access from server A (aka works
The problem was bbb.bbb.bbb.bbb, because the container with the rest-API accessing the database was not allowed in ufw. To allow this IP, I had to get the container’s IP.
So I ran: docker exec -it xyz /bin/sh, then inside the container: ifconfig which returned for eth0.
This is the IP which had to be added to ufw. So my new ufw configuration looks like this:

3307 ALLOW <- remote server 3307 ALLOW prod.local.ip.notebook <- development 3307 ALLOW bbb.bbb.bbb.bbb 3307 ALLOW <- container ip [/code]

If you have better answer, please add a comment about this, thank you!