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:
- Deploy a PostgresSQL container with init.sql file
- Generate JOOQ code for application (in image build phase)
- 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