Вот эксепшн. Уже пробовал и localhost и сделал container_name: db, пробовал сам service в docker-compose менять на db, нет коннекта.
2024-04-17T16:14:40.776+03:00 INFO 17492 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1226 ms
2024-04-17T16:14:41.017+03:00 INFO 17492 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2024-04-17T16:14:44.321+03:00 ERROR 17492 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: Ошибка при попытке подсоединения.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:358) ~[postgresql-42.7.3.jar:42.7.3]
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:54) ~[postgresql-42.7.3.jar:42.7.3]
Caused by: java.net.UnknownHostException: db
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:572) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
Еще не понимаю liquibase, влияет ли она на загрузку? Первый раз с ней сталкиваюсь.
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>
docker-compose.yml
version: "3"
services:
app:
build:
context: .
dockerfile: app.Dockerfile
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/javacode
- SPRING_DATASOURCE_USERNAME=postgres
- SPRING_DATASOURCE_PASSWORD=postgres
ports:
- "8080:8080"
# command: ["./wait-for-it.sh", "db:5432", "java", "-jar", "/app/app.war"]
psql:
build:
context: .
dockerfile: postgreSQL.Dockerfile
container_name: db
environment:
POSTGRES_DB: javacode
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "5432:5432"
volumes:
- ./db/initDB.sql:/docker-entrypoint-initdb.d/initDB.sql
app.Dockerfile
FROM openjdk:17-jdk
WORKDIR /app
COPY target/*.war /app/app.war
ENTRYPOINT ["java", "-jar", "/app/app.war"]
postgreSQL.Dockerfile
FROM postgres:16
ENV TZ=Europe/Moscow
application.yml
spring:
jpa:
show-sql: true
Пока захардкодил DataSourceBuilder.create(), так как думал проверить работает или нет, получается не работает...
@Configuration
public class LiquibaseConfiguration {
@Bean
public SpringLiquibase liquibase() {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog("classpath:db/changelog/db.changelog-master.yml");
liquibase.setDataSource(dataSource());
return liquibase;
}
@Bean
@ConfigurationProperties("spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create()
.url("jdbc:postgresql://db:5432/javacode")
.driverClassName("org.postgresql.Driver")
.username("postgres")
.password("postgres")
.build();
}
}