我的 docker-compose.yml 是:
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 是:
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"]
應用程式屬性
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;
}
}
當我構建我的應用程式映像一切正常時,我應該啟動我的資料庫映像和應用程式映像。我從 db 開始docker-compose up db,我得到:org.postgresql.util.PSQLException: FATAL: database "haircut_studio" does not exist. 我的 DbConfig 有問題嗎?如果我在本地啟動我的應用程式,而不是在 Docker 容器中,我的應用程式成功連接到資料庫,我可以做任何我想做的事情。我該如何解決這個錯誤?
這是 GitLab 存盤庫:https ://gitlab.com/staaankey/java/-/tree/reservation-feature
uj5u.com熱心網友回復:
默認資料目錄是 /var/lib/postgresql/data。您需要調整音量:
volumes:
- ./log-directory:/var/lib/postgresql/data
你可以在這里閱讀更多關于它的資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/463032.html
標籤:爪哇 春天 PostgreSQL 弹簧靴 码头工人
上一篇:更改列以使其成為派生列
