Keycloak is not saving data in PostgreSQL in docker - postgresql

I am trying to run keycloak in docker and to save its data in PostgreSQl.
But nothing is being saved.
this is the docker-compose.yml file:
version: '2'
services:
db:
build: "./Main Database Backup"
environment:
POSTGRES_DB: ${DB_POSTGRES_APP_DATABASE}
POSTGRES_USER: ${DB_POSTGRES_APP_USER}
POSTGRES_PASSWORD: ${DB_POSTGRES_APP_PASSW}
PGDATA: /var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
restart: unless-stopped
keycloak-postgres:
image: postgres:10-alpine
environment:
POSTGRES_DB: ${KEYCLOAK_DATABASE}
POSTGRES_PASSWORD: ${KEYCLOAK_DATABASE_PASSW}
POSTGRES_USER: ${KEYCLOAK_DATABASE_USER}
PGDATA: /var/lib/postgresql/data/pgdata
restart: unless-stopped
keycloak:
build: "./Keycloak Realm Export"
depends_on:
- keycloak-postgres
environment:
KEYCLOAK_USER: ${KEYCLOAK_USER}
KEYCLOAK_PASSWORD: ${KEYCLOAK_PASSWORD}
POSTGRES_USER: ${KEYCLOAK_DATABASE_USER}
POSTGRES_PASSWORD: ${KEYCLOAK_DATABASE_PASSW}
POSTGRES_PORT_5432_TCP_ADDR: keycloak-postgres
ports:
- "8080:8080"
Dockerfile in for keycloak
FROM jboss/keycloak:3.4.3.Final
WORKDIR /opt/jboss/keycloak
COPY realm-export.json initial_data.json
# RUN ./bin/standalone.sh -Dkeycloak.migration.action=import -Dkeycloak.migration.provider=singleFile -Dkeycloak.migration.file=initial_data.json -Dkeycloak.migration.strategy=OVERWRITE_EXISTING
RUN ./bin/add-user-keycloak.sh -r master -u admin -p password
ENTRYPOINT [ "/opt/jboss/docker-entrypoint.sh" ]
CMD ["-b", "0.0.0.0", "-Dkeycloak.import=/opt/jboss/keycloak/initial_data.json"]
That db is the main database which is for my API and its working correctly.
While the keycloak-postgres is the database for keycloak and its not saving any data.
Also I have created a database in that server with the same name as ${KEYCLOAK_DATABASE} and I have created a user and gave all privileges to that server, so it wont be a permission error.
And I have provided all the environment variables correctly.
Also regarding to commented code in keycloak Dockerfile, I'm trying to import a realm which is not working.
When I'm commenting that POSTGRES_PORT_5432_TCP_ADDR in docker-compose.yml its throwing this error:
2018-08-31T08:22:05.251344638Z 08:22:05,250 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([
2018-08-31T08:22:05.251375320Z ("subsystem" => "datasources"),
2018-08-31T08:22:05.251383320Z ("data-source" => "KeycloakDS")
2018-08-31T08:22:05.251402169Z ]) - failure description: "WFLYCTL0211: Cannot resolve expression 'jdbc:postgresql://${env.POSTGRES_PORT_5432_TCP_ADDR}:${env.POSTGRES_PORT_5432_TCP_PORT:5432}/${env.POSTGRES_DATABASE:keycloak}'"

This isn't Keycloak related, it's more Docker container related (and postgres). Each time you stop a container you going to loose your data.
What you need to use is Volumes ... meaning. Map a driver on your PC to the docker container. Such that each time the container starts again it uses this drive therefore able to retain data. You need something along the lines of:
version: '2'
volumes:
postgres_data:
driver: local
services:
db:
build: "./Main Database Backup"
environment:
POSTGRES_DB: ${DB_POSTGRES_APP_DATABASE}
POSTGRES_USER: ${DB_POSTGRES_APP_USER}
POSTGRES_PASSWORD: ${DB_POSTGRES_APP_PASSW}
PGDATA: /var/lib/postgresql/data/pgdata
volumes:
- postgres_data:/var/lib/postgresql/data
- ./data:/docker-entrypoint-initdb.d
ports:
- "5432:5432"
restart: unless-stopped

Related

I don't manage to configure volumes in docker-compose to have a folder reachable by dockerized pgAdmin, to restore a database

I have setup from a tutorial a pgadmin4 container and a PostgreSQL container with the following docker-compose:
version: "2"
services:
db:
image: postgres:14
restart: always
environment:
POSTGRES_DB: postgres
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
PGDATA: /var/lib/postgresql/data
volumes:
- db-data:/opt/docker/pgadmin4/postgresql/data
- /home/laurent/Documents/Informatique/Docker/sample-database:/opt
ports:
- "5432:5432"
pgadmin:
image: dpage/pgadmin4:4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: admin#linuxhint.com
PGADMIN_DEFAULT_PASSWORD: secret
PGADMIN_LISTEN_PORT: 80
ports:
- "8080:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
- /home/laurent/Documents/Informatique/Docker/sample-database:/opt
links:
- "db:pgsql-server"
volumes:
db-data:
pgadmin-data:
In both conainers, I have mounted the path /home/laurent/Documents/Informatique/Docker/sample-database where I have put a sample database.
After having created the dvdrental database as explained in the tutorial, I try to import it with restore, but I cannot find my /opt/dvdrental.tar as expected.
When I Select file, I get this:
If I add opt in the path and refresh, nothing more.
But if I open a console in both containers, I have /opt/dvdrental.tar.
What do I miss?

