Run Postgres migration on Docker Compose

Viewed 728

So I have a GoLang project which I have setup in a Docker Compose file. I am using SQL-Migrate (https://github.com/rubenv/sql-migrate) for my migrations.

I am wanting when we run in docker for a .sh script to run which it basically runs sql-migrate up. Now there are no errors when I run docker-compose up but there is also no tables being created.

I am wondering if anyone can see anything wrong with the order things or being done that maybe the reason the tables are not being created? Or even, if they know why this issue is happening?

Dockerfile

FROM golang:alpine AS builder

RUN apk add --no-cache ca-certificates git

RUN mkdir /user && \
    echo 'nobody:x:65534:65534:nobody:/:' > /user/passwd && \
    echo 'nobody:x:65534:' > /user/group

COPY ./netrc /root/.netrc
RUN chmod 600 /root/.netrc

WORKDIR /src

COPY ./go.mod ./go.sum ./
RUN go mod download

COPY entrypoint.sh ./
RUN chmod +x ./entrypoint.sh


COPY . .

RUN CGO_ENABLED=0 go build \
    -installsuffix 'static' \
    -o /app .

ENTRYPOINT ["/app/entrypoint.sh"]


FROM alpine:3.4 AS final

COPY --from=builder /user/group /user/passwd /etc/
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /app /app

USER nobody:nobody

EXPOSE 50051

CMD ["/app"]

.sh Script

#!/bin/sh
sql-migrate up
exec "$@"

Docker Compose

user_db:
  image: postgres
  container_name: user_db
  ports:
    - '5433:5432'
  environment:
    POSTGRES_USER: **
    POSTGRES_PASSWORD: ** 
    POSTGRES_DB: user
  volumes:
    - 'data:/var/lib/user'
  healthcheck:
    test:
      - CMD-SHELL
      - pg_isready -U postgres
    interval: 10s
    timeout: 5s
    retries: 5

user-api:
  container_name: user-api
  build:
    context: ../user-api
    dockerfile: Dockerfile
  environment:
    DNS: 0.0.0.0
    PORT: 50051
    PGS_USER: **
    PGS_PASSWORD: **
    PGS_DATABASE: user
    PGS_PORT: 5433
    PGS_HOST: user_db
    PGS_SSLMODE: disable
  ports:
    - '50051:50051'
  depends_on:
    user_db:
      condition: service_healthy
0 Answers
Related