Dockerfile icon indicating copy to clipboard operation
Dockerfile copied to clipboard

Add wait-for-it.sh script or similar to liquibase images

Open aairey opened this issue 6 years ago • 4 comments
trafficstars

Hello,

With docker-compose version 3.0 and higher, is no longer possible to add a healthcheck to a database service and set a conditional depends_on. See: https://docs.docker.com/compose/compose-file/#depends_on

As per https://docs.docker.com/compose/startup-order/, we can use a script that waits for the database service to be healthy. To avoid creating my own fork of the webdevops image, I thought it might be useful to include this script in the mysql and postgresql versions of the image. It could be optional to use it (so not having it as the default entrypoint or cmd ...), but at least have it inside the image.

aairey avatar Apr 18 '19 12:04 aairey

Which image are you referring to? We don't build database images.

htuscher avatar Apr 19 '19 12:04 htuscher

The liquibase images.

On Fri, 19 Apr 2019, 14:02 Hans Höchtl, [email protected] wrote:

Which image are you referring to? We don't build database images.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/webdevops/Dockerfile/issues/306#issuecomment-484872720, or mute the thread https://github.com/notifications/unsubscribe-auth/AB2KHPVNMV3RJ5C4JYFUNHTPRGYGZANCNFSM4HG4SBRA .

aairey avatar Apr 21 '19 17:04 aairey

The liquibase image could indeed benefit from this. It could work by allowing to specify environment variables with the hostname and port. The entrypoint script could then use wait-for-it.sh when these variables are defined, and run the liquibase command after that.

Currently it is quite tricky to get the Liquibase execution working correctly inside a docker-compose.yml. I will add my working example here:

docker-compose.yaml:

version: "3"
services:
  postgres:
    image: postgres:10
    restart: always
    ports:
      - "5432:5432"
    volumes:
      - ./docker/data-volumes/postgres/:/var/lib/postgresql/data/
      - ./docker/postgres/:/docker-entrypoint-initdb.d/
  liquibase:
    image: webdevops/liquibase:postgres
    restart: 'no'
    depends_on:
      - postgres
    volumes:
      - ./docker/liquibase/:/scripts/
    command: /scripts/wait_for_postgres_then_init.sh

docker/postgres/init.sql:

CREATE DATABASE my_database;
\connect my_database;
CREATE SCHEMA liquibase;

docker/liquibase/wait_for_postgres_then_init.sh:

#!/usr/bin/env bash
/scripts/wait-for-it.sh postgres:5432 -- /scripts/init.sh

docker/liquibase/wait-for-it.sh: wait-for-it.sh

docker/liquibase/init.sh:

#!/usr/bin/env bash
/opt/liquibase/liquibase \
    --driver=org.postgresql.Driver \
    --url=jdbc:postgresql://postgres:5432/my_database \
    --liquibaseSchemaName=liquibase \
    --classpath=/usr/share/java/postgresql.jar \
    --changeLogFile=/liquibase/my-changelog.xml \
    --username=postgres \
    --password= \
    --contexts=all \
    update

marklagendijk avatar May 01 '19 12:05 marklagendijk

@marklagendijk should we proceed to close this?

aairey avatar Jul 14 '22 23:07 aairey