How does Postgresql inside Docker work? psql: FATAL: password authentication failed for user xy

I created my app with .yml
services:
db:
image: postgres:11-alpine
environment:
POSTGRES_DB: wiki
POSTGRES_PASSWORD: quantoxrocks
POSTGRES_USER: wikijs
logging:
driver: "none"
restart: unless-stopped
volumes:
- db-data:/var/lib/postgresql/data
wiki:
image: ghcr.io/requarks/wiki:2
depends_on:
- db
environment:
DB_TYPE: postgres
DB_HOST: db
DB_PORT: 5432
DB_USER: wikijs
DB_PASS: quantoxrocks
DB_NAME: wiki
restart: unless-stopped
ports:
- "3000:3000"
webserver:
image: nginx:alpine
restart: unless-stopped
tty: true
ports:
- "443:443"
- "80:80"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
- ./ssl:/etc/nginx/ssl
volumes:
db-data:
I logged in my db container and want to create database. I have tried at least 10 times and I am sure that password is from the above docker-compose.yml file. It does not work.
docker exec -it wiki_db_1 sh
Next
psql -h wiki_db_1 -U wikijs
Password for user wikijs:
psql: FATAL: password authentication failed for user "wikijs"
Why? How can I check any further logs?
The environment variables for Postgres are only used if there is no database present already when the container starts.
You have a volume mapping of /var/lib/postgresql/data and it's likely that you already have a database there, which was created with different values from the environment variable values.
If you don't have any important data in the existing database, you can delete the volume and Postgres will create a new database with the correct username/password.

Does Postgres Store the POSTGRES_USER and POSTGRES_PASSWORD When the Data Directory is Created

Experimenting with Postgres and Pgadmin4 with Docker Compose. Initially I use this as my docker-compose.yml file.
version: '3.8'
services:
db:
container_name: pg_container
image: postgres
restart: always
volumes:
- ./postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: test_db
ports:
- "5432:5432"
pgadmin:
container_name: pgadmin4_container
image: dpage/pgadmin4
restart: always
volumes:
- ./data/pgadmin:/var/lib/pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
links:
- "db:psql-server"
I ran:
chown -R 5050:5050 data
So that pgadmin4 could save it's configuration and it worked beautifully. Then I tried to bring it down and update the POSTGRES_PASSWORD to test.
docker-compose down
UPDATED docker-compose.yml file to:
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: test
And then brought it back up:
docker-compose up
To my surprise when connecting to Postgres from Pgadmin4 test does not work as the password, but root does. So my question is when Postgres creates the data directory does it store the credentials somewhere in there? Is there a way to update the password and user once it has been initially created?

How to fix "Cannot start service pgsql: b'OCI runtime create failed:" when trying to up a pgsql container?

I get the following
ERROR: for marx_pgsql_1 Cannot start service pgsql: b'OCI runtime create failed: container_linux.go:345: starting container process caused "exec: \"/docker-entrypoint.sh\": permission denied": unknown'
when i try to fire up a pgsql alpine docker image.
Here is my docker-compose.yml
web:
image: nginx:1.17.1-alpine
ports:
- "80:80"
volumes:
- ./code:/code
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
build: .
volumes:
- ./code:/code
links:
- pgsql
pgsql:
image: yobasystems/alpine-postgres:latest
environment:
POSTGRES_DB: bookmarx
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
expose:
- "5432"
volumes:
- ./data:/var/lib/postgresql/data
restart: always
How do we fix this ?
I think the script docker-entrypoint.sh is not executable , I suggest to do the following:
create a Dockerfile:
FROM yobasystems/alpine-postgres:latest
RUN chmod +x docker-entrypoint.sh
update your docker-compose:
pgsql:
build: .
environment:
POSTGRES_DB: bookmarx
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
expose:
- "5432"
volumes:
- ./data:/var/lib/postgresql/data
restart: always

I cannot connect from adminer to postgresql

I try to up postgresql and adminer via docker container. But from adminer I cannot enter to postgresql with password and user I whote.
SQLSTATE[08006] [7] FATAL: password authentication failed for user "root"
I tried all.
version: '3'
services:
web:
build: .
environment:
- APACHE_RUN_USER=www-data
volumes:
- ./blog:/var/www/html/
ports:
- 8080:80
working_dir: /var/www/html/
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: kisphp
POSTGRES_USER: root
POSTGRES_DB: kisphp
ports:
- "5432:5432"
volumes:
- ./postgres:/var/lib/postgresql/data
adminer:
image: adminer
restart: always
ports:
- "6080:8080"
This docker-compose configuration works well.
Try recreating it from scratch:
Delete ./postgres folder
docker-compose stop
docker-compose down
docker-compose up -d