My docker-compose.yml is:
version: "3.9"
services:
db:
image: postgres
restart: always
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: 123
POSTGRES_DB: "haircut_studio"
POSTGRES_USER: "postgres"
volumes:
- ./log-directory:/var/lib/postgresql
app:
build: ../
restart: always
ports:
- "8080:8080"
environment:
- DATASOURCE_HOST=db
- DATASOURCE_PORT=5432
depends_on:
- db
Dockerfile is:
FROM openjdk:17-alpine
COPY / /spd
WORKDIR /spd
RUN chmod x gradlew
RUN ./gradlew :bootJar
WORKDIR /spd/build/libs
EXPOSE 8080
CMD ["java","-jar", "haircutStudio-0.0.1-SNAPSHOT.jar"]
application.properties
spring.jpa.show-sql=true
#logging.level.root=debug
server.port=8080
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
datasource.username=postgres
datasource.password=123
datasource.host=localhost
datasource.port=5432
datasource.url=jdbc:postgresql://${datasource.host}:${datasource.port}/haircut_studio
spring.datasource.driver-class-name=org.postgresql.Driver
spring.flyway.baselineOnMigrate = true
logging.level.org.springframework.boot.autoconfigure=ERROR
TestDbConfig.java
package com.spdu.haircutstudio.configuration;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DbConfig {
@Bean(destroyMethod = "close")
public DataSource getDataSource(
@Value("${datasource.password}") String password,
@Value("${datasource.username}") String username,
@Value("${datasource.url}") String jdbcUrl
) {
HikariConfig config = new HikariConfig();
config.setJdbcUrl(jdbcUrl);
config.setUsername(username);
config.setPassword(password);
return new HikariDataSource(config);
}
@Bean
public Flyway flyway(DataSource dataSource) {
final Flyway flyway = Flyway.configure()
.dataSource(dataSource)
.locations("classpath:db")
.outOfOrder(true)
.load();
flyway.migrate();
return null;
}
}
When I built my app image everything was ok, then I should start my db image and app image. I start db with docker-compose up db
and I got: org.postgresql.util.PSQLException: FATAL: database "haircut_studio" does not exist
. Is something wrong with my DbConfig? If I start my app locally, not in Docker container my app successfully connects to DB and I can do everything, that I want. How I can fix this error?
Here is GitLab repo: https://gitlab.com/staaankey/java/-/tree/reservation-feature
CodePudding user response:
The default data directory is /var/lib/postgresql/data. You need to adjust your volume:
volumes:
- ./log-directory:/var/lib/postgresql/data
You can read more about it here