Home > Blockchain >  How to build binaries outside of Dockerfile for docker-compose build
How to build binaries outside of Dockerfile for docker-compose build

Time:07-07

I am new to docker-compose and probably don't understand the ideology behind. Short question -- I see there is docker-compose build command. I'd like to trigger a (gradle) script outside of the Dockerfile file as part of running it.

Let's say that I have a java web service inside Docker that I build in advance by gradle. I don't add gradle agent to the Dockerfile (as I am expected to keep it small, right?), I only COPY binaries

FROM openjdk:17-jdk-alpine
RUN apk add --no-cache bash
VOLUME /var/log/app/
WORKDIR /app

ARG EXTRACTED
COPY ${EXTRACTED}/application/ ./
...
ENTRYPOINT ["java","org.springframework.boot.loader.JarLauncher"]

And so I build this image by a script

./gradlew build
java -Djarmode=layertools -jar build/libs/*.jar extract --destination build/libs-extracted/
docker build --build-arg EXTRACTED=build/libs-extracted -t my-server .

I can define the following compose.yml. How do I trigger gradle inside of it? Or, same as with my single Dockerfile, I am expected to wrap docker-compose build into a build script?

version: '3.1'

services:

  db:
    image: postgres:alpine
    restart: always

  my-server:
    image: my-server
    restart: always
    depends_on:
      - db

Maybe I am asking for a hack, but actually I am happy to take another approach if it's cleaner

CodePudding user response:

I discovered multi-stage Dockerfile feature that addresses my task, and I can use it for both individual Dockerfile builds and docker-compose builds. https://stackoverflow.com/a/61131308/1291049.

I will probably lose gradle daemon optimisations though.

CodePudding user response:

Change your docker compose file:

version: '3.1'

services:

  db:
    image: postgres:alpine
    restart: always

  my-server:
    build: 
      context: ./my-server
      dockerfile: Dockerfile
    restart: always
    depends_on:
      - db
  • Related