Skip to content

Commit a32ca45

Browse files
author
Roman Rafacz
committed
healthchecks added
1 parent cd841d3 commit a32ca45

6 files changed

+92
-19
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ EXPOSE 5432
5555

5656
USER postgres
5757

58-
#HEALTHCHECK CMD ["/usr/local/bin/docker-healthcheck.sh"]
58+
HEALTHCHECK CMD ["/usr/local/bin/docker-healthcheck.sh"]
5959

6060
ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]

README.md

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
Introduction
44

5-
This repository contains the Dockerfiles used to build a PostgresSQL server.
6-
7-
We currently built and maintained for:
8-
5+
This repository contains the Dockerfiles used to build a PostgresSQL server, currently built and maintained for:
96
PostgresSQL 14.0
107

118
# building
@@ -14,3 +11,14 @@ PostgresSQL 14.0
1411

1512
# tests
1613

14+
# TODO
15+
- Healthchecks
16+
- Tests - create them
17+
- Standalone
18+
- Replica
19+
- Backup and Restore
20+
- Create Test application to read/write
21+
- Generate certificates to use(or default ones may be ok)
22+
- Deploy to ECS
23+
- fix logging
24+
- try logging in with a non existing user, docker will show error. postgres no.. actually no logss

docker-entrypoint.sh

+42-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
#!/usr/bin/env bash
22

33
set -o pipefail
4-
set -eux
5-
#
6-
POSTGRES_USER=${POSTGRES_USER:-postgres}
7-
POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-password}
8-
PWFILE=/tmp/postgres_password
9-
PG_DATA=${PG_DATA:-/var/lib/postgresql/main}
10-
POSTGRES_LOGS_DIR=/var/log/postgresql
11-
POSTGRES_LOGS_FILE=/var/log/postgresql/postgresql.log
12-
POSTGRES_CLUSTER=${POSTGRES_CLUSTER:-genera}
13-
POSTGRES_CONFIG=${POSTGRES_CONFIG:-/etc/postgresql/postgresql.conf}
4+
set -eu
145

156
function setup_environment() {
167

@@ -29,26 +20,63 @@ function setup_environment() {
2920
chown -R postgres:postgres "${POSTGRES_LOGS_FILE}"
3021
fi
3122

32-
cat <<< "${POSTGRES_PASSWORD}" > "${PWFILE}"
23+
cat <<< "${POSTGRES_DEFAULT_PASSWORD}" > "${PW_FILE}"
3324

3425
return 0
3526

3627
}
3728

3829
function initialize_database() {
3930

40-
/usr/lib/postgresql/14/bin/initdb --pgdata="${PG_DATA}" --username="${POSTGRES_USER}" --pwfile="${PWFILE}" --data-checksums
31+
/usr/lib/postgresql/14/bin/initdb --pgdata="${PG_DATA}" --username="${POSTGRES_DEFAULT_USER}" --pwfile="${PW_FILE}" --data-checksums
4132

42-
rm "${PWFILE}"
33+
rm "${PW_FILE}"
4334

4435
return 0
4536

4637
}
4738

39+
# Used mainly for docker tests/healthcheck user but also to setup initial users
40+
function setupUsers() {
41+
42+
temp_sql=$(mktemp /tmp/user_update.XXXXXX)
43+
44+
if [[ "${ADMIN_USER}" != nil && "${ADMIN_PASSWORD}" != nil && "${POSTGRES_DATABASE}" != nil ]];then
45+
/usr/lib/postgresql/14/bin/pg_ctl -D "${PG_DATA}" -l ${POSTGRES_LOGS_FILE} start
46+
# create grants for these users for database x
47+
/usr/lib/postgresql/14/bin/createuser --superuser "${ADMIN_USER}"
48+
/usr/lib/postgresql/14/bin/createdb "${POSTGRES_DATABASE}"
49+
50+
cat <<< "ALTER USER ${ADMIN_USER} PASSWORD '${ADMIN_PASSWORD}'" > "${temp_sql}"
51+
psql -a -q -f "${temp_sql}"
52+
/usr/lib/postgresql/14/bin/pg_ctl -D "${PG_DATA}" stop
53+
rm "${temp_sql}"
54+
fi
55+
56+
return 0
57+
}
58+
59+
# postgres settings/configs/directories
60+
POSTGRES_DEFAULT_USER=${POSTGRES_DEFAULT_USER:-postgres}
61+
POSTGRES_DEFAULT_PASSWORD=${POSTGRES_DEFAULT_PASSWORD:-password}
62+
PW_FILE=/tmp/postgres_password
63+
PG_DATA=${PG_DATA:-/var/lib/postgresql/main}
64+
POSTGRES_LOGS_DIR=/var/log/postgresql
65+
POSTGRES_LOGS_FILE=/var/log/postgresql/postgresql.log
66+
POSTGRES_CLUSTER=${POSTGRES_CLUSTER:-genera}
67+
POSTGRES_CONFIG=${POSTGRES_CONFIG:-/etc/postgresql/postgresql.conf}
68+
69+
# additional users/databases
70+
POSTGRES_DATABASE=${POSTGRES_DATABASE:-nil}
71+
ADMIN_USER=${ADMIN_USER:-nil}
72+
ADMIN_PASSWORD=${ADMIN_PASSWORD:-nil}
73+
POSTGRES_HEALTHCHECK_USER=${POSTGRES_HEALTHCHECK_USER:-healthcheck}
74+
POSTGRES_HEALTHCHECK_PASSWD=${MYSQL_HEALTHCHECK_PASSWD:-nil}
75+
4876
setup_environment || exit 1
4977
initialize_database || exit 1
78+
setupUsers || exit 1
5079

51-
# Working
5280
eval "exec /usr/lib/postgresql/14/bin/postgres -D ${PG_DATA} -c config_file=/etc/postgresql/postgresql.conf"
5381

5482
# What i should get working...maybe?

docker-healthcheck.sh

+12
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@
33
set -o pipefail
44
set -eu
55

6+
POSTGRES_STATUS_CODE=""
7+
8+
if /usr/lib/postgresql/14/bin/pg_isready -d postgres://localhost:5432/template; then
9+
POSTGRES_STATUS_CODE="200";
10+
else
11+
POSTGRES_STATUS_CODE="500";
12+
exit 1
13+
fi
14+
15+
exit 0
16+
17+

tests/postgres-standalone.sh

Whitespace-only changes.

tests/postgres-standalone.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
version: '2.2'
3+
networks:
4+
postgres_test:
5+
driver: bridge
6+
services:
7+
postgres:
8+
image: romedawg/postgres-14:latest
9+
hostname: postgres
10+
container_name: postgres
11+
domainname: romedawg.com
12+
user: postgres
13+
networks:
14+
postgres_test:
15+
aliases:
16+
- postgres
17+
- postgres.romedawg.com
18+
ports:
19+
- '5432:5432'
20+
environment:
21+
ADMIN_USER: admin
22+
ADMIN_PASSWORD: password
23+
POSTGRES_DATABASE: admin
24+
25+

0 commit comments

Comments
 (0)