How to deploy Keycloak HA cluster in Docker - postgresql

I'm following http://blog.keycloak.org/2015/04/running-keycloak-cluster-with-docker.html and when trying to run the first keycloak instance:
docker run --name postgres -e POSTGRES_DATABASE=keycloak -e POSTGRES_USER=keycloak -e POSTGRES_PASSWORD=password -e POSTGRES_ROOT_PASSWORD=password -d postgres
docker run -p 8080:8080 --name keycloak --link postgres:postgres -e POSTGRES_DATABASE=keycloak -e POSTGRES_USER=keycloak -e POSTGRES_PASSWORD=password -d jboss/keycloak-ha-postgres
I am getting the error:
javax.resource.ResourceException: IJ031083: Wrong driver class [org.postgresql.Driver] for this connection URL [jdbc:postgresql://postgres:tcp://172.17.0.2:5432/keycloak]
Has anyone got experience using this Keycloak Docker image? Or is there an easier way to deploy a Keycloak cluster to Docker?

an example for deploy keycloak in HA mode with postgres
version: '3'
volumes:
postgres_data:
driver: local
services:
postgres:
image: 'postgres:alpine'
volumes:
- ./postgres:/var/lib/postgresql/data
restart: 'always'
# ports:
# - 5432:5432
environment:
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: password
POSTGRES_DB: keycloak
POSTGRES_HOST: postgres
traefik:
image: library/traefik:alpine
container_name: traefik
volumes:
- /var/run/docker.sock:/var/run/docker.sock
command: >
--logLevel=ERROR
--api.dashboard
--docker
--entrypoints="Name:http Address::80"
--defaultentrypoints="http"
ports:
- 80:80
- 3000:8080
keycloak:
image: jboss/keycloak
environment:
DB_VENDOR: postgres
DB_ADDR: postgres
DB_PORT: 5432
DB_DATABASE: keycloak
DB_USER: keycloak
DB_PASSWORD: password
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: Pa55w0rd
# KEYCLOAK_LOGLEVEL: DEBUG
JGROUPS_DISCOVERY_PROTOCOL: JDBC_PING
JGROUPS_DISCOVERY_PROPERTIES: datasource_jndi_name=java:jboss/datasources/KeycloakDS,info_writer_sleep_time=500,initialize_sql="CREATE TABLE IF NOT EXISTS JGROUPSPING ( own_addr varchar(200) NOT NULL, cluster_name varchar(200) NOT NULL, created timestamp default current_timestamp, ping_data BYTEA, constraint PK_JGROUPSPING PRIMARY KEY (own_addr, cluster_name))"
depends_on:
- postgres
labels:
traefik.enable: true
traefik.port: 8080
traefik.protocol: http
traefik.frontend.rule: Host:localhost
traefik.frontend.passHostHeader: true
# traefik.backend.loadbalancer.stickiness: true
https://gist.github.com/ERRECabrera/a4fb1ebdba300521b46587881b66aaf4

You should try using this more updated docker-compose file provided by the people behind the image wich might contain updated var names and versions. I ran it and it created the containers correctly. Just download the file and run docker-compose up and you'll have the stack running.
In the example you mentioned they just start another docker container, which in docker compose would only mean adding a new entry. Checkout this gist: https://gist.github.com/pacuna/e7427d8fef752992ff1df944223ad0ab
Now, that's not the ideal way of running a cluster of docker containers, you may want to checkout docker swarm or Kubernetes if it's a serious project. The docker compose template would be easy to translate to Kubernetes files.

Related

Pgadmin does not start with docker-compose-file

I started working with postgres and discovered pgadmin.
I started like this:
Postgres:
$ docker run --name admin -e POSTGRES_PASSWORD=admin -p 5432:5432 -d postgres:latest
Pgadmin:
docker run -p 5050:80 -e "PGADMIN_DEFAULT_EMAIL=admin#admin.com" -e "PGADMIN_DEFAULT_PASSWORD=root" -d dpage/pgadmin4
So this worked perfectly fine, I started the postgres container and afterwarda the pgadmin cointainer and got on the site http://localhost:80/login and could login
The problem now is the docker-compose.yml that I wrote. As I deploy my docker-compose-file, the containers are both running but I can't access the login page of pgadmin
Docker-compose.yml:
version: '3.8'
services:
db:
image: postgres:latest
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
ports:
- "5432:5432"
pgadmin:
image: dpage/pgadmin4:latest
environment:
PGADMIN_DEFAULT_EMAIL: admin#admin.com
PGADMIN_DEFAULT_PASSWORD: root
ports:
- "5050:80"
When I run it, it works as it should. So I think your issue is the URL you try to access it on. You need to access it on the mapped port 5050. Not 80, as you've written. So the URL is http://localhost:5050/login.

problem installing bamboo trial using docker and connecting to the postgres database on localhost

I am trying to run bamboo-server using a docker container and connect it to postgres db that is running on another container. First I run the postgres db and create an empty database named bamboo with a user postgres and password postgres.
And I run this commend to run bamboo server from https://hub.docker.com/r/atlassian/bamboo
$> docker volume create --name bambooVolume
$> docker run -v bambooVolume:/var/atlassian/application-data/bamboo --name="bamboo" -d -p 8085:8085 -p 54663:54663 atlassian/bamboo
Then I open localhost:8085 and generate a license and reach the point that I see this error
Error accessing 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.
What is the problem?
SOLUTION:
Worked with this dokcer-compose yaml:
version: '2'
services:
bamboo:
image: atlassian/bamboo
container_name: bamboo
ports:
- '54663:5436'
- '8085:8085'
networks:
- bamboonet
volumes:
- bamboo-data:/var/atlassian/application-data/bamboo
hostname: bamboo
environment:
CATALINA_OPTS: -Xms256m -Xmx1g
BAMBOO_PROXY_NAME:
BAMBOO_PROXY_PORT:
BAMBOO_PROXY_SCHEME:
BAMBOO_DELAYED_START:
labels:
com.blacklabelops.description: "Atlassian Bamboo"
com.blacklabelops.service: "bamboo"
db-bamboo:
image: postgres
container_name: postgres
hostname: postgres
networks:
- bamboonet
volumes:
- bamboo-data-db:/var/lib/postgresql/data
ports:
- '5432:5432'
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: bamboo
POSTGRES_DB: bamboo
POSTGRES_ENCODING: UTF8
POSTGRES_COLLATE: C
POSTGRES_COLLATE_TYPE: C
PGDATA: /var/lib/postgresql/data/pgdata
labels:
com.blacklabelops.description: "PostgreSQL Database Server"
com.blacklabelops.service: "postgresql"
volumes:
bamboo-data:
external: false
bamboo-data-db:
external: false
networks:
bamboonet:
driver: bridge
If you don't set network of your docker it will be used bridge mode as default.
I think the problem is you might use {containerName}:5432 instead of localhost:5432 from your JDBC connection string, because localhost mean your container of website instead of real computer, so that you can't connect to DB by that.
jdbc:postgresql://bamboo-pg-db-container:5432/bamboo

Docker and Postgis - How do I access shp2pgsql inside my docker container?

I am running postgres in a docker container using docker-compose and it spins up with no issue and I am able to connect to the database. But now I want to go into the container and execute the postgis shp2pgsql to load a shape file but the command seems to be nonexistent. Below is my code:
docker-compose.yaml
version: '3'
services:
db:
container_name: pg_container
image: postgis/postgis
restart: always
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: test_db
volumes:
- ./data:/var/lib/postgresql/
- ./postgres_init:/postgres_init
ports:
- 5433:5433
networks:
- ch_ntw
networks:
ch_ntw:
driver: bridge
ipam:
config:
- subnet: 10.222.1.0/24
Getting into the container:
docker exec -it pg_container bash
Connecting to the db without issue using psql:
psql --host=pg_container --dbname=test_db --username=root
But then if I try to invoke shp2pgsql from bash I get the following:
shp2pgsql -s 2263:4326 postgres_init/nyct2010_15b/nyct2010.shp | psql -d test_db
bash: shp2pgsql: command not found
I would think since this is a postgis container that the function should be accessible no?
shp2pgsql is a client package. The postgis/postgis image is the PostGIS server components only. If you want to use shp2pgsql or other client tools, install them locally on your host, or in another container.

connect hasura to existing postgresql

Docker desktop (windows10) running in WSL2
postgresql running in WSL2
pgadmin running in windows10
I can connect with pgadmin (local machine) to postgresql (localmachine WSL2) with the default settings
(localhost:5432)
postgres.conf
listen_addresses = '*'
port = 5432
When I create a docker container it will not connect to my local postgresql.
cmd used in WSL2
docker run -d --net=host \
-e HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:password#localhost:5432/mydb \
-e HASURA_GRAPHQL_ENABLE_CONSOLE=true \
-e HASURA_GRAPHQL_DEV_MODE=true \
hasura/graphql-engine:v1.3.3
error
"could not connect to server: Connection refused\n\tIs the server running on host \"localhost\" (127.0.0.1) and accepting\n\tTCP/IP connections on port 5432?\n","path":"$","error":"connection error","code":"postgres-error"}
What am I missing?
turned out I had to use this:
docker run -d -p 8080:8080
-e HASURA_GRAPHQL_DATABASE_URL=postgres://postgres:password#host.docker.internal:5432/mydb \
-e HASURA_GRAPHQL_ENABLE_CONSOLE=true \
-e HASURA_GRAPHQL_DEV_MODE=true \
hasura/graphql-engine:v1.3.3
I thought "host.docker.internal" was only ment for Mac. Seems to work with Docker Desktop Windows10(WSL2) too.
here is a working solution for me, important is hostname
version: "3.8"
services:
postgres:
restart: always
image: postgres
container_name: postgres
hostname: postgres
#depends_on:
#sql-server:
#condition: service_healthy
volumes:
- pg_data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgrespassword
networks:
- backend
sql-api:
restart: always
container_name: api
image: hasura/graphql-engine:v2.2.0
ports:
- 8055:8080
depends_on:
- "postgres"
hostname: sqlapi
environment:
## postgres database to store Hasura metadata
HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgrespassword#postgres:5432/postgres
## enable the console served by server
HASURA_GRAPHQL_ENABLE_CONSOLE: "true" # set to "false" to disable console
## enable debugging mode. It is recommended to disable this in production
HASURA_GRAPHQL_DEV_MODE: "true"
HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
## uncomment next line to set an admin secret
# HASURA_GRAPHQL_ADMIN_SECRET: myadminsecretkey
networks:
- backend
networks:
backend:
driver: bridge
volumes:
pg_data:

