From 89363e8a3061b804798d7199c2377a8ccec73092 Mon Sep 17 00:00:00 2001 From: ramil Date: Fri, 12 Feb 2021 16:31:55 +0300 Subject: [PATCH] entrypoint for docker image, apply migration on start up --- Dockerfile | 13 ++++++++++++- startup_script.sh | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100755 startup_script.sh diff --git a/Dockerfile b/Dockerfile index 285512b..27e0a26 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,12 +9,23 @@ ADD . /go/src/github.com/vulcanize/tracing-api WORKDIR /go/src/github.com/vulcanize/tracing-api RUN make linux +# Build migration tool +WORKDIR / +RUN go get -u -d github.com/pressly/goose/cmd/goose +WORKDIR /go/src/github.com/pressly/goose/cmd/goose +RUN GCO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-extldflags "-static"' -tags='no_mysql no_sqlite' -o goose . + # app container FROM alpine +WORKDIR /app + # keep binaries immutable COPY --from=builder /go/src/github.com/vulcanize/tracing-api/build/tracer-linux /usr/local/bin/tracer +COPY --from=builder /go/src/github.com/pressly/goose/cmd/goose/goose /usr/local/bin/goose +COPY --from=builder /go/src/github.com/vulcanize/tracing-api/startup_script.sh . +COPY --from=builder /go/src/github.com/vulcanize/tracing-api/db/migrations migrations EXPOSE 8080 -ENTRYPOINT ["tracer"] +ENTRYPOINT ["/app/startup_script.sh"] \ No newline at end of file diff --git a/startup_script.sh b/startup_script.sh new file mode 100755 index 0000000..da76a00 --- /dev/null +++ b/startup_script.sh @@ -0,0 +1,21 @@ +#!/bin/sh +# Runs the db migrations and starts the service + +set +x + +# Construct the connection string for postgres +VDB_PG_CONNECT=postgresql://$CACHE_DATABASE_USER:$CACHE_DATABASE_PASSWORD@$CACHE_DATABASE_HOSTNAME:$CACHE_DATABASE_PORT/$CACHE_DATABASE_NAME?sslmode=disable + +# Run the DB migrations +echo "Connecting with: $VDB_PG_CONNECT" +echo "Running database migrations" +goose -table goose_db_version_trace -dir migrations postgres "$VDB_PG_CONNECT" up + +# If the db migrations ran without err +if [[ $? -ne 0 ]]; then + echo "Could not run migrations. Are the database details correct?" + exit 1 +fi + +echo "Running the Tracing-API process" +exec tracer "$@" \ No newline at end of file