PostgreSQL Container Image do Red Hat đóng gói và Docker đóng gói – Bạn chọn cái nào?

PostgreSQL là cơ sở dữ liệu quan hệ thường nhanh nhảu ra mắt phiên bản mới với các tính năng SQL mới mà các RDBMS khác phải ghen tị.

PostgresSQL 16 là phiên bản mới nhất hiện nay được hỗ trợ trong 5 năm tới 11/2028 mới hết vòng đời phát triển.

PostgreSQL thường được đóng gói thành RPM và DEB để cài và chạy trực tiếp trên Linux. Tuy nhiên nếu bạn có nhu cầu chạy ứng dụng ở dạng container hóa (containerization) thì cũng có PostgreSQL container image cho bạn xài.

Team Docker có đóng gói PostgreSQL 15 thành Container image chạy trên 2 bản Linux là Debian 12 và Alpine Linux 3.19.

PostgreSQL 10 cổ lỗ sĩ nhưng được Red Hat cho miễn phí.

# podman images
REPOSITORY                                            TAG              IMAGE ID      CREATED      SIZE
registry.access.redhat.com/rhscl/postgresql-10-rhel7  latest           8301a77549cc  3 weeks ago  343 MB
docker.io/library/postgres                            16.2-bookworm    b9390dd1ea18  5 weeks ago  439 MB
docker.io/library/postgres                            16.2-alpine3.19  9a510ccf1de4  7 weeks ago  246 MB

Red Hat cũng đóng gói PostgreSQL 15 Container image để chạy trên bản Kubernetes của họ là OpenShift.

PostgreSQL 15 trên kho registry.redhat.io không miễn phí nên bạn cần mua mới có được.

# podman search postgresql
NAME                                                                    DESCRIPTION
registry.access.redhat.com/openshift3/postgresql-apb                    Ansible Playbook Bundle application definiti...
registry.access.redhat.com/openshift3/postgresql-92-rhel7               PostgreSQL 9.2 database server
registry.access.redhat.com/rhscl/postgresql-95-rhel7                    PostgreSQL server 9.5 for OpenShift and gene...
registry.access.redhat.com/rhscl/postgresql-96-rhel7                    PostgreSQL is an advanced Object-Relational...
registry.access.redhat.com/rhscl/postgresql-94-rhel7                    PostgreSQL 9.4 SQL database server
registry.access.redhat.com/cloudforms45/cfme-openshift-postgresql       Postgresql image that provides database serv...
registry.access.redhat.com/cloudforms46-beta/cfme-openshift-postgresql  Postgresql image that provides database serv...
registry.access.redhat.com/cloudforms46/cfme-openshift-postgresql       Postgresql image that provides database serv...
registry.access.redhat.com/cloudforms42/cfme-openshift-postgresql       Postgresql image that provides database serv...
registry.access.redhat.com/rhscl/postgresql-10-rhel7                    PostgreSQL is an advanced Object-Relational...
registry.access.redhat.com/cloudforms47/cfme-openshift-postgresql       CloudForms 4.7 APP image for OpenShift
registry.access.redhat.com/cloudforms45/cfme-openshift-app              Red Hat® CloudForms Appliance image to be u...
registry.access.redhat.com/cloudforms46-beta/cfme-openshift-app         Red Hat® CloudForms Appliance image to be u...
registry.access.redhat.com/cloudforms42/cfme-openshift-app              Red Hat® CloudForms Appliance image to be u...
registry.access.redhat.com/cloudforms46/cfme-openshift-app              Red Hat® CloudForms Appliance image to be u...
registry.redhat.io/openshift3/postgresql-apb                            Ansible Playbook Bundle application definiti...
registry.redhat.io/openshift3/postgresql-92-rhel7                       PostgreSQL 9.2 database server
registry.redhat.io/rhel8/postgresql-96                                  This container image provides a containerize...
registry.redhat.io/rhel8/postgresql-10                                  This container image provides a containerize...
registry.redhat.io/rhel8/postgresql-13                                  PostgreSQL is an advanced Object-Relational...
registry.redhat.io/rhel9/postgresql-13                                  rhcc_registry.access.redhat.com_rhel9/postgr...
registry.redhat.io/rhel8/postgresql-15                                  rhcc_registry.access.redhat.com_rhel8/postgr...
registry.redhat.io/rhel8/postgresql-12                                  PostgreSQL is an advanced Object-Relational...
registry.redhat.io/rhscl/postgresql-13-rhel7                            Postgresql 13 SQL database server
registry.redhat.io/rhel9/postgresql-15                                  rhcc_registry.access.redhat.com_rhel9/postgr...
registry.redhat.io/rhscl/postgresql-95-rhel7                            PostgreSQL server 9.5 for OpenShift and gene...
registry.redhat.io/openshift4/postgres-apb                              'Ansible Playbook Bundle application definit...
registry.redhat.io/openshift4/postgresql-apb                            'Ansible Playbook Bundle application definit...
registry.redhat.io/rhscl/postgresql-96-rhel7                            PostgreSQL is an advanced Object-Relational...
registry.redhat.io/rhscl/postgresql-94-rhel7                            PostgreSQL 9.4 SQL database server
registry.redhat.io/rhscl/postgresql-12-rhel7                            PostgreSQL is an advanced Object-Relational...
registry.redhat.io/cloudforms45/cfme-openshift-postgresql               Postgresql image that provides database serv...
registry.redhat.io/cloudforms46-beta/cfme-openshift-postgresql          Postgresql image that provides database serv...
registry.redhat.io/cloudforms46/cfme-openshift-postgresql               Postgresql image that provides database serv...
registry.redhat.io/cloudforms42/cfme-openshift-postgresql               Postgresql image that provides database serv...
registry.redhat.io/rhscl/postgresql-10-rhel7                            PostgreSQL is an advanced Object-Relational...
registry.redhat.io/cloudforms47/cfme-openshift-postgresql               CloudForms 4.7 APP image for OpenShift
registry.redhat.io/fuse7-tech-preview/fuse-postgres-exporter            'An exporter for Fuse Online metrics'
registry.redhat.io/fuse7/fuse-postgres-exporter-rhel8                   An exporter for Fuse Online metrics
registry.redhat.io/fuse7/fuse-postgres-exporter-rhel7                   An exporter for Fuse Online metrics
docker.io/library/postgres                                              The PostgreSQL object-relational database sy...
docker.io/bitnami/postgresql                                            Bitnami PostgreSQL Docker Image
docker.io/bitnami/postgresql-repmgr
docker.io/bitnamicharts/postgresql
docker.io/rapidfort/postgresql                                          RapidFort optimized, hardened image for Post...
docker.io/rapidfort/postgresql-official                                 RapidFort optimized, hardened image for Post...
docker.io/rapidfort/postgresql12-ib                                     RapidFort optimized, hardened image for Post...
docker.io/pachyderm/postgresql
docker.io/bitnamicharts/postgresql-ha
docker.io/vmware/postgresql
docker.io/vmware/postgresql-photon
docker.io/objectscale/postgresql-repmgr
docker.io/ckan/postgresql                                               **NOTE**: These images are deprecated and no...
docker.io/silintl/postgresql-backup-restore                             PostgreSQL backup/restore container
docker.io/rapidfort/postgresql-ib
docker.io/rapidfort/postgresql11-ib
docker.io/rapidfort/postgresql15-ib-alpine
docker.io/litmuschaos/postgresql-client
docker.io/silintl/postgresql-backup-restore-fs
docker.io/corpusops/postgresql                                          postgresql image
docker.io/circleci/postgres                                             The PostgreSQL object-relational database sy...
docker.io/malcolmnetsec/postgresql
docker.io/dhis2/postgresql-curl
docker.io/percona/percona-postgresql-operator                           Percona Distribution for PostgreSQL Operator
docker.io/greenbone/pg-gvm                                              PostgreSQL container image for the Greenbone...

DevOps lấy Container làm trung tâm và đóng gói phần mềm thành container nên 1 trong những việc khó nhất của DevOps là tạo ra Container ngon lành.

Thông thường thì tạo ra cái gì đó luôn khó hơn xài nó.

Giờ bạn hãy đọc hiểu 100% code Bash shell do Red Hat viết và Docker viết nha.

PostgreSQL Dockerfile chạy trên RHEL 9 được Red Hat viết ở đây.

https://catalog.redhat.com/software/containers/rhel9/postgresql-15/63f763f779eb1214c4d6fcf6?architecture=amd64&image=65e0af6ed6fed9d9cb59fffd&container-tabs=dockerfile

FROM ubi9/s2i-core:rhel9.3.0

# PostgreSQL image for OpenShift.
# Volumes:
#  * /var/lib/pgsql/data   - Database cluster for PostgreSQL
# Environment:
#  * $POSTGRESQL_USER     - Database user name
#  * $POSTGRESQL_PASSWORD - User's password
#  * $POSTGRESQL_DATABASE - Name of the database to create
#  * $POSTGRESQL_ADMIN_PASSWORD (Optional) - Password for the 'postgres'
#                           PostgreSQL administrative account

