我有一个运行数据库迁移的容器(源代码):
FROM golang:1.12-alpine3.10 AS downloader
ARG VERSION
RUN apk add --no-cache git gcc musl-dev
WORKDIR /go/src/github.com/golang-migrate/migrate
COPY . ./
ENV GO111MODULE=on
ENV DATABASES="postgres mysql redshift cassandra spanner cockroachdb clickhouse mongodb sqlserver firebird"
ENV SOURCES="file go_bindata github github_ee aws_s3 google_cloud_storage godoc_vfs gitlab"
RUN go build -a -o build/migrate.linux-386 -ldflags="-s -w -X main.Version=${VERSION}" -tags "$DATABASES $SOURCES" ./cmd/migrate
FROM alpine:3.10
RUN apk add --no-cache ca-certificates
COPY --from=downloader /go/src/github.com/golang-migrate/migrate/build/migrate.linux-386 /migrate
ENTRYPOINT ["/migrate"]
CMD ["--help"]
我希望将其集成到docker-compose中,并使其依赖于Postgres数据库服务。但是,由于我必须等待数据库完全初始化,因此我必须在脚本中包装
migrate
命令,并替换迁移容器的入口点。我正在使用wait-for脚本来轮询数据库,这是一个纯shell(而不是bash)脚本,因此应该可以在alpine容器中工作。
以下是在docker-compose中定义该服务的方式:
services:
database:
# ...
migration:
depends_on:
- database
image: migrate/migrate:v4.7.0
volumes:
- ./scripts/migrations:/migrations
- ./scripts/wait-for:/wait-for
entrypoint: ["/bin/sh"]
command: ["./wait-for database:5432", "--", "./migrate", "-path", "/migrations", "-database", "postgres://test:test@database:5432/test?sslmode=disable", "-verbose", "up"]
运行docker-compose up
失败并出现以下信息:
migration_1 | /bin/sh: can't open './wait-for database:5432': No such file or directory
使用下列命令在容器中运行迁移(migrate):
docker run -it --entrypoint /bin/sh -v $(pwd)/scripts/wait-for:/wait-for migrate/migrate:v4.7.0
这个脚本可以通过使用/bin/sh ./wait-for
命令来执行,但是在docker-compose中为什么会失败呢?