Skip to content

Commit 6a1b34f

Browse files
authored
Merge pull request #24 from cybertec-postgresql/reduceContainerSize
Reduce container size
2 parents f1cb621 + fb2061f commit 6a1b34f

12 files changed

Lines changed: 250 additions & 170 deletions

File tree

Makefile

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Define Default if Values not exist
33
BASE_IMAGE ?= rockylinux:9
44
BASEOS ?= rocky9
5+
CONTAINERIMAGE ?= rockylinux/rockylinux:9-ubi-micro
56
IMAGE_REPOSITORY ?= docker.io
67
IMAGE_PATH ?= cybertec-pg-container
78
CONTAINERSUITE ?= cybertec-pg-container
@@ -19,10 +20,7 @@ POSTGIS_IMAGE_TAG ?= $(BASEOS)-$(PGVERSION_FULL)-$(POSTGIS_VERSION)-$(BUILD)
1920

2021
# Settings for the Build-Process
2122
BUILDWITH ?= docker
22-
ROOTPATH = $(GOPATH)/src/github.com/cybertec/cybertec-pg-container
23-
ifndef ROOTPATH
24-
export ROOTPATH=$(GOPATH)/src/github.com/cybertec/cybertec-pg-container
25-
endif
23+
ROOTPATH ?= $(GOPATH)/src/github.com/cybertec/cybertec-pg-container
2624

2725
# Build Images
2826

@@ -48,18 +46,19 @@ base-build:
4846
base: base-build;
4947

5048
pgbackrest-build:
51-
docker build $(ROOTPATH) \
49+
docker build $(ROOTPATH) --no-cache \
5250
--file $(ROOTPATH)/docker/pgbackrest/Dockerfile \
5351
--tag cybertec-pg-container/pgbackrest:$(IMAGE_TAG)-$(BUILD) \
5452
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
53+
--build-arg CONTAINERIMAGE=${CONTAINERIMAGE} \
5554
--build-arg IMAGE_REPOSITORY=$(IMAGE_REPOSITORY) \
5655
--build-arg BASEOS=$(BASEOS) \
5756
--build-arg PACKAGER=$(PACKAGER) \
5857
--build-arg CONTAINERSUITE=$(CONTAINERSUITE) \
5958
--build-arg BUILD=$(BUILD) \
6059
--build-arg PGBACKREST_VERSION=$(PGBACKREST_VERSION) \
6160
--build-arg OLD_PG_VERSIONS="$(OLD_PG_VERSIONS)" \
62-
--build-arg PGVERSION
61+
--build-arg PGVERSION=$(PGVERSION)
6362

6463
pgbackrest: pgbackrest-build;
6564

@@ -68,6 +67,7 @@ postgres-build:
6867
--file $(ROOTPATH)/docker/postgres/Dockerfile \
6968
--tag cybertec-pg-container/postgres:$(IMAGE_TAG)-$(BETA)$(BUILD) \
7069
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
70+
--build-arg CONTAINERIMAGE=${CONTAINERIMAGE} \
7171
--build-arg IMAGE_REPOSITORY=$(IMAGE_REPOSITORY) \
7272
--build-arg BASEOS=$(BASEOS) \
7373
--build-arg PACKAGER=$(PACKAGER) \
@@ -86,6 +86,7 @@ postgres-stage-build:
8686
--file $(ROOTPATH)/docker/postgres-stage/Dockerfile \
8787
--tag cybertec-pg-container/postgres-stage:$(PGVERSION_FULL)-$(BETA)$(BUILD) \
8888
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
89+
--build-arg CONTAINERIMAGE=${CONTAINERIMAGE} \
8990
--build-arg IMAGE_REPOSITORY=$(IMAGE_REPOSITORY) \
9091
--build-arg BASEOS=$(BASEOS) \
9192
--build-arg PACKAGER=$(PACKAGER) \
@@ -103,6 +104,7 @@ postgres-gis-build:
103104
--file $(ROOTPATH)/docker/postgres-gis/Dockerfile \
104105
--tag cybertec-pg-container/postgres-gis:$(IMAGE_TAG)-$(BETA)$(BUILD) \
105106
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
107+
--build-arg CONTAINERIMAGE=${CONTAINERIMAGE} \
106108
--build-arg IMAGE_REPOSITORY=$(IMAGE_REPOSITORY) \
107109
--build-arg BASEOS=$(BASEOS) \
108110
--build-arg PACKAGER=$(PACKAGER) \
@@ -121,6 +123,7 @@ postgres-oracle-build:
121123
--file $(ROOTPATH)/docker/postgres-oracle/Dockerfile \
122124
--tag cybertec-pg-container/postgres-oracle:$(IMAGE_TAG)-$(BETA)$(BUILD) \
123125
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
126+
--build-arg CONTAINERIMAGE=${CONTAINERIMAGE} \
124127
--build-arg IMAGE_REPOSITORY=$(IMAGE_REPOSITORY) \
125128
--build-arg BASEOS=$(BASEOS) \
126129
--build-arg PACKAGER=$(PACKAGER) \
@@ -138,6 +141,7 @@ exporter-build:
138141
--file $(ROOTPATH)/docker/exporter/Dockerfile \
139142
--tag cybertec-pg-container/exporter:$(IMAGE_TAG)-$(BETA)$(BUILD) \
140143
--build-arg BASE_IMAGE=$(BASE_IMAGE) \
144+
--build-arg CONTAINERIMAGE=${CONTAINERIMAGE} \
141145
--build-arg IMAGE_REPOSITORY=$(IMAGE_REPOSITORY) \
142146
--build-arg BASEOS=$(BASEOS) \
143147
--build-arg PACKAGER=$(PACKAGER) \

docker/base/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ RUN ${PACKAGER} -y install --nodocs --noplugins --setopt=install_weak_deps=0 \
3535
RUN ${PACKAGER} -y config-manager --set-enabled crb
3636
#Add PostgreSQL-Repo to Base
3737
RUN ${PACKAGER} --nodocs --noplugins --setopt=install_weak_deps=0 install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-$(uname -i)/pgdg-redhat-repo-latest.noarch.rpm && ${PACKAGER} -y update && ${PACKAGER} -y clean all
38-
#RUN dnf -qy module disable postgresql
38+
RUN dnf -qy module disable postgresql
3939

4040
#Cleanup
4141

docker/exporter/Dockerfile

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,30 @@
11
ARG CONTAINERSUITE
22
ARG BUILD
33
ARG BASEOS
4+
ARG CONTAINERIMAGE
45

5-
FROM ${CONTAINERSUITE}/base:${BASEOS}-${BUILD}
6+
FROM ${CONTAINERSUITE}/base:${BASEOS}-${BUILD} AS builder
67

7-
# Dockerfile specific informations
88
ARG PACKAGER
9-
ARG BASEOS
10-
ARG PGBACKREST_VERSION
11-
ARG PG_MAJOR
12-
13-
RUN if [ "$BASEOS" = "ubi8" ] ; then \
14-
${PACKAGER} -y install --nodocs \
15-
shadow-utils \
16-
tar \
17-
bzip2 \
18-
lz4 \
19-
#crunchy-backrest-${BACKREST_VER} \
20-
&& ${PACKAGER} -y clean all ; \
21-
else \
22-
${PACKAGER} -y install --nodocs \
9+
10+
RUN ${PACKAGER} -y install --nodocs \
2311
--setopt=skip_missing_names_on_install=False \
24-
bzip2 \
25-
lz4 \
26-
nano \
2712
git \
2813
go \
2914
dumb-init \
30-
&& ${PACKAGER} -y clean all ; \
31-
fi
15+
&& ${PACKAGER} -y clean all ;
3216

3317
RUN git clone https://github.com/prometheus-community/postgres_exporter.git && cd postgres_exporter && make build
18+
19+
FROM ${CONTAINERIMAGE}
20+
COPY --from=builder /usr/bin/dumb-init /usr/bin/dumb-init
21+
COPY --from=builder ./postgres_exporter/postgres_exporter /bin/postgres_exporter
22+
3423
COPY launcher/exporter/launch.sh /
3524
COPY scripts/exporter/queries/ /postgres_exporter/queries
3625

26+
EXPOSE 9187
27+
3728
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
3829

3930
CMD ["/bin/sh", "/launch.sh", "init"]
40-
#CMD ["/bin/sh", "/scripts/postgres/promote.sh"]

docker/pgbackrest/Dockerfile

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,29 @@
11
ARG CONTAINERSUITE
22
ARG BUILD
33
ARG BASEOS
4+
ARG CONTAINERIMAGE
45

5-
FROM ${CONTAINERSUITE}/base:${BASEOS}-${BUILD}
6+
FROM ${CONTAINERSUITE}/base:${BASEOS}-${BUILD} AS builder
67

78
# Dockerfile specific informations
89
ARG PACKAGER
910
ARG PGBACKREST_VERSION
1011
ARG PGVERSION
1112
ARG OLD_PG_VERSIONS
12-
#ARG PG_SUPPORTED_VERSIONS="$PGVERSION"
1313
ARG PG_SUPPORTED_VERSIONS="$OLD_PG_VERSIONS $PGVERSION"
1414

15-
RUN if [ "$BASEOS" = "ubi8" ] ; then \
16-
${PACKAGER} -y install --nodocs \
17-
openssh-clients \
18-
openssh-server \
19-
shadow-utils \
20-
tar \
21-
bzip2 \
22-
lz4 \
23-
dumb-init \
24-
pgbackrest-${PGBACKREST_VERSION} \
25-
&& ${PACKAGER} -y clean all ; \
26-
else \
27-
${PACKAGER} -y install --nodocs \
15+
RUN ${PACKAGER} -y install --nodocs \
2816
--setopt=skip_missing_names_on_install=False \
2917
openssh-clients \
3018
openssh-server \
3119
bzip2 \
3220
lz4 \
33-
#postgresql${PGVERSION}-server \
21+
zstd \
22+
libicu \
3423
dumb-init \
24+
jq \
3525
pgbackrest-${PGBACKREST_VERSION} \
36-
&& ${PACKAGER} -y clean all ; \
37-
fi
26+
&& ${PACKAGER} -y clean all ;
3827

3928
# Install postgres-server
4029
RUN ${PACKAGER} -y update \
@@ -47,6 +36,55 @@ RUN ${PACKAGER} -y update \
4736
RUN rm /etc/pgbackrest.conf
4837
RUN rm -rf /var/spool/pgbackrest
4938

39+
# Add kubectl
40+
RUN curl -LO https://dl.k8s.io/release/v1.30.0/bin/linux/amd64/kubectl && chmod +x kubectl
41+
42+
FROM ${CONTAINERIMAGE} as micro
43+
44+
ARG PGVERSION
45+
46+
COPY --from=builder /usr/bin/dumb-init /usr/bin/dumb-init
47+
COPY --from=builder /etc/passwd /etc/passwd
48+
COPY --from=builder /etc/group /etc/group
49+
COPY --from=builder /usr/lib64 /usr/lib64
50+
# SSH-client
51+
COPY --from=builder /etc/ssh /etc/ssh
52+
COPY --from=builder /usr/bin/ssh /usr/bin/ssh
53+
COPY --from=builder /usr/libexec/openssh /usr/libexec/openssh
54+
# pgBackRest
55+
COPY --from=builder /usr/bin/pgbackrest /usr/bin/pgbackrest
56+
COPY --from=builder /usr/share/licenses/pgbackrest/LICENSE /usr/share/licenses/pgbackrest/LICENSE
57+
COPY --from=builder /var/lib/pgbackrest /var/lib/pgbackrest
58+
COPY --from=builder /var/log/pgbackrest /var/log/pgbackrest
59+
# Postgres
60+
COPY --from=builder /var/lib/pgsql /var/lib/pgsql
61+
COPY --from=builder ./kubectl /usr/local/bin/
62+
# lz4
63+
COPY --from=builder /usr/bin/lz4* /usr/bin/lz4*
64+
# zstd
65+
COPY --from=builder /usr/bin/zstd* /usr/bin/zstd*
66+
# Others
67+
COPY --from=builder /usr/bin/sed /usr/bin/sed
68+
COPY --from=builder /usr/bin/jq /usr/bin/jq
69+
COPY --from=builder /usr/bin/watch /usr/bin/watch
70+
COPY --from=builder /usr/share/locale /usr/share/locale
71+
COPY --from=builder /usr/lib /usr/lib
72+
COPY --from=builder /usr/lib64 /usr/lib64
73+
# PostgreSQL
74+
# TODO: implement a method that works with $PG_SUPPORTED_VERSIONS
75+
COPY --from=builder /usr/pgsql-13 /usr/pgsql-13
76+
COPY --from=builder /usr/pgsql-14 /usr/pgsql-14
77+
COPY --from=builder /usr/pgsql-15 /usr/pgsql-15
78+
COPY --from=builder /usr/pgsql-16 /usr/pgsql-16
79+
COPY --from=builder /var/lib/pgsql /var/lib/pgsql
80+
COPY --from=builder /usr/bin/pg_* /usr/bin/
81+
82+
# libraries
83+
COPY --from=builder /usr/bin/nss_wrapper.pl /usr/bin/nss_wrapper.pl
84+
# COPY --from=builder /usr/lib64/libnss_wrapper.so /usr/lib64/libnss_wrapper.so
85+
COPY --from=builder /usr/share/man/man1 /usr/share/man/man1
86+
COPY --from=builder /usr/bin/envsubst /usr/bin/envsubst
87+
5088
# add postgres user and group
5189
#RUN groupadd postgres -g 26 && useradd postgres -u 26 -g 26
5290

@@ -59,6 +97,9 @@ ADD scripts/pgbackrest/ /opt/pgbackrest/bin/
5997
# add pgbackrest-common files
6098
ADD /scripts/nss_wrapper /scripts/nss_wrapper
6199

100+
FROM scratch
101+
COPY --from=micro / /
102+
62103
# set user and group ownership
63104
RUN chown -R postgres:postgres /opt/pgbackrest \
64105
/backrestrepo /home/postgres/pgdata/pgbackrest /home/postgres/pgdata
@@ -71,9 +112,6 @@ RUN chmod -R g=u /etc/pgbackrest \
71112

72113
RUN mkdir /.ssh && chown postgres:postgres /.ssh && chmod o+rwx /.ssh
73114

74-
# Add kubectl
75-
RUN curl -LO https://dl.k8s.io/release/v1.26.0/bin/linux/amd64/kubectl && chmod +x kubectl && mv ./kubectl /usr/local/bin/
76-
77115
# set user and group ownership
78116
RUN chown -R postgres:postgres /opt/pgbackrest \
79117
/backrestrepo /home/postgres/pgdata
@@ -88,3 +126,4 @@ ENTRYPOINT ["/scripts/nss_wrapper/nss_wrapper.sh"]
88126
USER 26
89127

90128
CMD ["dumb-init", "/launch.sh", "init"]
129+

docker/postgres/Dockerfile

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
ARG CONTAINERSUITE
22
ARG BUILD
33
ARG BASEOS
4+
ARG PGVERSION
45

5-
FROM ${CONTAINERSUITE}/base:${BASEOS}-${BUILD}
6+
FROM ${CONTAINERSUITE}/base:${BASEOS}-${BUILD} as builder
67

78
# Dockerfile specific informations
89
ARG PACKAGER
@@ -109,13 +110,15 @@ RUN ETCDVERSION=3.5.13 \
109110

110111
ENV PATHBACKUP = $PATH
111112

113+
COPY runit-2.1.2.tar.gz /package/runit-2.1.2.tar.gz
114+
112115
# Install pam_oauth2.so
113116
#RUN #git clone -b $PAM_OAUTH2 --recurse-submodules https://github.com/zalando-pg/pam-oauth2.git \
114117
#&& make -C pam-oauth2 install \
115118
#&& curl -sL https://github.com/zalando-pg/bg_mon/archive/$BG_MON_COMMIT.tar.gz | tar xz \
116119
#&& curl -sL https://github.com/zalando-pg/pg_auth_mon/archive/$PG_AUTH_MON_COMMIT.tar.gz | tar xz \
117120

118-
RUN pip3 install 'PyYAML<6.0' setuptools pystache loader dumb-init kazoo meld3 boto python-etcd psutil requests --upgrade \
121+
RUN pip3 install 'PyYAML<6.0' setuptools pystache loader kazoo meld3 boto python-etcd psutil requests cdiff ydiff --upgrade \
119122
&& pip3 install patroni[kubernetes$EXTRAS]==$PATRONI_VERSION --upgrade \
120123
&& mkdir /usr/lib/postgresql \
121124
&& curl -sL https://github.com/cybertec-postgresql/pg_permissions/archive/$PG_PERMISSIONS_COMMIT.tar.gz | tar xz \
@@ -129,7 +132,7 @@ RUN pip3 install 'PyYAML<6.0' setuptools pystache loader dumb-init kazoo meld3 b
129132
#&& git clone https://github.com/pgq/pgqd.git \
130133
\
131134
&& ${PACKAGER} -y install --nodocs --noplugins --setopt=install_weak_deps=0 postgresql${PGVERSION} libevent-devel brotli-devel libbrotli \
132-
&& ${PACKAGER} -y clean all\
135+
&& ${PACKAGER} -y clean all \
133136
\
134137
# forbid creation of a main cluster when package is installed
135138
#&& sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf \
@@ -172,20 +175,34 @@ RUN pip3 install 'PyYAML<6.0' setuptools pystache loader dumb-init kazoo meld3 b
172175
&& cd /pgnodemx && make USE_PGXS=1 clean && make USE_PGXS=1 && make USE_PGXS=1 install; \
173176
#&& cd /pgqd && ./configure --prefix=/opt && make && make install \
174177
done \
175-
&& ${PACKAGER} -y clean all \
176-
&& rm -rf /pgextwlist /pg_stat_kcache /pgnodemx;
177-
178-
COPY runit-2.1.2.tar.gz /package/runit-2.1.2.tar.gz
179-
180-
RUN ${PACKAGER} -y install --nodocs --noplugins --setopt=install_weak_deps=0 glibc-devel glibc-static \
181-
&& ${PACKAGER} -y clean all \
182-
&& mkdir -p /package \
183-
&& cd /package && tar -xvzf runit-2.1.2.tar.gz && rm runit-2.1.2.tar.gz \
178+
&& ${PACKAGER} -y install --nodocs --noplugins --setopt=install_weak_deps=0 glibc-static \
179+
&& ${PACKAGER} -y clean all;
180+
181+
RUN cd /package && tar -xvzf runit-2.1.2.tar.gz && rm runit-2.1.2.tar.gz \
184182
&& cd admin/runit-2.1.2 && package/install \
185183
&& ln -s /usr/local/bin/runsvdir /usr/bin/runsvdir \
186-
&& ${PACKAGER} -y remove $(rpm -qa "*-devel") \
184+
&& rm -rf /pg_permissions* /pgextwlist /pg_stat_kcache /pgnodemx /timescaledb /set_user /pam-oauth2 \
185+
&& rm /etc/pgbackrest.conf && rm -rf /var/spool/pgbackrest \
186+
&& ${PACKAGER} -y remove $(rpm -qa "*devel*") python3-pip python3-wheel python3-dev python3-setuptools git patchutils flatpak glibc-static gcc glibc-devel \
187+
&& ${PACKAGER} -y autoremove \
188+
&& ${PACKAGER} -y clean dbcache \
187189
&& ${PACKAGER} -y clean all;
188190

191+
# RUN ${PACKAGER} -y install --nodocs --noplugins --setopt=install_weak_deps=0 glibc-devel glibc-static \
192+
# && ${PACKAGER} -y clean all \
193+
# && mkdir -p /package \
194+
# && cd /package && tar -xvzf runit-2.1.2.tar.gz && rm runit-2.1.2.tar.gz \
195+
# && cd admin/runit-2.1.2 && package/install \
196+
# && ln -s /usr/local/bin/runsvdir /usr/bin/runsvdir \
197+
# && ${PACKAGER} -y remove $(rpm -qa "*-devel") \
198+
# && ${PACKAGER} -y autoremove \
199+
# && ${PACKAGER} -y clean all;
200+
201+
FROM scratch
202+
203+
ARG PGVERSION
204+
205+
COPY --from=builder / /
189206

190207
EXPOSE 5432 8008 8080
191208
ENV LC_ALL=en_US.utf-8 \
@@ -247,26 +264,9 @@ RUN rm -rf /etc/service && mkdir /home/postgres/pgdata && chown -R postgres:post
247264
&& usermod -a -G root postgres; \
248265
fi
249266

250-
# Remove default pgbackrest-config and spool-path
251-
RUN rm /etc/pgbackrest.conf && rm -rf /var/spool/pgbackrest;
252267

253268
COPY scripts bootstrap major_upgrade /scripts/
254269
COPY launcher/postgres/launch.sh /
255-
#RUN curl -LO https://dl.k8s.io/release/v1.26.0/bin/linux/amd64/kubectl && chmod +x kubectl && mv ./kubectl /usr/local/bin/
256-
257-
# Cleanup
258-
RUN rm -rf /pgextwlist /pg_stat_kcache /oracle_fdw /pgnodemx /pg_permissions* /pg_profile* /pg_tm_aux* /timescaledb /set_user /pam-oauth2;
259-
260-
# Cleanup
261-
RUN for version in $PG_SUPPORTED_VERSIONS; do \
262-
${PACKAGER} -y remove postgresql${version}-devel; \
263-
done
264-
265-
# Clean Up Packages binutils glibc-devel glibc-static
266-
RUN ${PACKAGER} -y remove python3.11-pip python3.11-wheel python3.11-dev git patchutils flatpak glibc-static gcc glibc-devel \
267-
#&& ${PACKAGER} -y autoremove \
268-
&& ${PACKAGER} -y clean dbcache \
269-
&& ${PACKAGER} -y clean all;
270270

271271
ENTRYPOINT ["/scripts/nss_wrapper/nss_wrapper.sh"]
272272

launcher/exporter/launch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ do
1717
fi
1818
done
1919

20-
cd postgres_exporter && ./postgres_exporter --extend.query-path=/tmp/cpo_queries.yaml #--auto-discover-databases
20+
/bin/postgres_exporter --extend.query-path=/tmp/cpo_queries.yaml #--auto-discover-databases

0 commit comments

Comments
 (0)