docker - 在 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
解决方案
推荐阅读
- c++ - 带有抽象类的列表与向量
- haskell - 构建树中所有分支的列表
- amazon-web-services - 如何在 AWS 上自动扩展 Cassandra 集群或如何根据负载动态连接到集群
- reactjs - 在 history.push 之后中继 fetchQuery 不起作用,但在手动刷新时可以正常工作
- javascript - Counting letters and numbers in string
- c# - 如何读取和显示文本文件中的所有数据
- laravel - 如何解决这个 unserialize(): ErrorCache/FileStore.php:187
- scala - 从 spark 数据帧或 RDD 生成键值对,键中存在列名
- java - 当数据中心中的所有联系点在 cassandra 中发生故障时会发生什么
- android - 在 Kotlin 中使用星形投影作为超类型