简体   繁体   中英

Keycloak is not saving data in PostgreSQL in docker

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM