Skip to content

Commit 772f662

Browse files
committed
chore: remove unnecessary entrypoint from the image
Signed-off-by: Niccolò Fei <[email protected]>
1 parent b012dd1 commit 772f662

File tree

2 files changed

+49
-499
lines changed

2 files changed

+49
-499
lines changed

Dockerfile

Lines changed: 49 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ LABEL name="PostgreSQL Container Images" \
3030
COPY 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 \
@@ -43,69 +42,39 @@ RUN set -ex; \
4342
libxslt1.1 \
4443
xz-utils \
4544
zstd \
46-
$(cat /build-deps.txt); \
47-
rm -rf /var/lib/apt/lists/*;
45+
$(cat /build-deps.txt) && \
46+
rm -rf /var/lib/apt/lists/*
4847

4948
# 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; \
49+
RUN groupadd -r postgres --gid=999 && \
50+
useradd -r -g postgres --uid=26 --home-dir=/var/lib/postgresql --shell=/bin/bash postgres && \
51+
mkdir -p /var/lib/postgresql && \
5752
chown -R postgres:postgres /var/lib/postgresql
5853

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-
8254
# 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 \
8555
# 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
56+
RUN if [ -f /etc/dpkg/dpkg.cfg.d/docker ]; then \
57+
grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker && \
58+
sed -ri '/\/usr\/share\/locale/d' /etc/dpkg/dpkg.cfg.d/docker && \
59+
! grep -q '/usr/share/locale' /etc/dpkg/dpkg.cfg.d/docker && \
60+
fi && \
61+
apt-get update && \
62+
apt-get install -y --no-install-recommends locales && \
63+
echo 'en_US.UTF-8 UTF-8' >> /etc/locale.gen && \
64+
locale-gen && \
65+
rm -rf /var/lib/apt/lists/*
66+
67+
ENV LANG=en_US.utf8
68+
ENV PG_MAJOR=$PG_MAJOR
69+
ENV PATH=/usr/lib/postgresql/$PG_MAJOR/bin:$PATH
10070

10171
# 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 ; \
72+
# Partially refer to https://github.com/docker-library/postgres/blob/master/16/alpine3.21/Dockerfile#L119-L159
73+
RUN mkdir -p /usr/src/postgresql && \
74+
git clone -b "$PG_BRANCH" --single-branch "$PG_REPO" /usr/src/postgresql && \
75+
cd /usr/src/postgresql && \
76+
export LLVM_CONFIG="/usr/lib/llvm-16/bin/llvm-config" && \
77+
export CLANG=clang-16 && \
10978
./configure \
11079
--build=x86_64-linux-gnu \
11180
--prefix=/usr \
@@ -151,107 +120,44 @@ RUN set -eux ; \
151120
LDFLAGS="-Wl,-z,relro -Wl,-z,now" \
152121
CPPFLAGS="-Wdate-time -D_FORTIFY_SOURCE=2" \
153122
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
123+
&& \
124+
make -j "$(nproc)" world-bin && \
125+
make install-world-bin && \
126+
rm -rf /usr/src/postgresql
160127

161128
# TODO: re-enable once https://github.com/pgaudit/pgaudit/issues/257 is fixed
162129
# Build PgAudit
163130
# 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 / ; \
131+
# RUN mkdir -p /usr/src/pgaudit && \
132+
# git clone -b main --single-branch https://github.com/pgaudit/pgaudit.git /usr/src/pgaudit && \
133+
# cd /usr/src/pgaudit && \
134+
# make install USE_PGXS=1 PG_CONFIG=/usr/lib/postgresql/$PG_MAJOR/bin/pg_config && \
170135
# rm -rf /usr/src/pgaudit
171136

172137
# 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"
138+
RUN apt-get purge -y --autoremove $(cat /build-deps.txt)
188139

189140
# 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; \
141+
RUN key='B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8' && \
142+
export GNUPGHOME="$(mktemp -d)" && \
143+
mkdir -p /usr/local/share/keyrings/ && \
144+
gpg --batch --keyserver keyserver.ubuntu.com --recv-keys "$key" && \
145+
gpg --batch --export --armor "$key" > /usr/local/share/keyrings/postgres.gpg.asc && \
146+
gpgconf --kill all && \
147+
rm -rf "$GNUPGHOME" && \
148+
aptRepo="[ signed-by=/usr/local/share/keyrings/postgres.gpg.asc ] http://apt.postgresql.org/pub/repos/apt/ bookworm-pgdg main $PG_MAJOR" && \
149+
echo "deb $aptRepo" > /etc/apt/sources.list.d/pgdg.list && \
150+
apt-get update && \
194151
apt-get install -y --no-install-recommends \
195152
python3-pip \
196153
python3-psycopg2 \
197154
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
155+
&& \
156+
pip3 install --break-system-packages --upgrade pip && \
157+
pip3 install --break-system-packages barman[cloud,azure,snappy,google] boto3==1.35.99 && \
158+
rm -rf /var/lib/apt/lists/*
217159

218160
# DoD 2.3 - remove setuid/setgid from any binary that not strictly requires it, and before doing that list them on the stdout
219161
RUN find / -not -path "/proc/*" -perm /6000 -type f -exec ls -ld {} \; -exec chmod a-s {} \; || true
220162

221163
USER 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

Comments
 (0)