webdevops / Dockerfile

:package: Dockerfiles from WebDevOps for PHP, Apache and Nginx
https://webdevops.io/projects/dockerfiles/
MIT License
1.67k stars 493 forks source link

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

Open aairey opened 5 years ago

aairey commented 5 years ago

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.

htuscher commented 5 years ago

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

aairey commented 5 years ago

The liquibase images.

On Fri, 19 Apr 2019, 14:02 Hans Höchtl, notifications@github.com 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 .

marklagendijk commented 5 years ago

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
aairey commented 2 years ago

@marklagendijk should we proceed to close this?