I setup a prisma project recently and here is my docker-compose.yml file
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.31
restart: always
ports:
- '4030:4466'
environment:
TZ: ${PRISMA_DB_TIME_ZONE}
PRISMA_CONFIG: |
port: 4466
# managementApiSecret: my-secret
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: ${PRISMA_DB_PASSWORD}
migrations: true
rawAccess: true
postgres:
image: postgres:10.3
restart: always
ports:
- "3306:3306"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
prisma:
postgres:
I can open my prisma playground and it functions without any issue. but I can't create a direct connection to the postgre container with dbeaver.
dbeaver Error message
Connection reset
Why my connection to the database fails?
This photo will be helpful.
postgres by default listens on port 5432.
In your postgres container specification, you should expose the port 5432 not 3306.
version: '3'
services:
prisma:
image: prismagraphql/prisma:1.31
restart: always
ports:
- '4030:4466'
environment:
TZ: ${PRISMA_DB_TIME_ZONE}
PRISMA_CONFIG: |
port: 4466
# managementApiSecret: my-secret
databases:
default:
connector: postgres
host: postgres
port: 5432
user: prisma
password: ${PRISMA_DB_PASSWORD}
migrations: true
rawAccess: true
postgres:
image: postgres:10.3
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
volumes:
prisma:
postgres:
If 5432 port in your host is already in use and if you want to use 3306 instead , then you can do port forwarding like below:
postgres:
image: postgres:10.3
restart: always
ports:
- "3306:5432"
environment:
POSTGRES_USER: prisma
POSTGRES_PASSWORD: ${PRISMA_DB_PASSWORD}
TZ: ${PRISMA_DB_TIME_ZONE}
volumes:
- postgres:/var/lib/postgresql/data
UPDATE - 1
Reason why prisma can access postgres
ports section is meant only to make our services accessible in host level. But in the container level, If a port is open in a container, any other running container can access that port with the help of networks or links section.
By default, docker-compose will create a network per docker-compose.yml file and joins all the services in that file into that network.
That's the reason we could use the <service name> as the hostname and compose will resolve that name to the ip-address of the respective (in your case postgres) container.
Related
I'm using bitnami's Keycloak v.20.0.0 (Quarkus) image with docker-compose. Everything works fine and I have no problems with any configuration, however when I want to see the tables in the PostgreSQL database, I access without problems but I don't see anything! I don't see that any table or database exists.
I understand that I have to start Keycloak in dev mode which I configured but I still don't see anything.
What am I doing wrong?
this is my setup:
version: "3.7"
services:
keycloak:
image: bitnami/keycloak:20.0.1
container_name: keycloak_20
environment:
DB_VENDOR: POSTGRES
DB_ADDR: postgres
KEYCLOAK_ADMIN_USER: admin
KEYCLOAK_ADMIN_PASSWORD: admin
KEYCLOAK_DATABASE_HOST: postgres
KEYCLOAK_DATABASE_PORT: 5432
KEYCLOAK_DATABASE_NAME: postgres
KEYCLOAK_DATABASE_USER: postgres
KEYCLOAK_DATABASE_PASSWORD: postgres
KEYCLOAK_DATABASE_SCHEMA: public
KEYCLOAK_EXTRA_ARGS: "-Dkeycloak.profile.feature.scripts=enabled"
KC_HOSTNAME: postgres
ENV KC_HOSTNAME_STRICT: false
ENV KC_HTTP_ENABLED: true
ports:
- 8080:8080
volumes:
- ./keycloak/export:/tmp/export
- ./rus-theme:/opt/bitnami/keycloak/themes/my-theme
- ./keycloak/configuration/standalone-ha.xml:/bitnami/keycloak/configuration/standalone-ha.xml:ro
command:
- /bin/bash
- -c
- |
/opt/bitnami/keycloak/bin/kc.sh start-dev
depends_on:
- postgres
postgres:
image: postgres:10
container_name: postgres
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
mailhog:
# Conectarse al nombre del container para acceder
# Ejemplo: mailhog:1025
image: mailhog/mailhog
logging:
driver: 'none' # disable saving logs
container_name: mailhog
ports:
- 1025:1025 # smtp server
- 8025:8025 # web ui
volumes:
postgres_data:
driver: local
KEYCLOAK_DATABASE_* properties were used in the old versions of Keycloak (pre-Quarkus).
New properties are defined as KC_DB_* (see https://www.keycloak.org/server/all-config?q=db)
I tried implementing Postgres with Odoo in Azure through docker compose, but I faced this error when I tried to check Odoo's container logs :
Database connection failure: connection to server at "db" (127.0.0.1),
port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections ?
Here's my docker-compose.yml
version: '3.2'
services:
db:
image: registryodoo.azurecr.io/samples/postgres:13
volumes:
- db:/var/lib/postgresql/data/pgdata
deploy:
restart_policy:
condition: always
expose:
- 5432
environment:
POSTGRES_PASSWORD: odoo
POSTGRES_DB: postgres
POSTGRES_USER: odoo
PGDATA: /var/lib/postgresql/data/pgdata
odoocontainer:
depends_on:
- db
image: registryodoo.azurecr.io/samples/odoo:latest
volumes:
- data:/var/lib/odoo
- extra-addons:/mnt/extra-addons
ports:
- 8069:8069
deploy:
restart_policy:
condition: always
environment:
HOST: db
USER: odoo
PASSWORD: odoo
volumes:
data:
driver: azure_file
driver_opts:
share_name: odoofileshare
storage_account_name: odooaccount1
db:
driver: azure_file
driver_opts:
share_name: odoofileshare
storage_account_name: odooaccount1
extra-addons:
driver: azure_file
driver_opts:
share_name: odoofileshare
storage_account_name: odooaccount1
After looking at some answers in related questions I tried with : expose : - 5432 , ports : - 5432 and not writing it the docker compose I still had the same issue.
Here's a screen of the container logs:
Azure container logs
I try to connect to my Postgres database built by docker in DataGrip but I get connection error.
Here is my application.yml file:
spring:
jpa:
database: POSTGRESQL
show-sql: true
hibernate:
ddl-auto: update
datasource:
url: jdbc:postgresql://db:5432/postgis_db?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true
username: postgres
password: postgres
Here is docker-compose.yml file:
version: '3.8'
services:
db:
image: postgres:latest
restart: always
volumes:
- db:/var/lib/postgresql/data
environment:
POSTGRES_DB: postgis_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
expose:
- 5432
networks:
- app-network
geolocation-service:
image: geolocation-service
build:
context: .
dockerfile: dockerfile
restart: always
ports:
- "8080:8080"
networks:
- app-network
volumes:
db:
networks:
app-network:
DataGrip connection settings:
And the error I get:
Does anybody knows how to solve this?
Connecting to this db by shell works fine:
docker exec -it a37 psql -U postgres postgis_db
Please check pg_hba.conf file, you have to allow connections from all hosts for this user, by default it is restricted to localhost.
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 have a docker-compose file with services for python, nginx, postgres and pgadmin:
services:
postgres:
image: postgres:9.6
env_file: .env
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5431:5431"
pgadmin:
image: dpage/pgadmin4
links:
- postgres
depends_on:
- postgres
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: pwdpwd
volumes:
- pgadmin:/root/.pgadmin
ports:
- "5050:80"
backend:
build:
context: ./foobar # This refs a Dockerfile with Python and Django requirements
command: ["/wait-for-it.sh", "postgres:5431", "--", "/gunicorn.sh"]
volumes:
- staticfiles_root:/foobar/static
depends_on:
- postgres
nginx:
build:
context: ./foobar/docker/nginx
volumes:
- staticfiles_root:/foobar/static
depends_on:
- backend
ports:
- "0.0.0.0:80:80"
volumes:
postgres_data:
staticfiles_root:
pgadmin:
When I run docker-compose up and visit localhost:5050, I see the pgadmin interface. When I try to create a new server there, with localhost or 0.0.0.0 as host name and 5431 as port, I get an error "Could not connect to server". If I remove these and instead enter postgres in the "Service" field, I get the error "definition of service "postgres" not found". How can I connect to the database with pgadmin?
the docker container name changes when you run docker-compose to prefix the folder name (to keep container names unique). You could force the name of the container with container_name property
version: "3"
services:
# postgres database
postgres:
image: postgres:12.3
container_name: postgres
environment:
- POSTGRES_DB=admin
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=admin
- POSTGRES_HOST_AUTH_METHOD=trust # allow all connections without a password. This is *not* recommended for prod
volumes:
- database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
ports:
- "5432:5432"
# pgadmin for managing postgis db (runs at localhost:5050)
# To add the above postgres server to pgadmin, use hostname as defined by docker: 'postgres'
pgadmin:
image: dpage/pgadmin4
container_name: pgadmin
environment:
- PGADMIN_DEFAULT_EMAIL=admin
- PGADMIN_DEFAULT_PASSWORD=admin
- PGADMIN_LISTEN_PORT=5050
ports:
- "5050:5050"
volumes:
database-data:
Another option is to connect the postgres container to localhost with
network_mode: host
But you lose the nice network isolation from docker that way
Be careful that the default postgres port is 5432 not 5431. You should update the port mapping for the postgres service in your compose file. The wrong port might be the reason for the issues you reported. Change the port mapping and then try to connect to postgres:5432. localhost:5432 will not work.