can't connect to local postgres from docker container - postgresql

I have some docker containers such as php, nginx, etc. A also have postgres locally because I have learned that dabatase inside docker container is a bad practise. But I can't connect to local postgres from docker container.
At this moment I have done next
In postgresql.conf I changed listen_addresses
listen_addresses = '*'
In pg_hba.conf I have added next line
host all all 0.0.0.0/0 md5
The I executed next command for iptables
iptables -I INPUT -p tcp -m tcp -s 0.0.0.0 --dport 5432 -j ACCEPT
Then I restarted postgres.
My database configuration
DB_CONNECTION=pgsql
DB_HOST=my_server_ip_address
DB_PORT=5432
DB_DATABASE=mydbname
DB_USERNAME=mydbuser
DB_PASSWORD=mydbpasswd
But i still can't connect to posgresql. At the same moment I can connect to postgres via psql or phpstorm
My docker-compose.yml
version: '3.7'
networks:
backend-network:
driver: bridge
frontend-network:
driver: bridge
services:
&app-service app: &app-service-template
container_name: k4fntr_app
build:
context: ./docker/php-fpm
args:
UID: ${UID?Use your user ID}
GID: ${GID?Use your group ID}
USER: ${USER?Use your user name}
user: "${UID}:${GID}"
hostname: *app-service
volumes:
- /etc/passwd/:/etc/passwd:ro
- /etc/group/:/etc/group:ro
- ./:/var/www/k4fntr
environment:
APP_ENV: "${APP_ENV}"
CONTAINER_ROLE: app
FPM_PORT: &php-fpm-port 9000
FPM_USER: "${UID:-1000}"
FPM_GROUP: "${GID:-1000}"
networks:
- backend-network
&queue-service queue:
<<: *app-service-template
container_name: k4fntr_queue
restart: always
hostname: *queue-service
depends_on:
- app
environment:
CONTAINER_ROLE: queue
&schedule-service schedule:
<<: *app-service-template
container_name: k4fntr_schedule
restart: always
hostname: *schedule-service
depends_on:
- app
environment:
CONTAINER_ROLE: scheduler
&sportlevel-listener sportlevel_listener:
<<: *app-service-template
container_name: k4fntr_sl_listener
restart: always
hostname: *sportlevel-listener
ports:
- "${SPORTLEVEL_LISTEN_PORT}:${SPORTLEVEL_LISTEN_PORT}"
depends_on:
- app
environment:
CONTAINER_ROLE: sl_listener
&php-fpm-service php-fpm:
<<: *app-service-template
container_name: k4fntr_php-fpm
user: 'root:root'
restart: always
hostname: *php-fpm-service
ports: [*php-fpm-port]
entrypoint: /fpm-entrypoint.sh
command: php-fpm --nodaemonize
networks:
- backend-network
- frontend-network
echo-server:
container_name: k4fntr_echo
image: oanhnn/laravel-echo-server
volumes:
- ./:/app
environment:
GENERATE_CONFIG: "false"
depends_on:
- app
ports:
- "6001:6001"
networks:
- backend-network
- frontend-network
nginx:
container_name: k4fntr_nginx
image: nginx
volumes:
- ./docker/nginx/config:/etc/nginx/conf.d
- ./:/var/www/k4fntr
depends_on:
- *php-fpm-service
ports:
- "${NGINX_LISTEN_PORT}:80"
networks:
- frontend-network
redis:
container_name: k4fntr_redis
image: redis
restart: always
command: redis-server
volumes:
- ./docker/redis/config/redis.conf:/usr/local/etc/redis/redis.conf
- ./docker/redis/redis-data:/data:rw
ports:
- "16379:6379"
networks:
- backend-network

Related

Password auth failed for user "postgres"

I am trying to run my postgres server and nestjs project with docker script and it does fire up the server and database. While firing up it does run migrations too but when I open pgAdmin I see no database there and if i try to create new server i get fatal pasword incorrect error. Also my server crashes too with error saying Password authentication failed for user "postgres". It was running fine yesterday but today its not running at all. I tried pruning everything and made fresh build and then compose up but nothing. Here is docker script
version: "3.5"
services:
dev-api:
container_name: xxxxxxxx-api
build:
context: .
dockerfile: Dockerfile
depends_on:
- dev-db
environment:
DATABASE_URL: postgresql://postgres:postgres#dev-db:5432/xxxxxxx_api
APP_ENV: development
PORT: 3030
WAIT_HOSTS: dev-db:5432
ports:
- "3030:3030"
- "9229:9229"
volumes:
- .:/usr/api/
dev-db:
container_name: xxxxxxxx-postgres
image: postgres:13.5-alpine
restart: always
ports:
- "5432:5432"
volumes:
- ./pg-data:/var/lib/postgresql/data
- ./src/db/docker/init.sql:/docker-entrypoint-initdb.d/dbinit.sql
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=xxxxxxx_api
expose:
- "5432"
pgadmin:
container_name: xxxxxxx-pgadmin
image: dpage/pgadmin4:6.2
ports:
- 8080:80
volumes:
- pgadmin-data:/var/lib/pgadmin
environment:
- PGADMIN_DEFAULT_EMAIL=user#postgres.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=80
depends_on:
- dev-db
volumes:
pgadmin-data:

