Skip to content

Instantly share code, notes, and snippets.

@codonnell
Created June 27, 2020 12:20
Show Gist options
  • Save codonnell/45be5a775b209984fb49127583030f4f to your computer and use it in GitHub Desktop.
Save codonnell/45be5a775b209984fb49127583030f4f to your computer and use it in GitHub Desktop.
Flyway dockerfile using DATABASE_URL
FROM flyway/flyway:6.4.2
COPY migrations /flyway/sql
COPY parse-url.sh /parse-url.sh
USER root
RUN apt update && apt -y install --no-install-recommends curl
USER flyway
ENTRYPOINT ["/bin/sh" "-c"]
CMD ["/flyway/flyway -url=$(/parse-url.sh $DATABASE_URL jdbc_url) -user=$(/parse-url.sh $DATABASE_URL user) -password=$(/parse-url.sh $DATABASE_URL password) -connectRetries=60 migrate"]
#!/bin/bash
# extract the protocol
proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')"
# remove the protocol
url="$(echo ${1/$proto/})"
# extract the user (if any)
user="$(echo $url | grep @ | cut -d@ -f1)"
# extract the host and port
hostport="$(echo ${url/$user@/} | cut -d/ -f1)"
# by request host without port
host="$(echo $hostport | sed -e 's,:.*,,g')"
# by request - try to extract the port
port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')"
# extract the path (if any)
path="$(echo $url | grep / | cut -d/ -f2-)"
case $2 in
jdbc_url)
echo "jdbc:${proto}${hostport}/${path}"
;;
user)
echo $user | cut -d: -f1
;;
password)
echo $user | cut -d: -f2
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment