首页 > 解决方案 > 在 Docker Compose 上运行 Postgres 迁移

问题描述

所以我有一个 GoLang 项目,我在 Docker Compose 文件中设置了它。我正在使用 SQL-Migrate ( https://github.com/rubenv/sql-migrate ) 进行迁移。

当我们在 docker 中运行 .sh 脚本时,我想要运行它基本上运行的脚本sql-migrate up。现在我运行时没有错误,docker-compose up但也没有创建表。

我想知道是否有人可以看到订单或正在完成的任何问题,这可能是未创建表的原因?甚至,如果他们知道为什么会发生这个问题?

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 脚本

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

码头工人撰写

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

标签: dockerdocker-composedockerfilesh

解决方案


推荐阅读