I am trying to configure postgres to run with springboot and metabase. Each service is running separately alone but when I try to put the 3 together in a docker-compose file, I am getting the following error :
Caused by: org.postgresql.util.PSQLException: Connection to 0.0.0.0:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
metabase-container | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303)
However, I have mapped the port 5432 of db to the port 5432 of the metabase container.
And yet, It doesn't seem to work. Any help on this issue? (please find my docker-compose file below)
version: '2'
services:
spring:
image: 'realtime:latest'
container_name: spring
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/compose-postgres
- SPRING_DATASOURCE_USERNAME=compose-postgres
- SPRING_DATASOURCE_PASSWORD=same
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
volumes:
- /home/vagrant/valorisation-2.0:/app
command: ["java", "-jar", "rtv-1.jar"]
mem_limit: 10g
mem_reservation: 10g
ports:
- "8079:8080"
db:
image: 'postgres:13.1-alpine'
container_name: db
environment:
- POSTGRES_USER=compose-postgres
- POSTGRES_PASSWORD=********
- METABASE_PASSWORD=same
ports:
- "8078:5432"
- "54320:5432"
metabase:
container_name: metabase-container
restart: "always"
image: metabase/metabase
ports:
- "3000:3000"
- "5432:5432"
environment:
- MB_DB_TYPE=postgres
- MB_DB_DBNAME=db
- MB_DB_PORT=5432
- MB_DB_USER=compose-postgres
- MB_DB_PASS=same
- MB_DB_HOST=0.0.0.0
- MB_ENCRYPTION_SECRET_KEY=********
Try changing MB_DB_HOST in the metabase environmental variables from 0.0.0.0 to db.
The subtilty I hadn't understood is that all the services are in the same network. Hence, deleting port forwarding (which exposes the services to outside components but is irrelevant for communication within the container) between the services and changing the Metabase configuration file does the job. Here is the modified docker-compose.yml file :
version: '2'
services:
spring:
image: 'realtime:latest'
container_name: spring
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/compose-postgres
- SPRING_DATASOURCE_USERNAME=********
- SPRING_DATASOURCE_PASSWORD=**********
- SPRING_JPA_HIBERNATE_DDL_AUTO=update
volumes:
- /home/vagrant/valorisation-2.0:/app
command: ["java", "-jar", "rtv-1.jar"]
mem_limit: 10g
mem_reservation: 10g
ports:
- "8079:8080"
db:
image: 'postgres:13.1-alpine'
container_name: db
environment:
- POSTGRES_USER=compose-postgres
- POSTGRES_PASSWORD=compose-postgres
- METABASE_PASSWORD=compose-postgres
ports:
- "8078:5432"
metabase:
container_name: metabase-container
depends_on:
- db
restart: "always"
image: metabase/metabase
ports:
- "3000:3000"
environment:
- MB_DB_TYPE=postgres
- MB_DB_DBNAME=********
- MB_DB_PORT=5432
- MB_DB_USER=************
- MB_DB_PASS=compose-postgres
- MB_DB_HOST=db
- MB_ENCRYPTION_SECRET_KEY=***********
Related
I am trying to set up SonarQube self-hosted using docker with docker-compose. To use SonarQube via HTTPS I am using nginx as reverse proxy and the jrcs/letsencrypt-nginx-proxy-companion to handle the SSL-Certificate.
Now I have configured SonarQube using the following docker-compose.yml:
version: '3'
networks:
ext:
int:
volumes:
certs:
vhosts:
html:
postgresql_data:
postgresql:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
services:
nginxproxy:
image: jwilder/nginx-proxy:alpine
container_name: nginxproxy
hostname: nginxproxy
restart:
unless-stopped
networks:
- ext
volumes:
- certs:/etc/nginx/certs:ro
- vhosts:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./config/my_proxy.conf:/etc/nginx/conf.d/myproxy.conf:ro
ports:
- 80:80
- 443:443
labels:
- com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy
environment:
- SSL_POLICY=Mozilla-Intermediate
nginxproxy_comp:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: nginxproxy_comp
hostname: nginxproxy_comp
restart:
unless-stopped
depends_on:
- nginxproxy
networks:
- ext
volumes:
- certs:/etc/nginx/certs
- vhosts:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- /var/run/docker.sock:/var/run/docker.sock:ro
sonarqube:
image: sonarqube:community
container_name: sonarqube
hostname: sonarqube
depends_on:
- db
restart:
unless-stopped
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_logs:/opt/sonarqube/logs
- sonarqube_extensions:/opt/sonarqube/extensions
environment:
- SONAR_JDBC_URL=jdbc:postgresql://localhost:5432/sonarqube
- SONAR_JDBC_USER=sonarqube
- SONAR_JDBC_PASSWORD=sonarqube
networks:
- int
ports:
- 9000:9000
db:
image: postgres:12
container_name: db
hostname: db
networks:
- int
ports:
- 5432:5432
environment:
- POSTGRES_USER=sonarqube
- POSTGRES_PASSWORD=sonarqube
volumes:
- postgresql:/var/lib/postgresql
- postgresql_data:/var/lib/postgresql/data
restart:
unless-stopped
Now when I try to start docker-compose, SonarQube is giving me the following Error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader#277050dc-org.sonar.db.DefaultDatabase': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Fail to connect to database
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Can anyone please tell me what i did wrong?
What I see here is the incorrect JDBC URL. It must below
SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonarqube
Since you have network between containers DB accessible via db
Also, under SonarQube container add below as well
privileged: true
Check one of my sample project in GitHub
https://github.com/shamith234/sonarqube-9.5-postgres-docker/blob/master/docker-compose.yml
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.
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.
I want to make a developer offline space to develop my database with hasura
I know the existence of the container with the tag cli-migrations, but the command:
hasura-cli console
doesn't work for accessing outside of the container.
My configuration for the docker-compose.yml is:
version: '3'
services:
hasura:
environment:
- HASURA_GRAPHQL_DATABASE_URL=postgres://[some pass]:[some user]#db:5432/[some db]
- HASURA_GRAPHQL_ENABLE_CONSOLE=false
image: hasura/graphql-engine:v1.0.0-rc.1.cli-migrations
container_name: hasura
volumes:
- ./hasura-migrations:/hasura-migrations
networks:
- hasura-db
ports:
- "8081:8080"
- "8082:8081"
restart: always
command: hasura-cli console --console-port 8081 --no-browser
db:
environment:
- POSTGRES_USER=[some user]
- POSTGRES_PASSWORD=[some pass]
- POSTGRES_DB=[some db]
image: postgres:11.4-alpine
container_name: db
restart: always
networks:
- hasura-db
networks:
hasura-db:
There is a Pull request in the hasura graphql project for this issue, but is not merged.
I'm looking for a workaround for this pull request now.
I found a workaround for this!
If I install hasura cli in my machine and use
hasura console --console-port 8080 --endpoint http://127.0.0.1:8081
I can connect to the hasura api and run a console locally.
this is my updated docker-compose.yml
version: '3'
services:
hasura:
environment:
- HASURA_GRAPHQL_DATABASE_URL=postgres://[some pass]:[some user]#db:5432/[some db]
- HASURA_GRAPHQL_ENABLE_CONSOLE=false
image: hasura/graphql-engine:v1.0.0-rc.1.cli-migrations
container_name: hasura
volumes:
- ./hasura-migrations:/hasura-migrations
networks:
- hasura-db
ports:
- "8081:8080"
restart: always
db:
environment:
- POSTGRES_USER=[some user]
- POSTGRES_PASSWORD=[some pass]
- POSTGRES_DB=[some db]
image: postgres:11.4-alpine
container_name: db
restart: always
networks:
- hasura-db
networks:
hasura-db:
Using docker to connect springboot to postgres via docker-compose. Using port 5432 on postgres works fine, if i try an port other than that it fails
working code
spring
spring.datasource.url=jdbc:postgresql://db:5432/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
docker-compose
version: '2.1'
services:
db:
container_name: db
image: postgres:9.4
ports:
- 5432:5432
volumes:
- /tmp:/var/lib/postgresql
environment:
- POSTGRES_USER=wwc
- POSTGRES_DB=wwc
- POSTGRES_PASSWORD=test
server:
container_name: spring-boot-rest-server
build:
context: .
dockerfile: Dockerfile.server
ports:
- 8080:8080
logging:
driver: json-file
depends_on:
- db
web:
container_name: nginx-web
links:
- "server:springboot"
build:
context: .
dockerfile: Dockerfile.web
ports:
- 80:80
- 8088:8088
logging:
driver: json-file
depends_on:
- server
**connection refused code **
spring
spring.datasource.url=jdbc:postgresql://db:6000/wwc
spring.datasource.username=wwc
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
docker-compose
version: '2.1'
services:
db:
container_name: db
image: postgres:9.4
ports:
- 6000:5432
volumes:
- /tmp:/var/lib/postgresql
environment:
- POSTGRES_USER=wwc
- POSTGRES_DB=wwc
- POSTGRES_PASSWORD=test
server:
container_name: spring-boot-rest-server
build:
context: .
dockerfile: Dockerfile.server
ports:
- 8080:8080
logging:
driver: json-file
depends_on:
- db
web:
container_name: nginx-web
links:
- "server:springboot"
build:
context: .
dockerfile: Dockerfile.web
ports:
- 80:80
- 8088:8088
logging:
driver: json-file
depends_on:
- server
error:
spring-boot-rest-server | org.postgresql.util.PSQLException: Connection to db:6000 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
What am i doing wrong?
You are confusing a bit the ports: your "db" container only exports 1 port: 5432. The 6000 that you put in your docker-compose is the port on localhost that you map to that container (db) on that port (5432).
You shouldn't even use the port mappings for the postgres container unless you want to connect from localhost which I guess you don't.
If you want to use another port than 5432 you need to extend the postgres Dockerfile and change the configuration so that postgres starts listening on a different port.
Hope this helps.
In other words: The port mapping configured in docker-compose has no relevancy to how the containers connect to each other. The mapping is only relevant when something/someone attempts to connect to your containers within the docker-compose from the outside. (Like from the localhost, as #Mihai remarked.)