이 질문은 여기 , 여기 및 여기 에서 여러 번 요청 되었지만 이러한 솔루션은 저에게 효과적이지 않습니다.
이 docker-compose.yml
파일 로 Postgres 및 AppServer 컨테이너를 만들었습니다.
version: "3.7"
services:
db:
image: postgres:alpine
container_name: db
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: mydb
POSTGRES_INITDB_ARGS: '-A md5'
volumes:
- ./pgdata:/var/lib/postgressql/data
ports:
- "5432:5432"
api:
build: api
container_name: api
volumes:
- ./database/migrations:/migrations
ports:
- "8080:8080"
links:
- db
depends_on:
- db
이것을 실행하면 성공적으로 할 수 있습니다.
docker exec -it db psql -U user mydb
Postgres에 성공적으로 연결합니다. 두 컨테이너의 터미널에 성공적으로 로그인 할 수도 있습니다.
docker exec -it api bash
docker exec -it db bash
api의 bash 내부에서 아무 문제없이 db를 ping 할 수 있습니다.
그러나 내 API 컨테이너에서 Postgres 데이터베이스에 대한 JDBC 연결을 설정할 수 없습니다.
api | Flyway Community Edition 7.3.2 by Redgate
api | ERROR:
api | Unable to obtain connection from database (jdbc:postgresql://db:5432/mydb) for user 'user': Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
api | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
api | SQL State : 08001
api | Error Code : 0
api | Message : Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
api |
api | Caused by: org.postgresql.util.PSQLException: Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
api | Caused by: java.net.ConnectException: Connection refused (Connection refused)
psql을 통해 연결할 수 있는데 왜 연결이 거부됩니까? 이것은 나의 비행로 conf입니다
flyway.url=jdbc:postgresql://db:5432/mydb
flyway.user=user
flyway.password=password
flyway.locations=filesystem:/migrations
편집 :: 그래서 내가 기다렸다가 모든 것에서 flyway migrate
얼마 후 실행하면 docker exec -it api bash
잘 작동합니다. 위에서 일어나는 일은 flyway migrate
데이터베이스가 준비되기 전에도 명령이 실행되고 있다는 것 입니다.
왜 이런 일이 발생합니까? 종속성을 지정했기 때문에 API 컨테이너는 데이터베이스가 완전히 시작된 경우에만 시작해야합니다. 하지만 그렇지 않은 것 같습니다.
데이터베이스 컨테이너를 종속성으로 지정한다고해서 다른 서비스 / 컨테이너보다 먼저 준비 된다는 보장은 없습니다 . 다른 서비스보다 먼저 시작 된다는 것을 보장 합니다.
이 문제를 해결하는 한 가지 방법은 시작 중에 데이터베이스에 연결하지 못한 경우 API 애플리케이션에서 재 시도를 구현하는 것입니다.
다음은 서비스가 준비 될 때까지 기다리는 쉘 스크립트를 사용하는 기사에 대한 링크 입니다.
IMO 애플리케이션은 데이터베이스 연결을 설정할 수 없을 때 몇 번 재 시도 할 수있을만큼 똑똑해야합니다. 어쨌든 더 강력하게 만들 것입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다