Skip to content

Commit

Permalink
postgress new image
Browse files Browse the repository at this point in the history
  • Loading branch information
bestia.dev committed Feb 15, 2025
1 parent 03b46bd commit f939247
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"clippy",
"cntssh",
"cranelift",
"createcluster",
"crev",
"crun",
"crustde",
"CRUSTDE",
"ctlcluster",
"cweijan",
"daemonless",
"davidanson",
Expand Down Expand Up @@ -67,6 +69,7 @@
"LLDB",
"localnet",
"lonefy",
"lsclusters",
"luciano",
"lucianobestia",
"Lxss",
Expand Down
45 changes: 34 additions & 11 deletions create_and_push_container_images/crustde_postgres_img.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

printf " \n"
printf "\033[0;33m Bash script to build the docker image for the postgres database server \033[0m\n"
printf "\033[0;33m The image is created without clusters. They must be created in the entrypoint bash script. \033[0m\n"
printf "\033[0;33m The entrypoint must be set when 'podman create' the container. \033[0m\n"
printf "\033[0;33m This is described in 'create_and_push_container_images\postgres_entrypoint.sh' \033[0m\n"
printf "\033[0;33m Name of the image: crustde_postgres_img \033[0m\n"
# repository: https://github.com/CRUSTDE-ContainerizedRustDevEnv/crustde_cnt_img_pod

printf "\033[0;33m postgres image on docker hub has 8 layers. \033[0m\n"
printf "\033[0;33m I don't know if this is too much and affects performance, \033[0m\n"
printf "\033[0;33m but I will squash it to one single layer. \033[0m\n"

printf "\033[0;33m To build the image, run in bash with: \033[0m\n"
printf "\033[0;33m sh crustde_postgres_img.sh \033[0m\n"

Expand All @@ -24,16 +23,12 @@ buildah rm crustde_postgres_img || :
buildah rmi -f docker.io/bestiadev/crustde_postgres_img || :

printf " \n"
printf "\033[0;33m Create new 'buildah container' named crustde_postgres_img from official //hub.docker.com/_/postgres \033[0m\n"
printf "\033[0;33m Version postgres:15 on Debian 12 bookworm \033[0m\n"

printf "\033[0;33m Create new 'buildah container' named crustde_postgres_img \033[0m\n"
printf "\033[0;33m Version postgres 15 on Debian 12 bookworm \033[0m\n"
set -o errexit
buildah from \
--name crustde_postgres_img \
docker.io/library/postgres:15

printf "\033[0;33m podman image tree docker.io/library/postgres:15 \033[0m\n"
podman image tree docker.io/library/postgres:15
docker.io/library/debian:bookworm-slim

buildah config \
--author=github.com/bestia-dev \
Expand All @@ -42,11 +37,39 @@ buildah config \
--label source=github.com/CRUSTDE-ContainerizedRustDevEnv/crustde_cnt_img_pod \
crustde_postgres_img

printf " \n"
printf "\033[0;33m Debian apt update and upgrade \033[0m\n"
buildah run crustde_postgres_img apt -y update
buildah run crustde_postgres_img apt -y full-upgrade

printf " \n"
printf "\033[0;33m Install nano, the default easy to use text editor in Debian \033[0m\n"
buildah run crustde_postgres_img apt -y install nano

printf " \n"
printf "\033[0;33m Create non-root user 'postgres' the database superuser. \033[0m\n"
buildah run crustde_postgres_img groupadd postgres
buildah run crustde_postgres_img useradd -g postgres -m postgres

printf " \n"
printf "\033[0;33m Install postgresql-common \033[0m\n"
buildah run crustde_postgres_img apt -y install postgresql-common
printf "\033[0;33m I don't want the default cluster to be installed. \033[0m\n"
printf "\033[0;33m Change the line #create_main_cluster = true to false in /etc/postgresql-common/createcluster.conf \033[0m\n"
buildah run crustde_postgres_img sed -i 's/#create_main_cluster = true/create_main_cluster = false/g' /etc/postgresql-common/createcluster.conf

printf " \n"
printf "\033[0;33m Install postgres 15 in debian 12 \033[0m\n"
buildah run crustde_postgres_img apt -y install postgresql

printf " \n"
printf "\033[0;33m Remove unwanted files \033[0m\n"
buildah run --user root crustde_postgres_img apt -y autoremove
buildah run --user root crustde_postgres_img apt -y clean

buildah config --user postgres crustde_postgres_img
buildah config --workingdir /home/postgres crustde_postgres_img

printf " \n"
printf "\033[0;33m Finally save/commit the image named crustde_postgres_img \033[0m\n"
buildah commit --squash crustde_postgres_img docker.io/bestiadev/crustde_postgres_img:latest
Expand Down
103 changes: 103 additions & 0 deletions create_and_push_container_images/postgres_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#!/bin/dash

# /usr/bin/entrypoint.sh
# This is a Debian Slim container, so shebang /bin/dash is appropriate for scripting.

# Before calling 'podman create', modify this script to your needs.
# Make the local script file executable before copying into the container:
# sudo chmod +x ~/rustprojects/crustde_cnt_img_pod/create_and_push_container_images/postgres_entrypoint.sh