Can't connect containers mariadb and phpmyadmin

I get the error "mysqli::real_connect(): (HY000/2002): No such file or directory" when trying to login to phpmyadmin. I verified I can connect to the DB container from the localhost using mysql -h 127.0.0.1 -P 3306 -u root -p. Below is my docker-compose file:
version: "3.7"
########################### SECRETS
secrets:
mysql_root_password:
file: $DOCKERDIR/secrets/mysql_root_password
########################### SERVICES
services:
# Portainer - WebUI for Containers
portainer:
container_name: portainer
image: portainer/portainer-ce:latest
restart: unless-stopped
command: -H unix:///var/run/docker.sock
security_opt:
- no-new-privileges:true
ports:
- "$PORTAINER_PORT:9000"
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- $DOCKERDIR/portainer/data:/data
environment:
- TZ=$TZ
# MariaDB - MySQL Database
db:
container_name: db
image: linuxserver/mariadb:latest
restart: always
security_opt:
- no-new-privileges:true
ports:
- "$MARIADB_PORT:3306"
volumes:
- $DOCKERDIR/mariadb/data:/config
environment:
- PUID=$PUID
- PGID=$PGID
- TZ=$TZ
- FILE__MYSQL_ROOT_PASSWORD=/run/secrets/mysql_root_password
secrets:
- mysql_root_password
# phpMyAdmin - Database management
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
container_name: phpmyadmin
restart: unless-stopped
depends_on:
- db
security_opt:
- no-new-privileges:true
ports:
- "$PHPMYADMIN_PORT:80"
volumes:
- $DOCKERDIR/phpmyadmin:/etc/phpmyadmin
environment:
- PMA_HOST=db
#- PMA_ARBITRARY=1
- MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql_root_password
secrets:
- mysql_root_password
# Dozzle - Real-time Docker Log Viewer
dozzle:
image: amir20/dozzle:latest
container_name: dozzle
restart: unless-stopped
security_opt:
- no-new-privileges:true
ports:
- "$DOZZLE_PORT:8080"
environment:
DOZZLE_LEVEL: info
DOZZLE_TAILSIZE: 300
DOZZLE_FILTER: "status=running"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
For the life of me, I can't figure out what I am doing wrong to log into Phpmyadmin. Can someone explain my mistake or mistakes and point me in the right direction? Thanks
I figured the issue out, first was I had the network set on the pphpmyadmin section, and not db, once I added the network statement to db section, I was able to connect.

Docker-compose postgres error: FATAL: password authentication failed for user "postgres"

I have an error with postgres in docker-compose. When I start docker-compose with docker-compose up -d, everything is right. But after 1-2 days, the error occurs. And I noticed, that when it occurs the linux machine become really slow, and I can't do any operations with docker. I tried: sudo systemctl restart docker.service to restart docker daemon, but it just stops and do nothing. But when I reboot machine, the lags disappear. I consider that something wrong with config(docker-compose.yml).
Here's my docker-compose.yml:
volumes:
common-volume:
postgres:
services:
nginx:
container_name: nginx
image: nginx:latest
ports:
- '80:80'
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
networks:
app_net:
ipv4_address: 10.5.1.5
postgres:
container_name: docker-postgres
ports:
- ${POSTGRES_PORT:-5432}:${POSTGRES_PORT:-5432}
restart: always
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USERNAME}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- PGDATA=/var/lib/postgresql/data
image: postgres
networks:
app_net:
ipv4_address: 10.5.0.8
volumes:
- postgres:/var/lib/postgresql/data/
common:
container_name: docker-common
build: ./common
volumes:
- common-volume:/usr/src/common
auth_service:
container_name: docker-auth-service
build: ./auth-service
restart: always
environment:
- DB_HOST=postgres
- DB_USERNAME=postgres
- DB_NAME=postgres
- DB_PASSWORD=postgres
- NODE_ENV=production
- REDIS_HOST=10.5.0.7
env_file:
- .env
volumes:
- common-volume:/usr/src/common
expose:
- "8080"
depends_on:
- postgres
links:
- postgres
networks:
app_net:
ipv4_address: 10.5.0.5
redis:
container_name: redis
image: 'redis:4-alpine'
expose:
- "6379"
networks:
app_net:
ipv4_address: 10.5.0.7
networks:
app_net:
ipam:
config:
- subnet: 10.5.0.0/16
I had confronted similar issue. The problem is with this line of code for both postgresql and auth_service configuration:
restart: always
replace it with:
restart: no
or comment it out!

Problem connecting to postgresql hosted in docker

