Skip to content

Commit f174e9e

Browse files
authored
Make Dockerfile aware of Postgres versions. (#855)
That way we can easily build and test pg_auto_failover locally with any supported version of Postgres. We use docker build --build-arg for that, and split the Dockerfile in more stages for clarity and better cache re-use. It is now possible to run the same test for Postgres 10 then 14 that way: $ make run-test TEST=test_auth PGVERSION=10 $ make run-test TEST=test_auth PGVERSION=14
1 parent 7cfa0b6 commit f174e9e

2 files changed

Lines changed: 100 additions & 26 deletions

File tree

Dockerfile

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
FROM debian:buster-slim as build-test
1+
#
2+
# Using --build-arg PGVERSION=11 we can build pg_auto_failover for any
3+
# target version of Postgres. In the Makefile, we use that to our advantage
4+
# and tag test images such as pg_auto_failover_test:pg14.
5+
#
6+
ARG PGVERSION=10
27

3-
ENV PGVERSION 10
8+
#
9+
# Define a base image with all our build dependencies.
10+
#
11+
# This base image contains all our target Postgres versions.
12+
#
13+
FROM debian:buster-slim as base
414

515
RUN apt-get update \
616
&& apt-get install -y --no-install-recommends \
@@ -17,7 +27,8 @@ RUN apt-get update \
1727
libreadline-dev \
1828
libpam-dev \
1929
zlib1g-dev \
20-
libxml2-dev \
30+
liblz4-dev \
31+
libxml2-dev \
2132
libxslt1-dev \
2233
libselinux1-dev \
2334
libncurses-dev \
@@ -48,8 +59,16 @@ RUN echo "deb http://apt.postgresql.org/pub/repos/apt buster-pgdg main" > /etc/a
4859
RUN echo 'create_main_cluster = false' | sudo tee -a /etc/postgresql-common/createcluster.conf
4960
RUN apt-get update \
5061
&& apt-get install -y --no-install-recommends \
51-
postgresql-server-dev-${PGVERSION} \
52-
postgresql-${PGVERSION} \
62+
postgresql-server-dev-10 \
63+
postgresql-server-dev-11 \
64+
postgresql-server-dev-12 \
65+
postgresql-server-dev-13 \
66+
postgresql-server-dev-14 \
67+
postgresql-10 \
68+
postgresql-11 \
69+
postgresql-12 \
70+
postgresql-13 \
71+
postgresql-14 \
5372
&& rm -rf /var/lib/apt/lists/*
5473

5574
RUN pip3 install pyroute2>=0.5.17
@@ -58,25 +77,48 @@ RUN adduser docker sudo
5877
RUN adduser docker postgres
5978
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
6079

80+
#
81+
# On-top of the base build-dependencies image, now we can build
82+
# pg_auto_failover for a given --build-arg PGVERSION target version of
83+
# Postgres.
84+
#
85+
FROM base as build
86+
87+
ARG PGVERSION
88+
89+
ENV PG_CONFIG /usr/lib/postgresql/${PGVERSION}/bin/pg_config
90+
6191
WORKDIR /usr/src/pg_auto_failover
6292

6393
COPY Makefile ./
6494
COPY ./src/ ./src
6595
RUN make -s clean && make -s install -j8
6696

67-
COPY ./tests/ ./tests
6897

98+
#
99+
# Given the build image above, we can now run our test suite targetting a
100+
# given version of Postgres.
101+
#
102+
FROM build as test
103+
104+
ARG PGVERSION
105+
106+
COPY ./tests/ ./tests
69107
COPY ./valgrind ./valgrind
70108
RUN chmod a+w ./valgrind
71109

72110
USER docker
73-
ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/postgresql/${PGVERSION}/bin
111+
74112
ENV PG_AUTOCTL_DEBUG 1
113+
ENV PATH /usr/lib/postgresql/${PGVERSION}/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
75114

76115

116+
#
117+
# And finally our "run" images with the bare minimum for run-time.
118+
#
77119
FROM debian:buster-slim as run
78120

79-
ENV PGVERSION 10
121+
ARG PGVERSION
80122

81123
RUN apt-get update \
82124
&& apt-get install -y --no-install-recommends \
@@ -110,9 +152,9 @@ RUN adduser docker sudo
110152
RUN adduser docker postgres
111153
RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
112154

113-
COPY --from=build-test /usr/lib/postgresql/${PGVERSION}/lib/pgautofailover.so /usr/lib/postgresql/${PGVERSION}/lib
114-
COPY --from=build-test /usr/share/postgresql/${PGVERSION}/extension/pgautofailover* /usr/share/postgresql/${PGVERSION}/extension/
115-
COPY --from=build-test /usr/lib/postgresql/${PGVERSION}/bin/pg_autoctl /usr/local/bin
155+
COPY --from=build /usr/lib/postgresql/${PGVERSION}/lib/pgautofailover.so /usr/lib/postgresql/${PGVERSION}/lib
156+
COPY --from=build /usr/share/postgresql/${PGVERSION}/extension/pgautofailover* /usr/share/postgresql/${PGVERSION}/extension/
157+
COPY --from=build /usr/lib/postgresql/${PGVERSION}/bin/pg_autoctl /usr/local/bin
116158

117159
#
118160
# In tests/upgrade/docker-compose.yml we use internal docker volumes in

Makefile

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
TOP := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
55

66
CONTAINER_NAME = pg_auto_failover
7+
BUILD_CONTAINER_NAME = pg_auto_failover_build
78
TEST_CONTAINER_NAME = pg_auto_failover_test
89
DOCKER_RUN_OPTS = --privileged -ti --rm
910

11+
PGVERSION ?= 10
1012
NOSETESTS = $(shell which nosetests3 || which nosetests)
1113

1214
# Tests for the monitor
@@ -146,30 +148,56 @@ indent:
146148
docs: $(FSM)
147149
$(MAKE) -C docs html
148150

149-
build:
150-
docker build \
151-
$(DOCKER_BUILD_OPTS) \
152-
-t $(CONTAINER_NAME) \
153-
.
154-
155151
interactive-test:
156152
docker run --name $(CONTAINER_NAME) --rm -ti $(CONTAINER_NAME)
157153

154+
build-image:
155+
docker build $(DOCKER_BUILD_OPTS) --target build -t $(BUILD_CONTAINER_NAME) .
156+
157+
build-test-pg10: build-image
158+
docker build --build-arg PGVERSION=10 --target test -t $(TEST_CONTAINER_NAME):pg10 .
159+
160+
build-test-pg11:
161+
docker build --build-arg PGVERSION=11 --target test -t $(TEST_CONTAINER_NAME):pg11 .
158162

159-
build-test:
160-
docker build \
161-
$(DOCKER_BUILD_OPTS) \
162-
--target build-test \
163-
-t $(TEST_CONTAINER_NAME) \
164-
.
163+
build-test-pg12:
164+
docker build --build-arg PGVERSION=12 --target test -t $(TEST_CONTAINER_NAME):pg12 .
165165

166-
run-test: build-test
166+
build-test-pg13:
167+
docker build --build-arg PGVERSION=13 --target test -t $(TEST_CONTAINER_NAME):pg13 .
168+
169+
build-test-pg14:
170+
docker build --build-arg PGVERSION=14 --target test -t $(TEST_CONTAINER_NAME):pg14 .
171+
172+
run-test: build-test-pg$(PGVERSION)
167173
docker run \
168174
--name $(TEST_CONTAINER_NAME) \
169175
$(DOCKER_RUN_OPTS) \
170-
$(TEST_CONTAINER_NAME) \
176+
$(TEST_CONTAINER_NAME):pg$(PGVERSION) \
171177
make -C /usr/src/pg_auto_failover test \
172-
TEST='${TEST}'
178+
PGVERSION=$(PGVERSION) TEST='${TEST}'
179+
180+
build-pg10: build-test-pg10
181+
docker build --build-arg PGVERSION=10 $(DOCKER_BUILD_OPTS) -t $(CONTAINER_NAME):pg10 .
182+
183+
build-pg11: build-test-pg11
184+
docker build --build-arg PGVERSION=11 $(DOCKER_BUILD_OPTS) -t $(CONTAINER_NAME):pg11 .
185+
186+
build-pg12: build-test-pg12
187+
docker build --build-arg PGVERSION=12 $(DOCKER_BUILD_OPTS) -t $(CONTAINER_NAME):pg12 .
188+
189+
build-pg13: build-test-pg13
190+
docker build --build-arg PGVERSION=13 $(DOCKER_BUILD_OPTS) -t $(CONTAINER_NAME):pg13 .
191+
192+
build-pg14: build-test-pg14
193+
docker build --build-arg PGVERSION=14 $(DOCKER_BUILD_OPTS) -t $(CONTAINER_NAME):pg14 .
194+
195+
build: build-pg10 build-pg11 build-pg12 build-pg13 build-pg14 ;
196+
197+
build-check:
198+
for v in 10 11 12 13 14; do \
199+
docker run --rm -t pg_auto_failover_test:pg$$v pg_autoctl version --json | jq ".pg_version" | xargs echo $$v: ; \
200+
done
173201

174202
build-i386:
175203
docker build -t i386:latest -f Dockerfile.i386 .
@@ -276,3 +304,7 @@ azdrop: all
276304
.PHONY: build-test run-test
277305
.PHONY: tmux-clean cluster
278306
.PHONY: azcluster azdrop az
307+
.PHONY: build-image
308+
.PHONY: build-test-pg10 build-test-pg11 build-test-pg142
309+
.PHONY: build-test-pg13 build-test-pg14
310+
.PHONY: build build-pg10 build-pg11 build-pg12 build-pg13 build-pg14

0 commit comments

Comments
 (0)