Install older package version in Alpine
Asked Answered
I

4

6

So recently (5th September) the Alpine Linux package repo was updated to postgresql-client 12.4

I'm referencing version 12.3 in my Dockerfile (apk add postgresql-client=~12.3). Is it not possible to install that version now?

I'd like to update on my time and terms, why should I be forced to update now? Is there another repository I can add to use the older version?

Thanks

Isostasy answered 17/9, 2020 at 13:55 Comment(0)
S
11

Unfortunately, Alpine packages are always updated in place to the latest version, and older versions are discarded. This could be painful, indeed...

Usually, when a package is updated, it's updated with all Alpine distro versions that it's compatible to. For example, postgresql-client was bumped to 12.4-r0 on edge, v3.12 and v3.11, but on Alpine v3.10 repos you'll still find 11.9-r0. In case this was enough, the old version could be installed from the desired repository, as long as it lasts, using:

apk add postgresql-client=11.9-r0 --repository=http://dl-cdn.alpinelinux.org/alpine/v3.10/main

However, since 12.3 doesn't live in the official Alpine repositories anymore, you could rely on an external Docker image, instead.

Luckily, the postgres official images has version tags, and you can find the desired Alpine image for 12.3:

$ wget -q https://registry.hub.docker.com/v1/repositories/postgres/tags -O - | jq -r '.[].name' | grep 12.3
12.3  
12.3-alpine

Therefore, you can use FROM:postgres:12.3-alpine to get the desired version from.

In tougher cases, where the Alpine package version is updated, and couldn't be found in other images, the only resort may be building from source.

Snaggy answered 20/9, 2020 at 7:12 Comment(0)
U
3

for example; the latest dnsmasq version ins 2.84-r0 at now, if you install 2.83-r0, will:

$ docker run --rm -ti alpine:3.13
$ apk add 'dnsmasq-dnssec==2.83-r0'
fetch https://mirrors.aliyun.com/alpine/v3.13/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.aliyun.com/alpine/v3.13/community/x86_64/APKINDEX.tar.gz
ERROR: unable to select packages:
  dnsmasq-dnssec-2.84-r0:
    breaks: world[dnsmasq-dnssec=2.83-r0]

The best thing you can achieve is using repositories of the earlier releases, at the websiete https://pkgs.alpinelinux.org/packages to search the old version, will find 2.83-r0 in https://pkgs.alpinelinux.org/packages?name=dnsmasq&branch=v3.12. so add the old repo

$ echo 'http://dl-cdn.alpinelinux.org/alpine/v3.12/main' >> /etc/apk/repositories
$ apk add 'dnsmasq-dnssec==2.83-r0'
fetch http://mirrors.aliyun.com/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
fetch http://mirrors.aliyun.com/alpine/v3.12/community/x86_64/APKINDEX.tar.gz
(1/3) Installing gmp (6.2.0-r0)
(2/3) Installing nettle (3.5.1-r1)
(3/3) Installing dnsmasq-dnssec (2.83-r0)
Executing dnsmasq-dnssec-2.83-r0.pre-install
Executing busybox-1.31.1-r16.trigger
OK: 7 MiB in 17 packages
Unwitting answered 19/2, 2021 at 2:57 Comment(0)
L
2

Another solution based on the answer of @valiano.

For upgrading postgresql to a newer version, it is recommended to use the higher version pg_dump binaries. But how to get these into your image? This works for me:

Dockerfile:

ARG VERSION=10
ARG UPGRADE_VERSION=11
ARG TYPE
###############################################################
# Normal server
###############################################################
FROM postgres:${VERSION}-alpine AS server

RUN apk update \
 && apk add --upgrade apk-tools \
 && apk upgrade --available

COPY /rootfs/ /

###############################################################
# Upgrade version with upgrade executables
###############################################################
FROM postgres:${UPGRADE_VERSION}-alpine AS upgrade_version
RUN apk update \
 && apk add --upgrade apk-tools \
 && apk upgrade --available

###############################################################
# Add postgresql upgrade client executables to upgrade_server_layer
###############################################################
FROM server AS upgrade_server

RUN mkdir -p /usr/local/postgresql/upgrade
COPY --from=upgrade_version /usr/local/bin/pg* /usr/local/postgresql/upgrade/

###############################################################
# Final version
###############################################################
FROM ${TYPE}server AS final

Then build your normal server as:

docker build --build-arg TYPE= --build-arg VERSION=11 --build-arg UPGRADE_VERSION=12 -t my_normal_server:11 .

And a server with upgrade binaries, to make the dumpfile:

docker build --build-arg TYPE=upgrade_ --build-arg VERSION=10 --build-arg UPGRADE_VERSION=11 -t my_upgrade_server:10 .

Upgrade scenario if current version is postgresql 10 and you want to upgrade to 11:

  1. Build an upgrade version and a normal version.
  2. Stop the postgresql 10 container and replace it with the my_upgrade_version:10
  3. Create a dumpfile with the /usr/local/postgresql/upgrade/pg_dump.
  4. Create a new postgresql 11 container with my_normal_version:11 with access to the dumpfile and use pg_restore to restore the created dumpfile.
Lapp answered 18/11, 2021 at 16:25 Comment(0)
D
0

About making a dry single-point-of-entry for fixed package versions:

I use the following method, where I have an .env file where I store the PG major and minor version. I only need to update the .env-file entry with preferred version numbers and rebuild my images to upgrade the Postgres. As long as the package is present with the corresponding version in the dockerhub and the PG version itself isn't deprecated by the alpine repositories or sth:

Step 1: Specify the PG version in .env file as single point of entry:

PG_MAJOR_VERSION=14
PG_MINOR_VERSION=5

Step 2: Reference the db-package inside the docker-compose:

services:
  db:
    image: postgres:${PG_MAJOR_VERSION}.${PG_MINOR_VERSION}-alpine

Step 3: Use the variables inside the Dockerfile itself if needed:

ARG RUBY_VERSION
ARG DISTRO_NAME

FROM ruby:${RUBY_VERSION}-${DISTRO_NAME}

# Need to define the args again:
ARG DISTRO_NAME
ARG PG_MAJOR_VERSION

RUN apk add --update build-base bash bash-completion libffi-dev tzdata postgresql$PG_MAJOR_VERSION-client postgresql$PG_MAJOR_VERSION-dev nodejs npm yarn

NB! The FROM clause loses the ARG-variables defined before it. Therefore if you need them later then you need to define them again after the FROM-clause. This issue is described in more detail in this Github issue

Depending on the packages you wish to install you can specify the minor version or other suffixes as needed (for ex for postgresql15-client-15.1-r0 and postgresql15-dev-15.1-r0: package etc)

Dorkas answered 14/1, 2023 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.