flyway无法在docker

编程入门 行业动态 更新时间:2024-10-26 18:27:20
本文介绍了flyway无法在docker-entrypoint-initdb.d脚本中连接到Postgres容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将 postgres Docker 映像扩展为可能(通过环境变量标志)在数据库初始化上执行flyway数据库迁移.我的Dockerfile在这里:

I'm trying to extend the postgres Docker image to potentially (via an environment variable flag) execute flyway DB migrations on DB init. My Dockerfile is here:

FROM postgres:9.6 # Install curl and java (for Flyway) RUN set -x \ && apt-get update \ && apt-get install -y --no-install-recommends ca-certificates curl openjdk-8-jre # Install Flyway ENV FLYWAY_VERSION 4.2.0 ENV FLYWAY_INSTALL_DIR /usr/src/flyway ENV FLYWAY_CONF ${FLYWAY_INSTALL_DIR}/flyway-${FLYWAY_VERSION}/conf/flyway.conf ENV FLYWAY_EXE ${FLYWAY_INSTALL_DIR}/flyway-${FLYWAY_VERSION}/flyway RUN mkdir -p ${FLYWAY_INSTALL_DIR} && \ curl -L repo1.maven/maven2/org/flywaydb/flyway-commandline/${FLYWAY_VERSION}/flyway-commandline-${FLYWAY_VERSION}.tar.gz | \ tar -xzC ${FLYWAY_INSTALL_DIR} && \ chmod +x ${FLYWAY_EXE} # Copy migration scripts ENV MIGRATIONS_LOCATION /flyway/migrations COPY migrations $MIGRATIONS_LOCATION COPY init_db.sh /docker-entrypoint-initdb.d/init_db.sh

使用我的 init_db.sh 启动脚本:

#!/bin/bash set -e RUN_MIGRATIONS="${RUN_MIGRATIONS:-false}" DB_URL="jdbc:postgresql://localhost:5432/$DB_NAME" psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL CREATE DATABASE $DB_NAME; EOSQL if [ "$RUN_MIGRATIONS" == "true" ]; then echo "running migrations ..." ${FLYWAY_EXE} -user=$POSTGRES_USER -password=$POSTGRES_PASSWORD -url=$DB_URL -locations="filesystem:$MIGRATIONS_LOCATION" migrate fi

但是,当使用 RUN_MIGRATIONS = true 运行容器时,flyway无法连接到postgres:

However, when running the container with RUN_MIGRATIONS=true, flyway fails to connect to postgres:

docker build . -t postgres-flyway && docker run -e DB_NAME=db -e RUN_MIGRATIONS=true -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres postgres-flyway The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "en_US.utf8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english". Data page checksums are disabled. fixing permissions on existing directory /var/lib/postgresql/data ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting dynamic shared memory implementation ... posix creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok WARNING: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: pg_ctl -D /var/lib/postgresql/data -l logfile start waiting for server to start....LOG: database system was shut down at 2018-08-06 02:19:32 UTC LOG: MultiXact member wraparound protections are now enabled LOG: autovacuum launcher started LOG: database system is ready to accept connections done server started ALTER ROLE /usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/init_db.sh CREATE DATABASE running migrations ... Flyway 4.2.0 by Boxfuse ERROR: Unable to obtain Jdbc connection from DataSource (jdbc:postgresql://localhost:5432/db) for user 'postgres': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SQL State : 08001 Error Code : 0 Message : Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

postgres映像在端口5432上运行postgres(与往常一样),所以我不知道为什么flyway无法通过 localhost:5432 连接到postgres.

The postgres image runs postgres on port 5432 (as usual) so I'm at a loss on why flyway is unable to connect to postgres over localhost:5432.

我还注意到,在这种情况下, pg_isready 指出postgres正在接受连接,但是在将主机名指定为 localhost 或 127.0.0.1 时它也无法到达postgres.也就是说,通过在我的 init_db.sh 脚本中插入一些 pg_isready 命令:

I also noticed that within this context, pg_isready states that postgres is accepting connections but when specifying the hostname as localhost or 127.0.0.1 it is unable to reach postgres either. That is, by inserting a few pg_isready commands in my init_db.sh script:

... pg_isready pg_isready -p 5432 pg_isready -h localhost -p 5432 ...

我在postgres init上看到以下日志输出:

I see the following log output on postgres init:

... /var/run/postgresql:5432 - accepting connections /var/run/postgresql:5432 - accepting connections localhost:5432 - no response ...

我怀疑自己已经达到了postgres初始化上下文的限制,但是我想了解为什么在初始化时通过 localhost/127.0.0.1:5432 无法访问postgres

I'm suspicious that I've reached a limitation of postgres' initialize context, but I would like to understand why postgres is unreachable over localhost/127.0.0.1:5432 at this point of initialization.

推荐答案

我在浏览图像时发现了问题进入点脚本.图片的最新更改限制了Postgres在内部初始化期间仅监听Unix域套接字上的连接: github/docker-library/postgres/pull/440

I discovered the problem while digging through the images entry point script. A recent change to the image restricts postgres to only listen for connections over a unix domain socket during internal initialization: github/docker-library/postgres/pull/440

更多推荐

flyway无法在docker

本文发布于:2023-10-23 06:16:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520036.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:flyway   docker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!