gvenzl / oci-oracle-free

Build scripts for Oracle Database FREE container/docker images
Apache License 2.0
127 stars 32 forks source link

Customize container healthcheck in docker compose #53

Closed niktekusho closed 3 weeks ago

niktekusho commented 1 month ago

Hello, thank you for the awesome image.

Coming from a spring boot project with the spring-boot-starter-data-jdbc and spring-boot-docker-compose dependencies, I'm noticing the spring boot application starts too fast for the Oracle container. The spring boot app bootstrap fails with "Unable to establish JDBC connections".

From what I've read spring-boot-docker-compose should delay the application bootstrap until the containers defined in the compose.yml are up and running. My idea was to "artificially" delay the healthcheck of the oracle container so that it has more time to boot up.

Currently, in the README, there're the following lines:

https://github.com/gvenzl/oci-oracle-free/blob/b5decfee76b7d3b6898f3a4852f8dc1f159fbd4b/README.md?plain=1#L106-L111

which is not a valid compose syntax.

Is that options key supposed to be healthcheck instead?

If you have a better approach to solve this, please feel free to share ;)

Small sample

compose.yml

services:
  mongodb:
    image: 'mongo:6.0'
    environment:
      - 'MONGO_INITDB_DATABASE=mydatabase'
      - 'MONGO_INITDB_ROOT_PASSWORD=secret'
      - 'MONGO_INITDB_ROOT_USERNAME=root'
    ports:
      - '27017'
  oracle:
    image: 'gvenzl/oracle-free:23.4-slim-faststart'
    environment:
      - 'ORACLE_DATABASE=audit_dev'
      - 'ORACLE_PASSWORD=secret'
    ports:
      - '1521'

build.gradle

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.1'
    id 'io.spring.dependency-management' version '1.1.5'
    id 'org.graalvm.buildtools.native' version '0.10.2'
    id 'org.asciidoctor.jvm.convert' version '3.3.2'
}

group = 'example'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

ext {
    set('snippetsDir', file("build/generated-snippets"))
}

dependencies {

    // Lombok (autogen repetitive code)
    annotationProcessor 'org.projectlombok:lombok'
    compileOnly 'org.projectlombok:lombok'

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    developmentOnly 'org.springframework.boot:spring-boot-docker-compose'
    runtimeOnly 'com.oracle.database.jdbc:ojdbc11'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.boot:spring-boot-testcontainers'
    testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
    testImplementation 'org.testcontainers:junit-jupiter'
    testImplementation 'org.testcontainers:mongodb'
    testImplementation 'org.testcontainers:oracle-free'

    // Mockito (test stub and mocks)    
    testImplementation 'org.mockito:mockito-core'
    testImplementation 'org.mockito:mockito-junit-jupiter'

    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    outputs.dir snippetsDir
    useJUnitPlatform()
}

tasks.named('asciidoctor') {
    inputs.dir snippetsDir
    dependsOn test
}
gvenzl commented 1 month ago

Hey @niktekusho,

Thanks a lot for using these images!

The healthcheck documentation in the ReadMe is for a GitHub Actions yaml file, not for Docker compose. According to the Docker compose reference, however, a similar healthcheck operation can be performed with something like this:

healthcheck:
  test: ["CMD", "healthcheck.sh"]
  interval: 10s
  timeout: 5s
  retries: 10
  start_period: 5s
  start_interval: 5s

Hope this helps!

niktekusho commented 1 month ago

Your solution worked wonders; thank you! Would you accept a PR that adds that bit to the docs? I was thinking about expanding the healthcheck section by mentioning the docker-compose syntax for future users.

gvenzl commented 1 month ago

Awesome, glad to hear that @niktekusho!

Yeah sure, PRs are always welcome, but I'm also happy to put the info into the ReadMe for you, up to you. :)

gvenzl commented 3 weeks ago

Merged and live!

Thanks a lot for your contribution, @niktekusho!