I have below error connecting to postgresql database hosted in docker.
myapp_Container | WARNING - ConnectionBus: Database connections: 0 active, 0 idle.
myapp_Container | ERROR - ConnectionBus: Opening JDBC connection to Some(db:54325) failed with SQLState: 08001 Error code: 0 Message: The connection attempt failed., giving up...(4/4)
myapp_Container | WARNING - ConnectionBus: Cannot create database connection.
I have two docker-compose files as below
APP Docker-Compose file
version: "3"
services:
app:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
network_mode: postgres_net
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd#db:54325/mydb
external_links:
- db:localpostgres_1
DB Docker-Compose file
version: "3"
services:
postgres:
image: postgres
container_name: localpostgres_1
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
- TZ=Europe/Amsterdam
ports:
- 54325:5432
networks:
- postgres_net
networks:
postgres_net:
external: true
I created postgres_net via docker network create -d bridge postgres_net
Why am I doing this? I want to create apps with same docker-compose structure without creating a postgresql db for each, if I put them together like below it will work.
version: "3"
services:
app:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd#db/mydb
links:
- db
db:
image: postgres
container_name: postgres_x
ports:
- "54320:5432"
environment:
- TZ=Europe/Amsterdam
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
restart: always
I can connect to the Db via pgadmin, but can't understand why myapp cannot connect, please help
pg_hba.conf
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all all md5
You have not properly defined network in First compose file(App compose). You have specified postgres_net as network_mode: postgres_net which is invalid. You need to specify it properly like in the second compose file.
Corrected compose file will be:
APP Docker-Compose
version: "3"
services:
app:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd#db:54325/mydb
networks: # declare network here
- postgres_net
networks: #define network
postgres_net:
external: true
DB Docker-Compose
version: "3"
services:
postgres:
image: postgres
container_name: localpostgres_1
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
- TZ=Europe/Amsterdam
ports:
- 54325:5432
networks:
- postgres_net
networks:
postgres_net:
external: true
this config worked perfectly
app docker-compose file
version: "3"
services:
mendix:
image: myapp
container_name: myapp_Container
ports:
- "8081:8081"
network_mode: postgres_net
environment:
- ADMIN_PASSWORD=Password1!
- DATABASE_ENDPOINT=postgres://user:passwd#localpostgres_1/myapp
external_links:
- localpostgres_1
networks:
postgres_net:
external: true
db docker-compose
version: "3"
services:
postgres:
image: postgres
container_name: localpostgres_1
restart: always
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=passwd
- TZ=Europe/Amsterdam
ports:
- 54325:5432
networks:
- postgres_net
networks:
postgres_net:
external: true
as probably people may know, one may run the docker-compose files in a different folder per application, because docker-compose cares about the path and overwrites the last container you created in a given folder.

Access fusionAuth-app UI from outside container (external access)?

I am using the following docker-compose.yml for deployment cloned from following https://fusionauth.io/docs/v1/tech/installation-guide/docker
version: '3'
services:
db:
image: postgres:9.6
environment:
PGDATA: /var/lib/postgresql/data/pgdata
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
# Un-comment to access the db service directly
ports:
- 5432:5432
networks:
- db
restart: unless-stopped
volumes:
- db_data:/var/lib/postgresql/data
search:
image: docker.elastic.co/elasticsearch/elasticsearch:6.3.1
environment:
- cluster.name=fusionauth
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=${ES_JAVA_OPTS}"
# Un-comment to access the search service directly
ports:
- 9200:9200
- 9300:9300
networks:
- search
restart: unless-stopped
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- es_data:/usr/share/elasticsearch/data
fusionauth:
image: fusionauth/fusionauth-app:latest
depends_on:
- db
- search
environment:
DATABASE_URL: jdbc:postgresql://db:5432/fusionauth
DATABASE_ROOT_USER: ${POSTGRES_USER}
DATABASE_ROOT_PASSWORD: ${POSTGRES_PASSWORD}
DATABASE_USER: ${DATABASE_USER}
DATABASE_PASSWORD: ${DATABASE_PASSWORD}
FUSIONAUTH_MEMORY: ${FUSIONAUTH_MEMORY}
FUSIONAUTH_SEARCH_SERVERS: http://search:9200
FUSIONAUTH_URL: http://fusionauth:9011
networks:
- db
- search
restart: unless-stopped
ports:
- 9011:9011
volumes:
- fa_config:/usr/local/fusionauth/config
networks:
db:
driver: bridge
search:
driver: bridge
volumes:
db_data:
es_data:
fa_config:
I am unable to access the fusionAuth UI screen from http://localhost:9011 or http://fusionauth:9011
How can I access the UI welcome screen from outside docker container? Is there any
enableExternal: true env variable available for docker-compose.yml?
Is there a way to test or ping the fusionAuth App server to make sure it's up and running other than using docker ps -a
This was due to an error with the docker image. An updated docker image was released hours after this question was asked, and that resolved the issue.
More details here: https://github.com/FusionAuth/fusionauth-containers/issues/47