# First 'create' the container, copy the entrypoint file and then start the container.
# Multiline block comment in bash script to run all together in bash:
<<'###BLOCK-COMMENT'
clear;
cd ~/rustprojects/crustde_cnt_img_pod/create_and_push_container_images
printf "\033[0;33m Show existing containers \033[0m\n";
podman ps -a;
printf "\033[0;33m Remove old container \033[0m\n";
podman rm -f crustde_postgres_cnt;
printf "\033[0;33m Podman create \033[0m\n";
podman create --name crustde_postgres_cnt -p 5450:5450 -p 5460:5460 --entrypoint /usr/bin/entrypoint.sh docker.io/bestiadev/crustde_postgres_img:postgres15;
printf "\033[0;33m Copy entrypoint.sh create \033[0m\n";
podman cp ~/rustprojects/crustde_cnt_img_pod/create_and_push_container_images/postgres_entrypoint.sh crustde_postgres_cnt:/usr/bin/entrypoint.sh ;
printf "\033[0;33m Podman start \033[0m\n";
podman start crustde_postgres_cnt;
printf "\033[0;33m Read the log with: podman logs crustde_postgres_cnt \033[0m\n";
###BLOCK-COMMENT

# Connect from Debian host of the container:
# psql -h localhost -p 5450 -U postgres -W
# SHOW cluster_name;
# psql -h localhost -p 5460 -U postgres -W
# SHOW cluster_name;

printf "\033[0;33m Running container crustde_postgres_cnt \033[0m\n"

printf " \n"
printf "\033[0;33m Create and run new clusters using the debian wrapper tool pg_createcluster: \033[0m\n"

printf " \n"
printf "\033[0;33m cluster 'dev_01' on port '5450' \033[0m\n"
printf "\033[0;33m data in /var/lib/postgresql/15/dev_01 \033[0m\n"
printf "\033[0;33m conf in /etc/postgresql/15/dev_01 \033[0m\n"
printf "\033[0;33m log in /var/log/postgresql/postgresql-15-dev_01.log \033[0m\n"
pg_createcluster --port=5450 15 dev_01;

printf "\033[0;33m Change the line #listen_addresses = 'localhost' to '*' in /etc/postgresql/15/dev_01/postgresql.conf \033[0m\n"
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /etc/postgresql/15/dev_01/postgresql.conf

printf "\033[0;33m Change the line in /etc/postgresql/15/dev_01/pg_hba.conf from 127.0.0.1 to 0.0.0.0 \033[0m\n"
sed -i "s/host all all 127.0.0.1\/32 scram-sha-256/host all all 0.0.0.0\/0 md5/g" /etc/postgresql/15/dev_01/pg_hba.conf

pg_ctlcluster 15 dev_01 start;

printf "\033[0;33m Set password for postgres user using local peer connection over unix socket \033[0m\n"
psql -p 5450 -c "ALTER USER postgres WITH PASSWORD 'Passw0rd';"

# Check the log
# podman exec crustde_postgres_cnt cat /var/log/postgresql/postgresql-15-dev_01.log

# Connection rule 'listen_addresses' in postgresql.conf
# podman exec -it crustde_postgres_cnt psql -p 5450
# show listen_addresses;
# SHOW config_file;
# podman exec -it crustde_postgres_cnt nano /etc/postgresql/15/dev_01/postgresql.conf
# podman cp crustde_postgres_cnt:/etc/postgresql/15/dev_01/postgresql.conf ./postgresql.conf

# The rules who can connect to the cluster is in the file /etc/postgresql/15/dev_01/pg_hba.conf
# podman exec -it crustde_postgres_cnt nano /etc/postgresql/15/dev_01/pg_hba.conf
# podman cp crustde_postgres_cnt:/etc/postgresql/15/dev_01/pg_hba.conf ./pg_hba.conf
# After changes, to reload conf:
# podman exec -it crustde_postgres_cnt psql -p 5450
# SELECT pg_reload_conf();
# To see the effective rules in psql:
# TABLE pg_hba_file_rules;

# Run psql from the Debian host of the container:
# psql -h localhost -p 5450 -U postgres -W

printf " \n"
printf "\033[0;33m cluster 'test_01' on port '5460' \033[0m\n"
printf "\033[0;33m data in /var/lib/postgresql/15/test_01 \033[0m\n"
printf "\033[0;33m conf in /etc/postgresql/15/test_01 \033[0m\n"
printf "\033[0;33m log in /var/log/postgresql/postgresql-15-test_01.log \033[0m\n"
pg_createcluster --port=5460 15 test_01;

printf "\033[0;33m Change the line #listen_addresses = 'localhost' to '*' in /etc/postgresql/15/test_01/postgresql.conf \033[0m\n"
sed -i "s/#listen_addresses = 'localhost'/listen_addresses = '*'/g" /etc/postgresql/15/test_01/postgresql.conf

printf "\033[0;33m Change the line in /etc/postgresql/15/test_01/pg_hba.conf from 127.0.0.1 to 0.0.0.0 \033[0m\n"
sed -i "s/host all all 127.0.0.1\/32 scram-sha-256/host all all 0.0.0.0\/0 md5/g" /etc/postgresql/15/test_01/pg_hba.conf

pg_ctlcluster 15 test_01 start;

printf "\033[0;33m Set password for postgres user using local peer connection over unix socket \033[0m\n"
psql -p 5460 -c "ALTER USER postgres WITH PASSWORD 'Passw0rd';"


printf " \n"
pg_lsclusters

# container runs only until there is a foreground process
# this will make it run forever or at least 24 hours. Enough for development.
sleep infinity

0 comments on commit f939247

Please sign in to comment.