英文:
docker compose flyway not connecting to postgres
问题
运行 docker compose up
后只有 postgres docker 容器输出。flyway 容器似乎未尝试连接到 postgres 容器。尝试从终端运行独立的 flyway docker 容器并尝试连接到 postgres,但出现连接被拒绝错误(更多信息见下文)。
我不确定我漏掉了什么?以下是运行 docker compose up
后的输出:
警告:POSTGRES_HOST_AUTH_METHOD 已设置为“trust”。这将允许任何访问 Postgres 端口的人在没有密码的情况下访问数据库。在 Docker 的默认配置中,这实际上是同一系统上的任何其他容器。
不推荐使用 POSTGRES_HOST_AUTH_METHOD=trust。请改用“-e POSTGRES_PASSWORD=password”来设置密码。
这是在运行 docker compose up
后获取的输出。还尝试通过命令行运行 flyway docker 容器来测试从 flyway 到 postgres 的连接,发现如下错误:
无法从数据库获取连接(jdbc:postgresql://localhost:5432/restra)的错误,用户 'restra':拒绝连接到 localhost:5432。请检查主机名和端口是否正确,并且 postmaster 是否接受 TCP/IP 连接。
英文:
When running docker compose up
, I only get output from my postgres docker container. It seems that the flyway container is not even attempting to connect to the postgres container. I have also tried to run a standalone flyway docker container from the terminal and attempt to connect to postgres, but I get connection refused error ( more info below).
Im not sure what I'm missing here?
# Use postgres/example user/password credentials
version: '3'
services:
flyway:
image: flyway/flyway
command: -url=jdbc:postgresql://localhost:5432/restra -user=restra -password=restra -connectRetries=60 migrate
volumes:
- ${PWD}/resources/db/migrations:/flyway/sql
db:
image: postgres
restart: always
environment:
- POSTGRES_USER=restra # database with same name created automatically
- POSTGRES_PASSWORD=restra #password MUST be set, cannot be blank
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- "5432:5432"
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-U", "restra" ]
interval: 5s
timeout: 1s
retries: 2
I get this output after doing docker compose up
restra-db-1 | ********************************************************************************
restra-db-1 | WARNING: POSTGRES_HOST_AUTH_METHOD has been set to "trust". This will allow
restra-db-1 | anyone with access to the Postgres port to access your database without
restra-db-1 | a password, even if POSTGRES_PASSWORD is set. See PostgreSQL
restra-db-1 | documentation about "trust":
restra-db-1 | https://www.postgresql.org/docs/current/auth-trust.html
restra-db-1 | In Docker's default configuration, this is effectively any other
restra-db-1 | container on the same system.
restra-db-1 |
restra-db-1 | It is not recommended to use POSTGRES_HOST_AUTH_METHOD=trust. Replace
restra-db-1 | it with "-e POSTGRES_PASSWORD=password" instead to set a password in
restra-db-1 | "docker run".
restra-db-1 | ********************************************************************************
restra-db-1 | The files belonging to this database system will be owned by user "postgres".
restra-db-1 | This user must also own the server process.
restra-db-1 |
restra-db-1 | The database cluster will be initialized with locale "en_US.utf8".
restra-db-1 | The default database encoding has accordingly been set to "UTF8".
restra-db-1 | The default text search configuration will be set to "english".
restra-db-1 |
restra-db-1 | Data page checksums are disabled.
restra-db-1 |
restra-db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
restra-db-1 | creating subdirectories ... ok
restra-db-1 | selecting dynamic shared memory implementation ... posix
restra-db-1 | selecting default max_connections ... 100
restra-db-1 | selecting default shared_buffers ... 128MB
restra-db-1 | selecting default time zone ... Etc/UTC
restra-db-1 | creating configuration files ... ok
restra-db-1 | running bootstrap script ... ok
restra-db-1 | performing post-bootstrap initialization ... ok
restra-db-1 | syncing data to disk ... ok
restra-db-1 |
restra-db-1 |
restra-db-1 | Success. You can now start the database server using:
restra-db-1 |
restra-db-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
restra-db-1 |
restra-db-1 | initdb: warning: enabling "trust" authentication for local connections
restra-db-1 | initdb: hint: 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.
restra-db-1 | waiting for server to start....2023-05-10 17:38:59.384 UTC [49] LOG: starting PostgreSQL 15.2 (Debian 15.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
restra-db-1 | 2023-05-10 17:38:59.413 UTC [49] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
restra-db-1 | 2023-05-10 17:38:59.518 UTC [52] LOG: database system was shut down at 2023-05-10 17:38:57 UTC
restra-db-1 | 2023-05-10 17:38:59.565 UTC [49] LOG: database system is ready to accept connections
restra-db-1 | done
restra-db-1 | server started
restra-db-1 | CREATE DATABASE
restra-db-1 |
restra-db-1 |
restra-db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
restra-db-1 |
restra-db-1 | 2023-05-10 17:39:00.046 UTC [49] LOG: received fast shutdown request
restra-db-1 | waiting for server to shut down....2023-05-10 17:39:00.085 UTC [49] LOG: aborting any active transactions
restra-db-1 | 2023-05-10 17:39:00.089 UTC [49] LOG: background worker "logical replication launcher" (PID 55) exited with exit code 1
restra-db-1 | 2023-05-10 17:39:00.090 UTC [50] LOG: shutting down
restra-db-1 | 2023-05-10 17:39:00.120 UTC [50] LOG: checkpoint starting: shutdown immediate
restra-db-1 | 2023-05-10 17:39:00.680 UTC [50] LOG: checkpoint complete: wrote 918 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.153 s, sync=0.272 s, total=0.590 s; sync files=250, longest=0.216 s, average=0.002 s; distance=4217 kB, estimate=4217 kB
restra-db-1 | 2023-05-10 17:39:00.699 UTC [49] LOG: database system is shut down
restra-db-1 | done
restra-db-1 | server stopped
restra-db-1 |
restra-db-1 | PostgreSQL init process complete; ready for start up.
restra-db-1 |
restra-db-1 | 2023-05-10 17:39:00.896 UTC [1] LOG: starting PostgreSQL 15.2 (Debian 15.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
restra-db-1 | 2023-05-10 17:39:00.896 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
restra-db-1 | 2023-05-10 17:39:00.896 UTC [1] LOG: listening on IPv6 address "::", port 5432
restra-db-1 | 2023-05-10 17:39:00.971 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
restra-db-1 | 2023-05-10 17:39:01.074 UTC [65] LOG: database system was shut down at 2023-05-10 17:39:00 UTC
restra-db-1 | 2023-05-10 17:39:01.122 UTC [1] LOG: database system is ready to accept connections
I also tried to run the flyway docker container from command line to test the connection to the postgres docker container from flyway (found on the official flyway github page :https://github.com/flyway/flyway-docker) .
docker run --rm flyway/flyway -url=jdbc:postgresql://localhost:5432/restra -user=restra -password=restra info
Then I get the following error:
ERROR: Unable to obtain connection from database (jdbc:postgresql://localhost:5432/restra) for user 'restra': 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.
Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Caused by: java.net.ConnectException: Connection refused
答案1
得分: 1
尝试在你的 docker run
命令中添加 --network="host"
:
docker run --rm --network="host" flyway/flyway -url=jdbc:postgresql://localhost:5432/restra -user=restra -password=restra info
英文:
Try adding --network="host"
to your docker run
command:
docker run --rm --network="host" flyway/flyway -url=jdbc:postgresql://localhost:5432/restra -user=restra -password=restra info
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论