Docker postgres connection refused when trying to pg_restore - linux

Internal connection between my Spring Boot app and postgres db works fine but when I want to connect to db through init_database.sh and pg_restore I get pg_restore: error: connection to server at "db" (192.168.224.2), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?. In this .sh script I use the same host and port as in application.properties (where connection works fine as I said before).
I simply want to do a pg_restore from init_database.sh in my Docker container.
docker-compose.yml
services:
db:
container_name: db
hostname: db
image: postgres:14-alpine
command: -c 'max_connections=250'
ports:
- "5430:5432"
environment:
POSTGRES_DB: endlessblow_db
POSTGRES_USER: kuba
POSTGRES_PASSWORD: pass
volumes:
- ./init.dump:/init.dump
- ./init_database.sh:/docker-entrypoint-initdb.d/init_database.sh
restart: always
app:
container_name: app
image: 'endlessblow-server:latest'
build: ./
ports:
- "8000:8080"
depends_on:
- db
init_database.sh
#!/bin/sh
pg_restore --no-privileges --no-owner -h db -p 5432 -U kuba -d endlessblow_db init.dump
application.properties
spring.datasource.jdbc-url=jdbc:postgresql://db:5432/endlessblow_db
spring.datasource.url=jdbc:postgresql://db:5432/endlessblow_db
spring.datasource.username=kuba
spring.datasource.password=pass
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.platform=postgres
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
How to solve that? Thank you!

in config of service "app", try following:
app:
...
depends_on:
- db
links:
- db
without links, docker network will isolate containers without explicitly exposing them to one another.

Related

Can't access MongoDB container from NodeJS App

I'm running an instance of a web application in my Docker container and am also running a MongoDB container so when I launch the web app I can easily connect to the DB on the app's connection page.
The issue is that I'm not sure how to reach the Mongo container from my web app and am not sure if my host/port connection info is correct.
My Docker Setup
As you can see the container is up and running with both mongo and web app services running without errors
I build the two through docker-compose.yml
version: "3.3"
services:
web:
image: grafana-asw-v3
container_name: grafana-asw-v3
restart: always
build: .
ports:
- "13000:3000"
volumes:
- grafana-storage:/var/lib/grafana
stdin_open: true
tty: true
db:
container_name: mongo
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- grafana-mongo-db:/var/lib/mongo
ports:
- "27018:27017"
volumes:
grafana-mongo-db: {}
grafana-storage: {}
Issue
With everything up and running I'm attempting to connect through the web app, but I seem to be using the wrong connection info...
I assumed to use "hostMachine:port" (roxane:27018), but it's not connecting. Is there something I overlooked here?
There were two changes I had to make to fix this issue:
Modify the bind_ip in mongod.conf via making this change to my docker-compose file
db:
container_name: mongo
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- grafana-mongo-db:/var/lib/mongo
ports:
- "27018:27017"
command: mongod --bind_ip 0.0.0.0
I needed to refer to the IP address instead of the hostname in the cli in my we application. (Thanks to this answer for help with this one)
Short answer
db service is in the same network than web service not in host network.
As you named your services via container_name you shoud be able to use the connection string mongodb://mongo:27017
Explanation
By default, docker containers run under a bridge network allowing them to communicate without viewing your host network.
When using ports in a compose file, you define that you want to map an internal port of the container to the host port
"27018:27017" => I want to expose the container port number 27017 to the host port number 27018.
As a result, you could expose your web frontend without exposing your mongo service :
version: "3.3"
services:
web:
image: grafana-asw-v3
container_name: grafana-asw-v3
restart: always
build: .
ports:
- "13000:3000"
volumes:
- grafana-storage:/var/lib/grafana
stdin_open: true
tty: true
db:
container_name: mongo
image: mongo
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- grafana-mongo-db:/var/lib/mongo
volumes:
grafana-mongo-db: {}
grafana-storage: {}

how to fix Connection refused [::ffff:127.0.0.1]:port on linux

hi i create project with .net core.my project connect to other project to get services.i read docker-compose and set network for projects.in windows os my project correctly connect to other project and get services.i set ip 127.0.0.1 to connect to other project.in linux i see ip docker container projects like this
docker network inspect my_project_net
and see
"IPv4Address": "172.18.0.5/16",
"IPv6Address": ""
and get this error when my project connect to other projects
Connection refused [::ffff:127.0.0.1]:port // port is 105
how i can cast this port to 127.0.0.1 or run correctly my project
thanks for read my problem
you must set container_name in docker-compose to environment project that connects to another project for example :
version: '3.3'
services:
proj1:
container_name: proj-1
image: ...
ports:
- "5002:5002"
- "8443:8443"
environment:
- ConnectionString=Data Source=127.0.0.1,14330;Initial Catalog=db;
User id=sa;Password=******; // in docker-compose you must use (container_name) mssql insted 127.0.0.1
- ConnectionString=Data Source=mssql,14330;Initial Catalog=db;
User id=sa;Password=******; // true
restart: always
depends_on:
- mssqlservice
restart: always
mssqlservice:
image: 'mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04'
container_name: mssql
environment:
ACCEPT_EULA: Y
MSSQL_SA_PASSWORD: ******
volumes:
- sqlvolume:/var/opt/mssql
expose:
- 1433
ports:
- "14330:1433"
restart: always
volumes:
sqlvolume:
driver: local
name: mssqldata

