Search code examples
postgresqldockerarm64apple-silicon

Using x86 PostgreSQL Data on Arm64v8 / Apple Silicon


In our project, we're creating a nightly "dev db" posgresql docker image, currently based on postgres:12.13-alpine

This image contains quite a large database and the base image is x86.

I now have a new mac with apple silicon. While I can run the x86 image, I only get subpar performance using docker desktop + rosetta.

I have created this Dockerfile to create an arm64 image and copy over the data directory. While the database starts up and the tables are all there, I only get garbled data.

Is there any way to "copy over" the data without doing a full dump + restore?

Creating the x86 image already takes multiple hours on our CI, so I was hoping to not spend the same amount of time creating an arm64 image, especially as I'm currently the only arm64 user.

Here's the Dockerfile I created:

ARG BASE=image-registry.apps.os-test.acme.inet/acme-dev/devdb 
ARG TAG=2023-11-27_amd64
ARG ORIGINAL="${BASE}:${TAG}"

# Create a temporary container from the existing amd64 image to extract the data
FROM --platform=linux/amd64 ${ORIGINAL} as amd64image

# Use the arm64/arm64v8 version of the PostgreSQL image as the base image
FROM arm64v8/postgres:12

ARG PG_POSTGRES_PWD=UShouldKnowBetter

ENV POSTGRES_DB postgres
ENV POSTGRES_USER postgres
ENV POSTGRES_PASSWORD ${PG_POSTGRES_PWD}
ENV PGDATA /pgdata

# Create a directory to copy data from the amd64 image
RUN mkdir -p /docker-entrypoint-initdb.d /pgdata

# Copy the data from the amd64 image to the arm64 image
COPY --from=amd64image /docker-entrypoint-initdb.d /docker-entrypoint-initdb.d
COPY --from=amd64image /var/lib/postgresql /var/lib/postgresql
COPY --from=amd64image /pgdata /pgdata

#RUN echo "de_DE.utf8" >> /etc/locale.gen && locale-gen && grep -vwE "(lc_messages|lc_monetary|lc_numeric|lc_time)" /pgdata/postgresql.conf > /pgdata/postgresql.conf.new && \
#  cp /pgdata/postgresql.conf.new /pgdata/postgresql.conf && chown -R postgres:postgres /pgdata && chmod 0750 -R /pgdata
RUN echo "de_DE.utf8 UTF-8" >> /etc/locale.gen && locale-gen && chown -R postgres:postgres /pgdata && chmod 0750 -R /pgdata

HEALTHCHECK --interval=10s --timeout=30s --start-period=5s --retries=3 \
  CMD pg_isready -U postgres -d postgres

Any help appreciated!


Solution

  • No, if the architectures differ, dump and restore is your only option.