ENV POSTGRESQL_VERSION=15 \
    POSTGRESQL_PREV_VERSION=13 \
    HOME=/var/lib/pgsql \
    PGUSER=postgres \
    APP_DATA=/opt/app-root

ENV SUMMARY="PostgreSQL is an advanced Object-Relational database management system" \
    DESCRIPTION="PostgreSQL is an advanced Object-Relational database management system (DBMS). \
The image contains the client and server programs that you'll need to \
create, run, maintain and access a PostgreSQL DBMS server."

LABEL summary="$SUMMARY" \
      description="$DESCRIPTION" \
      io.k8s.description="$DESCRIPTION" \
      io.k8s.display-name="PostgreSQL 15" \
      io.openshift.expose-services="5432:postgresql" \
      io.openshift.tags="database,postgresql,postgresql15,postgresql-15" \
      io.openshift.s2i.assemble-user="26" \
      name="rhel9/postgresql-15" \
      com.redhat.component="postgresql-15-container" \
      version="1" \
      com.redhat.license_terms="https://www.redhat.com/en/about/red-hat-end-user-license-agreements#rhel" \
      usage="podman run -d --name postgresql_database -e POSTGRESQL_USER=user -e POSTGRESQL_PASSWORD=pass -e POSTGRESQL_DATABASE=db -p 5432:5432 rhel9/postgresql-15" \
      maintainer="SoftwareCollections.org <sclorg@redhat.com>"

EXPOSE 5432

COPY root/usr/libexec/fix-permissions /usr/libexec/fix-permissions

# This image must forever use UID 26 for postgres user so our volumes are
# safe in the future. This should *never* change, the last test is there
# to make sure of that.
RUN { yum -y module enable postgresql:15 || :; } && \
    INSTALL_PKGS="rsync tar gettext bind-utils nss_wrapper postgresql-server postgresql-contrib" && \
    INSTALL_PKGS="$INSTALL_PKGS pgaudit" && \
    yum -y --setopt=tsflags=nodocs install $INSTALL_PKGS && \
    rpm -V $INSTALL_PKGS && \
    postgres -V | grep -qe "$POSTGRESQL_VERSION\." && echo "Found VERSION $POSTGRESQL_VERSION" && \
    (yum -y reinstall tzdata || yum -y update tzdata ) && \
    yum -y clean all --enablerepo='*' && \
    localedef -f UTF-8 -i en_US en_US.UTF-8 && \
    test "$(id postgres)" = "uid=26(postgres) gid=26(postgres) groups=26(postgres)" && \
    mkdir -p /var/lib/pgsql/data && \
    /usr/libexec/fix-permissions /var/lib/pgsql /var/run/postgresql

# Get prefix path and path to scripts rather than hard-code them in scripts
ENV CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/postgresql \
    ENABLED_COLLECTIONS=

COPY root /
COPY ./s2i/bin/ $STI_SCRIPTS_PATH

# Not using VOLUME statement since it's not working in OpenShift Online:
# https://github.com/sclorg/httpd-container/issues/30
# VOLUME ["/var/lib/pgsql/data"]

# S2I permission fixes
# --------------------
# 1. unless specified otherwise (or - equivalently - we are in OpenShift), s2i
#    build process would be executed as 'uid=26(postgres) gid=26(postgres)'.
#    Such process wouldn't be able to execute the default 'assemble' script
#    correctly (it transitively executes 'fix-permissions' script).  So let's
#    add the 'postgres' user into 'root' group here
#
# 2. we call fix-permissions on $APP_DATA here directly (UID=0 during build
#    anyways) to assure that s2i process is actually able to _read_ the
#    user-specified scripting.
RUN usermod -a -G root postgres && \
    /usr/libexec/fix-permissions --read-only "$APP_DATA"

USER 26

ENTRYPOINT ["container-entrypoint"]
CMD ["run-postgresql"]

PostgreSQL Dockerfile chạy trên Debian 12 Bookworm được Docker viết ở đây.

https://github.com/docker-library/postgres/blob/ab6925051ca097d415816928a50c483ecc370c00/16/bookworm/Dockerfile

PostgreSQL Dockerfile chạy trên Alpine Linux 3.19 được Docker viết ở đây.

https://github.com/docker-library/postgres/blob/ab6925051ca097d415816928a50c483ecc370c00/16/alpine3.19/Dockerfile

Bài tập DevOps cho bạn hãy so sánh cách Red Hat và Docker đóng gói PostgreSQL thành container có gì khác nhau.