Creating JOOQ objects during the Docker image build process using a PostgreSQL container

I’m working on a side project and I want to create a docker-compose in order to deploy the project locally successfully.
The main flow is:

  1. Deploy a PostgresSQL container with init.sql file
  2. Generate JOOQ code for application (in image build phase)
  3. Copy the generated code to the image and run app

However, no matter what I do, the container I’m trying to build cannot connect to the PostgresSQL container and generate the code.
I’ve tried using links, networks and nothing seems to work.

Docker Compose:

services:
  database:
    container_name: database
    hostname: database
    image: postgres:latest
    ports:
      - "5432:5432"
    environment:
      - PGUSER=user
      - POSTGRES_USER=user
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=db
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}" ]
      interval: 2s
      timeout: 5s
      retries: 5
    networks: 
      -mynet

  server:
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      database:
        condition: service_healthy
    ports:
      - "8080:8080"
    networks:
      -mynet

networks:
  mynet:
    driver: bridge

Dockerfile:

FROM maven:3.9.7-amazoncorretto-17 AS build
COPY models models

RUN mvn clean package -f models/pom.xml -Plocal

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.abl.rjmdb</groupId>
    <artifactId>models</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jooq.version>3.19.8</jooq.version>
        <postgresql.version>42.7.3</postgresql.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq</artifactId>
            <version>${jooq.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-meta</artifactId>
            <version>${jooq.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq-codegen</artifactId>
            <version>${jooq.version}</version>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
        </dependency>
    </dependencies>

    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <database.driver>org.postgresql.Driver</database.driver>
                <database.url>jdbc:postgresql://database:5432/db</database.url>
                <database.user>user</database.user>
                <database.password>password</database.password>
                <database.schema>public</database.schema>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.jooq</groupId>
                        <artifactId>jooq-codegen-maven</artifactId>
                        <version>${jooq.version}</version>

                        <executions>
                            <execution>
                                <phase>generate-sources</phase>
                                <goals>
                                    <goal>generate</goal>
                                </goals>
                            </execution>
                        </executions>

                        <configuration>
                            <jdbc>
                                <driver>${database.driver}</driver>
                                <url>${database.url}</url>
                                <user>${database.user}</user>
                                <password>${database.password}</password>
                            </jdbc>
                            <generator>
                                <database>
                                    <includes>.*</includes>
                                    <inputSchema>${database.schema}</inputSchema>
                                </database>
                                <target>
                                    <packageName>com.abl.rjmdb.model.jooq</packageName>
                                    <directory>${project.build.directory}/generated-sources/jooq</directory>
                                </target>
                            </generator>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

The error I get:

if I use “database” host –

11.96 [ERROR] Failed to execute goal org.jooq:jooq-codegen-maven:3.19.8:generate (default) on project models: Error running jOOQ code generation tool: The connection attempt failed. Unknown host database -> [Help 1]

if I use “127.0.0.1” host –

Failed to execute goal org.jooq:jooq-codegen-maven:3.19.8:generate (default) on project models: Error running jOOQ code generation tool: Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. Connection refused

if I use the IP address I got from docker inspect <container name>:

Failed to execute goal org.jooq:jooq-codegen-maven:3.19.8:generate (default) on project models: Error running jOOQ code generation tool: Connection to 172.17.0.2:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. Connection refused

Any ideas how I can approach this?
I’ve checked countless times, the PostgresSQL container runs and I can connect to it from the host.
Maybe it is not a good practice to generate JOOQ code inside a docker image?
I’m attaching the repository in case it is somehow relevant: https://github.com/AmitBarlev/RJMDB

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật