@@ -30,82 +30,38 @@ LABEL name="PostgreSQL Container Images" \
3030COPY build-deps.txt /
3131
3232# Install runtime and build dependencies
33- RUN set -ex; \
34- apt-get update; \
33+ RUN apt-get update && \
3534 apt-get install -y --no-install-recommends \
3635 gnupg \
3736 dirmngr \
3837 ca-certificates \
38+ locales-all \
3939 ssl-cert \
4040 libnss-wrapper \
4141 libxml2 \
4242 libllvm16 \
4343 libxslt1.1 \
4444 xz-utils \
4545 zstd \
46- $(cat /build-deps.txt); \
47- rm -rf /var/lib/apt/lists/*;
46+ $(cat /build-deps.txt) && \
47+ rm -rf /var/lib/apt/lists/* /var/cache/* /var/log/*
4848
4949# explicitly set user/group IDs
50- RUN set -eux; \
51- groupadd -r postgres --gid=999; \
52- # https://salsa.debian.org/postgresql/postgresql-common/blob/997d842ee744687d99a2b2d95c1083a2615c79e8/debian/postgresql-common.postinst#L32-35
53- useradd -r -g postgres --uid=26 --home-dir=/var/lib/postgresql --shell=/bin/bash postgres; \
54- # also create the postgres user's home directory with appropriate permissions
55- # see https://github.com/docker-library/postgres/issues/274
56- mkdir -p /var/lib/postgresql; \
50+ RUN groupadd -r postgres --gid=999 && \
51+ useradd -r -g postgres --uid=26 --home-dir=/var/lib/postgresql --shell=/bin/bash postgres && \
52+ mkdir -p /var/lib/postgresql && \
5753 chown -R postgres:postgres /var/lib/postgresql
5854
59- # grab gosu for easy step-down from root
60- # https://github.com/tianon/gosu/releases
61- ENV GOSU_VERSION 1.17
62- RUN set -eux; \
63- savedAptMark="$(apt-mark showmanual)" ; \
64- apt-get update; \
65- apt-get install -y --no-install-recommends ca-certificates wget; \
66- rm -rf /var/lib/apt/lists/*; \
67- dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')" ; \
68- wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch" ; \
69- wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch.asc" ; \
70- export GNUPGHOME="$(mktemp -d)" ; \
71- gpg --batch --keyserver hkps://keys.openpgp.org --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4; \
72- gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu; \
73- gpgconf --kill all; \
74- rm -rf "$GNUPGHOME" /usr/local/bin/gosu.asc; \
75- apt-mark auto '.*' > /dev/null; \
76- [ -z "$savedAptMark" ] || apt-mark manual $savedAptMark > /dev/null; \
77- apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false; \
78- chmod +x /usr/local/bin/gosu; \
79- gosu --version; \
80- gosu nobody true
81-
82- # make the "en_US.UTF-8" locale so postgres will be utf-8 enabled by default
83- RUN set -eux; \
84- if [ -f /etc/dpkg/dpkg.cfg.d/docker ]; then \
85- # if this file exists, we're likely in "debian:xxx-slim", and locales are thus being excluded so we need to remove that exclusion (since we need locales)
86- grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker; \
87- sed -ri '/\/ usr\/ share\/ locale/d' /etc/dpkg/dpkg.cfg.d/docker; \
88- ! grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker; \
89- fi; \
90- apt-get update; apt-get install -y --no-install-recommends locales; rm -rf /var/lib/apt/lists/*; \
91- echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen; \
92- locale-gen; \
93- locale -a | grep 'en_US.utf8'
94- ENV LANG en_US.utf8
95-
96- RUN mkdir /docker-entrypoint-initdb.d
97-
98- ENV PG_MAJOR $PG_MAJOR
99- ENV PATH $PATH:/usr/lib/postgresql/$PG_MAJOR/bin
55+ ENV PG_MAJOR=$PG_MAJOR
56+ ENV PATH=/usr/lib/postgresql/$PG_MAJOR/bin:$PATH
10057
10158# Build PostgreSQL
102- # Partially refer to https://github.com/docker-library/postgres/blob/master/16/alpine3.19/Dockerfile#L33-L160
103- RUN set -eux ; \
104- mkdir -p /usr/src/postgresql ; \
105- git clone -b "$PG_BRANCH" --single-branch "$PG_REPO" /usr/src/postgresql ; \
106- cd /usr/src/postgresql ; \
107- export LLVM_CONFIG="/usr/lib/llvm-16/bin/llvm-config" ; \
108- export CLANG=clang-16 ; \
59+ # Partially refer to https://github.com/docker-library/postgres/blob/master/16/alpine3.21/Dockerfile#L119-L159
60+ RUN mkdir -p /usr/src/postgresql && \
61+ git clone -b "$PG_BRANCH" --single-branch "$PG_REPO" /usr/src/postgresql && \
62+ cd /usr/src/postgresql && \
63+ export LLVM_CONFIG="/usr/lib/llvm-16/bin/llvm-config" && \
64+ export CLANG=clang-16 && \
10965 ./configure \
11066 --build=x86_64-linux-gnu \
11167 --prefix=/usr \
@@ -151,107 +107,44 @@ RUN set -eux ; \
151107 LDFLAGS="-Wl,-z,relro -Wl,-z,now" \
152108 CPPFLAGS="-Wdate-time -D_FORTIFY_SOURCE=2" \
153109 CXXFLAGS="-g -Og -fstack-protector-strong -Wformat -Werror=format-security" \
154- ; \
155- make -j "$(nproc)" world-bin ; \
156- make install-world-bin ; \
157- cd / ; \
158- rm -rf /usr/src/postgresql ; \
159- postgres --version
110+ && \
111+ make -j "$(nproc)" world-bin && \
112+ make install-world-bin && \
113+ rm -rf /usr/src/postgresql
160114
161115# TODO: re-enable once https://github.com/pgaudit/pgaudit/issues/257 is fixed
162116# Build PgAudit
163117# See to https://github.com/pgaudit/pgaudit/blob/master/README.md#compile-and-install
164- # RUN set -eux ; \
165- # mkdir -p /usr/src/pgaudit ; \
166- # git clone -b main --single-branch https://github.com/pgaudit/pgaudit.git /usr/src/pgaudit ; \
167- # cd /usr/src/pgaudit ; \
168- # make install USE_PGXS=1 PG_CONFIG=/usr/lib/postgresql/$PG_MAJOR/bin/pg_config ; \
169- # cd / ; \
118+ # RUN mkdir -p /usr/src/pgaudit && \
119+ # git clone -b main --single-branch https://github.com/pgaudit/pgaudit.git /usr/src/pgaudit && \
120+ # cd /usr/src/pgaudit && \
121+ # make install USE_PGXS=1 PG_CONFIG=/usr/lib/postgresql/$PG_MAJOR/bin/pg_config && \
170122# rm -rf /usr/src/pgaudit
171123
172124# Purge build dependencies
173- RUN set -xe ; \
174- apt-get purge -y --autoremove $(cat /build-deps.txt)
175-
176- # Even though we compile from source, we still need PGDG to gather an updated version of psycopg2
177- RUN set -ex; \
178- # pub 4096R/ACCC4CF8 2011-10-13 [expires: 2019-07-02]
179- # Key fingerprint = B97B 0AFC AA1A 47F0 44F2 44A0 7FCC 7D46 ACCC 4CF8
180- # uid PostgreSQL Debian Repository
181- key='B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8' ; \
182- export GNUPGHOME="$(mktemp -d)" ; \
183- mkdir -p /usr/local/share/keyrings/; \
184- gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key" ; \
185- gpg --batch --export --armor "$key" > /usr/local/share/keyrings/postgres.gpg.asc; \
186- gpgconf --kill all; \
187- rm -rf "$GNUPGHOME"
125+ RUN apt-get purge -y --autoremove $(cat /build-deps.txt)
188126
189127# Install barman-cloud
190- RUN set -xe; \
191- aptRepo="[ signed-by=/usr/local/share/keyrings/postgres.gpg.asc ] http://apt.postgresql.org/pub/repos/apt/ bookworm-pgdg main $PG_MAJOR" ; \
192- echo "deb $aptRepo" > /etc/apt/sources.list.d/pgdg.list; \
193- apt-get update; \
128+ RUN key='B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8' && \
129+ export GNUPGHOME="$(mktemp -d)" && \
130+ mkdir -p /usr/local/share/keyrings/ && \
131+ gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key" && \
132+ gpg --batch --export --armor "$key" > /usr/local/share/keyrings/postgres.gpg.asc && \
133+ gpgconf --kill all && \
134+ rm -rf "$GNUPGHOME" && \
135+ aptRepo="[ signed-by=/usr/local/share/keyrings/postgres.gpg.asc ] http://apt.postgresql.org/pub/repos/apt/ bookworm-pgdg main $PG_MAJOR" && \
136+ echo "deb $aptRepo" > /etc/apt/sources.list.d/pgdg.list && \
137+ apt-get update && \
194138 apt-get install -y --no-install-recommends \
195139 python3-pip \
196140 python3-psycopg2 \
197141 python3-setuptools \
198- ; \
199- pip3 install --break-system-packages --upgrade pip; \
200- pip3 install --break-system-packages barman[cloud,azure,snappy,google] boto3==1.35.99; \
201- rm -rf /var/lib/apt/lists/*;
202-
203- # make the sample config easier to munge (and "correct by default")
204- RUN set -eux; \
205- dpkg-divert --add --rename --divert "/usr/share/postgresql/postgresql.conf.sample.dpkg" "/usr/share/postgresql/$PG_MAJOR/postgresql.conf.sample" ; \
206- cp -v /usr/share/postgresql/postgresql.conf.sample.dpkg /usr/share/postgresql/postgresql.conf.sample; \
207- ln -sv ../postgresql.conf.sample "/usr/share/postgresql/$PG_MAJOR/" ; \
208- sed -ri "s!^#?(listen_addresses)\s *=\s *\S +.*!\1 = '*'!" /usr/share/postgresql/postgresql.conf.sample; \
209- grep -F "listen_addresses = '*'" /usr/share/postgresql/postgresql.conf.sample
210-
211- RUN mkdir -p /var/run/postgresql && chown -R postgres:postgres /var/run/postgresql && chmod 3777 /var/run/postgresql
212-
213- ENV PGDATA /var/lib/postgresql/data
214- # this 1777 will be replaced by 0700 at runtime (allows semi-arbitrary "--user" values)
215- RUN mkdir -p "$PGDATA" && chown -R postgres:postgres "$PGDATA" && chmod 1777 "$PGDATA"
216- VOLUME /var/lib/postgresql/data
142+ && \
143+ pip3 install --break-system-packages --upgrade pip && \
144+ pip3 install --break-system-packages barman[cloud,azure,snappy,google] boto3==1.35.99 && \
145+ rm -rf /var/lib/apt/lists/* /var/cache/* /var/log/*
217146
218147# DoD 2.3 - remove setuid/setgid from any binary that not strictly requires it, and before doing that list them on the stdout
219148RUN find / -not -path "/proc/*" -perm /6000 -type f -exec ls -ld {} \; -exec chmod a-s {} \; || true
220149
221150USER 26
222-
223- COPY docker-entrypoint.sh /usr/local/bin/
224- ENTRYPOINT ["docker-entrypoint.sh" ]
225-
226- # We set the default STOPSIGNAL to SIGINT, which corresponds to what PostgreSQL
227- # calls "Fast Shutdown mode" wherein new connections are disallowed and any
228- # in-progress transactions are aborted, allowing PostgreSQL to stop cleanly and
229- # flush tables to disk, which is the best compromise available to avoid data
230- # corruption.
231- #
232- # Users who know their applications do not keep open long-lived idle connections
233- # may way to use a value of SIGTERM instead, which corresponds to "Smart
234- # Shutdown mode" in which any existing sessions are allowed to finish and the
235- # server stops when all sessions are terminated.
236- #
237- # See https://www.postgresql.org/docs/12/server-shutdown.html for more details
238- # about available PostgreSQL server shutdown signals.
239- #
240- # See also https://www.postgresql.org/docs/12/server-start.html for further
241- # justification of this as the default value, namely that the example (and
242- # shipped) systemd service files use the "Fast Shutdown mode" for service
243- # termination.
244- #
245- STOPSIGNAL SIGINT
246- #
247- # An additional setting that is recommended for all users regardless of this
248- # value is the runtime "--stop-timeout" (or your orchestrator/runtime's
249- # equivalent) for controlling how long to wait between sending the defined
250- # STOPSIGNAL and sending SIGKILL (which is likely to cause data corruption).
251- #
252- # The default in most runtimes (such as Docker) is 10 seconds, and the
253- # documentation at https://www.postgresql.org/docs/12/server-start.html notes
254- # that even 90 seconds may not be long enough in many instances.
255-
256- EXPOSE 5432
257- CMD ["postgres" ]
0 commit comments