3

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

5
  • Can you log and check your datasource url? Commented Apr 23, 2022 at 10:21
  • 1
    The Postgres container only creates the POSTGRES_DB database if no database already exists in /var/lib/postgresql. So make sure ./log-directory is empty on the host before you start the containers. Commented Apr 23, 2022 at 10:23
  • @HansKilian .local/log-directory/data is empty before I start Commented Apr 23, 2022 at 10:31
  • @tbjorch How i can do this? I restarted my container and got this: PS C:\Users\hp-laptop-dmytro\IdeaProjects\java\.local> docker-compose up app Starting local_db_1 ... done Starting local_app_1 ... done But when my app image is trying to connect to DB in logs of my app same error: FATAL: database "haircut_studio" does not exist Commented Apr 23, 2022 at 10:35
  • @tbjorch I provided a link to the GitLab repo Commented Apr 23, 2022 at 10:39

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.