docker-compose up: FATAL: database "boilerplate," does not exist (Postgres Node)

I am trying to connect a node server to a postgres database with node-postgres (pg). My docker-compose.yaml is below:
version: "3.7"
services:
server:
build:
context: ./node-boilerplate
dockerfile: Dockerfile
ports:
- '3000:3000'
depends_on:
- db
environment:
DB_USER: postgres
DB_PASSWORD: postgres
DB_DATABASE: boilerplate,
DB_HOST: db
db:
image: postgres:latest
container_name: boiler-db
ports:
- '5432:5432'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: boilerplate
When the node app tries to access a database called boilerplate that actually exists, it gives the error:
FATAL: database "boilerplate," does not exist
However, when I connect to the postgres container (using docker exec -it <postgres_container_ID> psql -U postgres) and list databases with \l, it shows the database boilerplate listed.
What am I missing? How come the database shows up but the node server cannot find it?
Thank you in advance.
Try to docker-compose up without comma:
DB_DATABASE: boilerplate

Cant connect to postgres database inside docker container

My problem is I have a script that should scrap data and put it inside postgres database, however it has a problem to reach out postgres container.
When I run my docker-compose here is the result:
Name Command State Ports
------------------------------------------------------------------------------------------
orcsearch_dev-db_1 docker-entrypoint.sh postgres Up 0.0.0.0:5432->5432/tcp
orcsearch_flask_1 gunicorn wsgi:application ... Up 0.0.0.0:80->80/tcp, 8000/tcp
We can clearly see that postgres is on 5432 port.
This is my python script database setting:(ofcourse I removed password for obvious reason)
class App():
settings = {
'db_host': 'db',
'db_user': 'postgres',
'db_pass': '',
'db_db': 'orc',
}
db = None
proxies = None
and this is my docker-compose.yml
version: '2'
services:
flask:
build:
context: ./backend
dockerfile: Dockerfile.dev
volumes:
- ./backend:/app
- pip-cache:/root/.cache
ports:
- "80:80"
links:
- "dev-db:db"
environment:
- DATABASE_URL=postgresql://postgres#db:5432/postgres
stdin_open: true
command: gunicorn wsgi:application -w 1 --bind 0.0.0.0:80 --log-level debug --reload
networks:
app:
aliases:
- flask
dev-db:
image: postgres:9.5
ports:
- "5432:5432"
networks:
app:
aliases:
- dev-db
volumes:
pip-cache:
driver: local
networks:
app:
When going into exec flask bash(inside flask container) and running script command I get this error:
psycopg2.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Obviously there is postgres running on this port and I cant figure out what wrong do I do. Any help would be nice!
Probably you are using DSN instead of URI, and PostgreSQL thinks that "db" is not a host because it's hard to tell if "db" is host or path to socket. To fix it, use URI instead of DSN if you use >=9.2 version of PostgreSQL.
Example of URI:
postgresql://[user[:password]#][netloc][:port][/dbname][?param1=value1&...]
https://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-CONNSTRING
In your App class it should be 'db_host': 'dev-db', Seems like that hostname is exposed, not db.
I think that the problem is related to the fact that you're using the network and the link together. Try remove the link and change the postgres address to dev-db or change the alias to:
networks:
app:
aliases:
- dev-db
- db

NodeJS: Can't connect to postgreSQL container from another container

I have container with a NodeJS application and it has to be linked to another container with a Postgres db. My docker-compose.yml is:
version: "3"
services:
postgres:
image: "postgres:9.5.6-alpine"
container_name: postgres
volumes:
- /shared_folder/postgresql:/var/lib/postgresql
ports:
- "5432:5432"
web:
build: .
container_name: web
ports:
- "3000:3000"
links:
- postgres
depends_on:
- postgres
command: sh /usr/home/freebsd/wait-for-command.sh -c 'nc -z postgres 5432'
command: sh start.sh
On postgres container there is a db named "bucket", owned by user "user" with password "password".
When I type docker-compose up, and try to link the containers, after this message from console:
[INFO] console - postgres://user:password#localhost:5432/bucket
I get this error:
Error: Connection terminated
at [object Object].<anonymous>
in /node_modules/massive/node_modules/deasync/index.js:46
where is the problem?
You define command twice, which means your wait-for-command.sh is being overridden by sh start.sh and it's probably being run before PostgreSQL is ready to accept connections.

Resources