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