Postgres with liquibase in one docker image

7/29/2020

I want to create dataBase for autotest in kubernetes. I want to create an image(postg-my-app-v1) from postgres image, add changelog files, and liquibase image. When I deploy this image with helm i just want to specify containers - postg-my-app-v1 and it should startup pod with database and create tables with liquibase changelog.

Now i create Dockerfile as below

FROM postgres
ADD /changelog /liquibase/changelog

I don't understand how to add liquibase to this image? Or i must use docker compose? or helm lifecycle postStart for liquibase?

-- Tim
docker-image
kubernetes
liquibase
postgresql

1 Answer

8/7/2020
FROM docker-proxy.tcsbank.ru/liquibase/liquibase:3.10.x AS Liquibase

FROM docker-proxy.tcsbank.ru/postgres:9.6.12 AS Postgres
ENV POSTGRES_DB bpm
ENV POSTGRES_USER priest
ENV POSTGRES_PASSWORD Bpm_123


COPY --from=Liquibase /liquibase /liquibase

ENV JAVA_HOME /usr/local/openjdk-11
COPY --from=Liquibase $JAVA_HOME $JAVA_HOME


ENV LIQUIBASE_CHANGELOG /liquibase/changelog/
COPY /changelog $LIQUIBASE_CHANGELOG


COPY liquibase.sh /usr/local/bin/
COPY main.sh /usr/local/bin/

RUN chmod +x /usr/local/bin/liquibase.sh && \
    chmod +x /usr/local/bin/main.sh && \
    ln -s /usr/local/bin/main.sh / && \
    ln -s /usr/local/bin/liquibase.sh /

ENTRYPOINT ["main.sh"]

main.sh

#!/bin/bash

bash liquibase.sh | awk '{print "liquiBase script: " $0}' &

bash docker-entrypoint.sh postgres

liquibase.sh

#!/bin/bash

for COUNTER in {1..120}
do
   sleep 1s
   echo "check db $COUNTER times"
   pg_isready
   if [ $? -eq 0 ]
   then
     break
   fi
done

echo "try execute liquibase"
bash liquibase/liquibase --url="jdbc:postgresql://localhost:5432/$POSTGRES_DB"  --username=$POSTGRES_USER --password=$POSTGRES_PASSWORD --changeLogFile=/liquibase/changelog/changelog.xml update
-- Tim
Source: StackOverflow