Pgadmin4 cannot connect to my postgres database

here is my docker-compose.yml file:
version: "3.5"
services:
db:
image: myapp
container_name: my-database
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypasswd
POSTGRES_DB: mydb
expose:
- "5432"
ports:
- 8000:5432
volumes:
- /path/to/my/migrationsV1_0__audit_table.sql
- //path/to/my/migrations/V1_1__tables.sql
pgadmin:
image: dpage/pgadmin4
ports:
- 5454:5454/tcp
environment:
- PGADMIN_DEFAULT_EMAIL=admin#mydomain.com
- PGADMIN_DEFAULT_PASSWORD=postgres
- PGADMIN_LISTEN_PORT=5454
Then i $ docker inspect xxxxxxxx | grep "IPAddress"
It produces output:
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
After docker-compose I enter values to pgadmin as so:
Pgadmin_capture
Yet pgadmin gives error:
Unable to connect to server
timeout expired.
What is wrong here?
In your configuration PostgreSQL database has 5432 port (it is default value).
Are you sure in this line?
PGADMIN_LISTEN_PORT=5454
Try to replace it with
PGADMIN_LISTEN_PORT=5432
for a workaround to this issue, just run the container with --user=root (in docker-compose use user: root)
You need to use the gateway address of Postgres container. Use docker inspect xxxxxxxx | grep Gateway to get it. I guess in your case it would be 172.17.0.1
In my case, my pgadmin container didn't shared the DB container's network.
So, you should run this command.
docker network inspect NETWORK <YOUR DB CONTAINER'S NETWORK>
And if it doesn't, run this command
docker network connect <YOUR_DB_CONTAINER'S_NETWORK> <YOUR_PGADMIN_CONTAINER'S NAME>