Hi i have postgres into a container, when i use in mi local pc, it works.
In mi PC i have this (is the backend that communicates with the db):
const {Pool} = require("pg");
const pool = new Pool({
user: "postgres",
password: 12345",
host: "localhost",
database: "attendancebd",
port: "2345"
});
module.exports = pool;
and i run my postgres container with it:
docker run -p 2345:5432 -d --name mypostgrescontainer -e POSTGRES_PASSWORD=12345 -e POSTGRES_DB=attendancebd postgres
Later i put mi sql tables and inserts with this:
docker exec -i mypostgrescontainer psql -U postgres attendancebd < D:\MyRoute\database.sql
and this works fine, when i make a request from my client to the backend that is listening in port 2000, the backend make a query with the db normally
So, i tried to do the same but with docker containers, i have one with the client, another with the backend (express) and the postgres container, when i visit the localhost:3000 that is for the client i can see the interface and when i see the state of the backend is listening, also postgres container is running, but when i try to make a operation between the client and the backend, this just send me this:
Error: connect ECONNREFUSED 127.0.0.1:2345
i try to connect the containers into a docker network but it didnt work either.
CodePudding user response:
First of all, do not publish (-p 2345:5432
) the database's port, unless you want the database to be publicly accessible on the host's network interface. When publishing a port, a port-mapping rule is created, exposing the container's port on the host. This may be "ok" for development purposes on your local machine, but may not be desirable if your host is accessible from a (public) network.
If you want your backend container to connect with the database container, you can use a container network and connect both containers to that network. For example;
Create a network named mynetwork
:
docker network create mynetwork
Then, when creating the containers, set the --network=mynetwork
option, for example:
docker run -d --name mypostgrescontainer --network=mynetwork -e POSTGRES_PASSWORD=12345 -e POSTGRES_DB=attendancebd postgres
Now, when your backend container is also connected to the same network, it can connect with the database container using that container's name (mypostgrescontainer
) as hostname and the port-number that's used by the database (5432
in case of postgres)
docker run -it --rm --network=mynetwork postgres psql -h mypostgrescontainer -U postgres -l
Password for user postgres:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-------------- ---------- ---------- ------------ ------------ -----------------------
attendancebd | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres
| | | | | postgres=CTc/postgres
(4 rows)