Skip to content

Commit bffd097

Browse files
committed
Use data dir and remove /tmp/pg_* caches (#100)
Go back to using `/var/lib/postgresql/data` instead of `/var/lib/postgresql/tembo` because, although it made it compatible with stock CNPG, it was incompatible with Tembo Cloud, which also relies on CNPG, so it would never be mounted. It means the image will no longe be compatible with stock CNPG, sadly, because `/var/lib/postgresql/data/share` is still the shared directory, and CNPG will wipe it out when it mounts the `pgdata` volume at `/var/lib/postgresql/data`. Also remove the copying of the contents of `/var/lib/postgresql/data` to `/tmp/pg_sharedir` and `/tmp/pg_pkglibdir` from all images. As of tembo-io/tembo#1185, the init pod now copies files directly from `/var/lib/postgresql/data`.
1 parent 99daed0 commit bffd097

File tree

7 files changed

+33
-75
lines changed

7 files changed

+33
-75
lines changed

Diff for: CONTAINER_README.md

+6-15
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,20 @@ live here in `/var/lib/postgresql`. Here's a description of subdirectories and
66
their purposes:
77

88
* `data`: The directory in which the Docker entrypoint initializes
9-
a cluster and which [CloudNativePG] mounts as a persistent volume.
9+
a cluster and which [Tembo Cloud] mounts as a persistent volume.
1010
* `data/pgdata`: The data directory initialized by the Docker entrypoint and
11-
[CloudNativePG].
12-
* `tembo`: The directory that [Tembo Cloud] mounts as the persistent volume
13-
for the database and extensions.
14-
* `tembo/pgdata`: The data directory initialized by [Tembo Cloud].
15-
* `tembo/lib`: A directory for shared library files required by extensions.
16-
* `tembo/mod`: A directory for extension module library files.
17-
* `tembo/share`: The directory for architecture-independent support files
11+
[Tembo Cloud].
12+
* `data/lib`: A directory for shared library files required by extensions.
13+
* `data/mod`: A directory for extension module library files.
14+
* `data/share`: The directory for architecture-independent support files
1815
used by Postgres. This is the directory used by `pg_config --sharedir` to
19-
install non-binary extension files. Its files are copied from
20-
`/tmp/pg_sharedir` when the Tembo operator initializes the volume.
21-
16+
install non-binary extension files.
2217

2318
Other useful locations around the system:
2419

2520
* `/usr/lib/postgresql`: The base directory for Postgres itself.
2621
* `/usr/local/bin/docker-entrypoint.sh`: The docker entrypoint script, which
2722
initializes and starts a Postgres cluster in `/var/lib/postgresql/data/pgdata`.
28-
* `/etc/ld.so.conf.d/postgres.conf`: Configures the library loader to look
29-
for library files in the Postgres library directory (`pg_config --libdir`).
30-
* `/etc/ld.so.conf.d/tembo.conf`: Configures the library loader to look
31-
for library files in `/var/lib/postgresql/tembo/${OS_NAME}/lib`.
3223

3324
[CloudNativePG]: https://cloudnative-pg.io
3425
[Tembo Cloud]: https://tembo.io/docs/product/cloud/overview "Tembo Cloud Overview"

Diff for: Dockerfile

+10-16
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ ARG PG_VERSION
55
ARG PG_MAJOR=${PG_VERSION%%.*}
66
ARG PG_PREFIX=/usr/lib/postgresql
77
ARG PG_HOME=/var/lib/postgresql
8-
ARG CNPG_VOLUME=${PG_HOME}/data
8+
ARG DATA_VOLUME=${PG_HOME}/data
99

1010
# Tembo-specific volume mount, sharedir, dynamic_library_path target, and
1111
# System LLD dir.
12-
ARG TEMBO_VOLUME=${PG_HOME}/tembo
13-
ARG TEMBO_SHARE_DIR=${TEMBO_VOLUME}/share
14-
ARG TEMBO_PG_LIB_DIR=${TEMBO_VOLUME}/mod
15-
ARG TEMBO_LD_LIB_DIR=${TEMBO_VOLUME}/lib
12+
ARG TEMBO_SHARE_DIR=${DATA_VOLUME}/share
13+
ARG TEMBO_PG_MOD_DIR=${DATA_VOLUME}/mod
14+
ARG TEMBO_LD_LIB_DIR=${DATA_VOLUME}/lib
1615

1716
# Set rpath to search the Postgres lib directory, then the Tembo Postgres lib
1817
# directory, where Trunk-installed extension libraries will live, and the
1918
# Tembo OS lib directory, where Trunk-installed third-party libraries will
2019
# live. https://lekensteyn.nl/rpath.html
21-
ARG TEMBO_RPATH=${PG_PREFIX}/lib:${TEMBO_PG_LIB_DIR}:${TEMBO_LD_LIB_DIR}
20+
ARG TEMBO_RPATH=${PG_PREFIX}/lib:${TEMBO_PG_MOD_DIR}:${TEMBO_LD_LIB_DIR}
2221

2322
##############################################################################
2423
# Build trunk.
@@ -107,7 +106,7 @@ RUN set -ex; \
107106
##############################################################################
108107
# Install additional stuff for the dev image.
109108
FROM build AS dev-install
110-
ARG PG_PREFIX PG_HOME TEMBO_LD_LIB_DIR TEMBO_PG_LIB_DIR CNPG_VOLUME
109+
ARG PG_PREFIX PG_HOME TEMBO_LD_LIB_DIR TEMBO_PG_MOD_DIR
111110

112111
ENV DEBIAN_FRONTEND=noninteractive
113112
RUN set -ex; \
@@ -121,7 +120,7 @@ RUN set -ex; \
121120
apt-get clean -y; \
122121
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8; \
123122
rm -rf /var/lib/apt/lists/* /var/cache/* /var/log/*; \
124-
mkdir -p "${TEMBO_LD_LIB_DIR}" "${TEMBO_PG_LIB_DIR}" "${CNPG_VOLUME}"; \
123+
mkdir -p "${TEMBO_LD_LIB_DIR}" "${TEMBO_PG_MOD_DIR}"; \
125124
groupadd -r postgres --gid=999 && \
126125
useradd -r -g postgres --uid=26 --home-dir=${PG_HOME} --shell=/bin/bash postgres && \
127126
chown -R postgres:postgres ${PG_HOME};
@@ -144,7 +143,7 @@ ENTRYPOINT ["docker-entrypoint.sh"]
144143
##############################################################################
145144
# Install the dependencies necessary for the base image.
146145
FROM ${BASE} AS install
147-
ARG CNPG_VOLUME PACKAGES TEMBO_LD_LIB_DIR TEMBO_PG_LIB_DIR PG_PREFIX PG_HOME
146+
ARG PACKAGES TEMBO_LD_LIB_DIR TEMBO_PG_MOD_DIR PG_PREFIX PG_HOME
148147

149148
# Copy the PostgreSQL files and trunk.
150149
COPY --link --from=build --parents /var/lib/./postgresql /var/lib/
@@ -173,14 +172,9 @@ RUN apt-get update && apt-get upgrade -y && apt-get install --no-install-recomme
173172
ENV PATH=${PG_PREFIX}/bin:$PATH
174173
RUN set -xe; \
175174
apt-get clean -y; \
176-
# Set up en_US.UTF-8
177175
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8; \
178-
mkdir -p "${TEMBO_LD_LIB_DIR}" "${TEMBO_PG_LIB_DIR}" "${CNPG_VOLUME}"; \
179-
rm -rf /var/lib/apt/lists/* /var/cache/* /var/log/*; \
180-
# Stash away sharedir and etc so the Tembo operator can copy them back
181-
# when it initializes the pod. (We should be able to do it during pod
182-
# initialization without a temp copy: https://stackoverflow.com/a/72269316/79202)
183-
cp -lr "$(pg_config --sharedir)" /tmp/pg_sharedir;
176+
mkdir -p "${TEMBO_LD_LIB_DIR}" "${TEMBO_PG_MOD_DIR}"; \
177+
rm -rf /var/lib/apt/lists/* /var/cache/* /var/log/*;
184178

185179
# Add the README and entrypoint script.
186180
COPY CONTAINER_README.md "${PG_HOME}/README.md"

Diff for: README.md

+15-22
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,6 @@ Ubuntu Noble (24.04) and Jimmy (22.04) for the ARM64 and AMD64 processors.
1414
docker exec -it tembo-postgres psql
1515
```
1616

17-
* Runs in [CloudNativePG]; just set the `imageName` key in the `spec`
18-
section of the `Cluster` manifest:
19-
20-
```yaml
21-
apiVersion: postgresql.cnpg.io/v1
22-
kind: Cluster
23-
metadata:
24-
# [...]
25-
spec:
26-
imageName: quay.io/tembo/postgres:17
27-
#[...]
28-
```
29-
3017
* Based on the latest and previous LTS Ubuntu [Ubuntu Linux], currently
3118
24.04 LTS "Noble Numbat" and 22.04 LTS "Jammy Jellyfish".
3219

@@ -41,10 +28,13 @@ Ubuntu Noble (24.04) and Jimmy (22.04) for the ARM64 and AMD64 processors.
4128
## Building
4229

4330
The easiest way to build and load the `postgres` and `postgres-dev` images
44-
into Docker is:
31+
into Docker is to set up a temporary registry, push to it, then pull from it:
4532

4633
```sh
47-
arch="$(uname -m)" pg_version=17.4 docker buildx bake --load
34+
docker run -d -p 5001:5000 --restart=always --name registry registry:2
35+
registry=localhost:5001 arch="$(uname -m)" pg_version=17.4 docker buildx bake --push
36+
docker pull localhost:5001/postgres
37+
docker pull localhost:5001/postgres-dev
4838
```
4939

5040
Set these environment variables to modify the build behavior:
@@ -152,12 +142,9 @@ And an image built on the latest Postgres includes the tag:
152142
* `/var/lib/postgresql`: The home directory for the `postgres` user where
153143
all the potentially persistent data files and libraries live.
154144

155-
* `/var/lib/postgresql/data`: The default data directory where the Docker
156-
entrypoint script and [CloudNativePG] store the data files in a `pgdata`
157-
subdirectory. Mount a volume to this directory for data persistence.
158-
159-
* `/var/lib/postgresql/tembo`: The directory where [Tembo Cloud] mounts a
160-
persistent volume and stores persistent data:
145+
* `/var/lib/postgresql/data`: The directory where the Docker entrypoint
146+
script initializes the database in the `pgdata` subdirectory, and where
147+
[Tembo Cloud] mounts a persistent volume and stores persistent data:
161148
* `pgdata`: Tembo initializes and runs the cluster from this
162149
subdirectory.
163150
* `mod`: Tembo stores extension module libraries this subdirectory.
@@ -166,7 +153,13 @@ And an image built on the latest Postgres includes the tag:
166153
subdirectory.
167154

168155
* `/usr/lib/postgresql`: The home of the PostgreSQL binaries, libraries, and
169-
header & locale files. Immutable in [CloudNativePG] and [Tembo Cloud].
156+
header & locale files. Immutable in [Tembo Cloud].
157+
158+
### Cloud Native Postgres Support
159+
160+
Unfortunately, this image does not work on [CloudNativePG]. This is because it
161+
currently stores files in `/var/lib/postgresql/data` that [CloudNativePG]
162+
masks when it mounts the directory as a volume.
170163

171164
## Tasks
172165

Diff for: geo-cnpg/Dockerfile

+1-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ RUN /usr/bin/trunk install postgis --version 3.5.0 \
4545
&& make -C MobilityDB/build -j8 install \
4646
&& rm -rf MobilityDB
4747

48-
# cache all extensions
49-
RUN set -eux; \
50-
ldconfig; \
51-
cp -r $(pg_config --pkglibdir)/* /tmp/pg_pkglibdir; \
52-
cp -r $(pg_config --sharedir)/* /tmp/pg_sharedir;
48+
RUN ldconfig
5349

5450
ENV LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
5551
ENV XDG_CACHE_HOME=/var/lib/postgresql/data/tembo/.cache

Diff for: ml-cnpg/Dockerfile

-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ RUN set -eux; \
1919
trunk install jsonb_plpython3u; \
2020
trunk install ltree_plpython3u;
2121

22-
# cache all extensions
23-
RUN set -eux; \
24-
cp -r $(pg_config --pkglibdir)/* /tmp/pg_pkglibdir; \
25-
cp -r $(pg_config --sharedir)/* /tmp/pg_sharedir;
26-
2722
# Revert the postgres user to id 26
2823
RUN usermod -u 26 postgres
2924
USER 26

Diff for: standard-cnpg/Dockerfile

+1-5
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,7 @@ RUN set -eux; \
122122
ldconfig; \
123123
# Install auto_explain and pg_stat_statements.
124124
/usr/bin/trunk install auto_explain; \
125-
/usr/bin/trunk install pg_stat_statements; \
126-
# cache pg_stat_statements and auto_explain and pg_stat_kcache to temp directory
127-
mkdir /tmp/pg_pkglibdir /tmp/pg_sharedir; \
128-
cp -r $(pg_config --pkglibdir)/* /tmp/pg_pkglibdir; \
129-
cp -r $(pg_config --sharedir)/* /tmp/pg_sharedir
125+
/usr/bin/trunk install pg_stat_statements;
130126

131127
# Revert the postgres user to id 26
132128
RUN usermod -u 26 postgres

Diff for: tembo-pg-cnpg/Dockerfile

-7
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ RUN trunk install pg_stat_statements
3939
# Install auto_explain
4040
RUN trunk install auto_explain
4141

42-
# cache pg_stat_statements and auto_explain and pg_stat_kcache to temp directory
43-
RUN set -eux; \
44-
mkdir /tmp/pg_pkglibdir; \
45-
mkdir /tmp/pg_sharedir; \
46-
cp -r $(pg_config --pkglibdir)/* /tmp/pg_pkglibdir; \
47-
cp -r $(pg_config --sharedir)/* /tmp/pg_sharedir
48-
4942
# Revert the postgres user to id 26
5043
RUN usermod -u 26 postgres
5144
USER 26

0 commit comments

Comments
 (0)