application.properties
spring.datasource.url=jdbc:postgresql://db:5432/test
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.id.new_generator_mappings=true
server.port=8000
docker-compose
version: '3.7'
services:
db:
image: postgres:11.4
ports:
- "5432:5432"
expose:
- 5432
environment:
POSTGRES_PASSWORD: "root"
POSTGRES_DB: test
lady:
depends_on:
- db
build: ./test/
ports:
- "8000:8000"
expose:
- 8000
environment:
DB_USER: "postgres"
DB_PASSWORD: "123"
DB_TYPE: "postgres"
command: ["java","-jar","test.jar"]
记录错误
com.women.lady.LadyApplicationTests > contextLoads() FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: javax.persistence.PersistenceException
Caused by: org.hibernate.exception.JDBCConnectionException
Caused by: org.postgresql.util.PSQLException
Caused by: java.net.UnknownHostException
运行docker compose时,我无法连接到postgres或db: docker-compose up
问题
如何在docker-compose上连接数据库。 请举个例子。