armijo
armijo

Reputation: 57

SpringBoot bootBuildImage with docker-compose build?

Spring Boot recommends using it's Maven/Gradle plugins to build a Docker image (actually, an OCI image which is similar). From https://spring.io/guides/topicals/spring-boot-docker, with Gradle, you run:

gradle bootBuildImage --imageName=myorg/myapp

docker-compose lets you specify a build config with a Dockerfile like this:

  backend:
    image: example/database
    build:
      context: backend
      dockerfile: ../backend.Dockerfile

Is there any way for a docker-compose build to reference the Spring Boot gradle bootBuildImage step instead of a Dockerfile?

I can build a Spring Booth image with gradle bootBuildImage

I can configure a more traditional Dockerfile based image build in docker-compose with build.dockerfile.

Is there any way to specify the Spring Boot image build in a docker-compose configuration?

Upvotes: 2

Views: 294

Answers (2)

buzz8year
buzz8year

Reputation: 416

You are doing almost right, in docker-compose.yml:

services:
  backend:
    image: myorg/myapp

Just create a script that first builds the image and then starts the Docker Compose services:

#!/bin/bash
gradle bootBuildImage --imageName=myorg/myapp
docker-compose up

Yet, if you still prefer to use traditional Dockerfile & keep everything within Docker Compose, you can use the buildpacks feature directly in your docker-compose.yml:

services:
  backend:
    build:
      context: backend
      dockerfile: ../backend.Dockerfile
      args:
        BUILDPACKS: "gcr.io/buildpacks/builder"

Execute Spring buildpacks when calling docker-compose build command

Upvotes: 0

Sai Kalyankar
Sai Kalyankar

Reputation: 11

Docker Compose can't directly use bootBuildImage. Instead:

  1. Build the image:

    gradle bootBuildImage --imageName=myorg/myapp
    
  2. Reference it in docker-compose.yml:

    services:
      backend:
        image: myorg/myapp
    
  3. Automate with:

    gradle bootBuildImage && docker-compose up
    

This separates image building from Compose.

Why this approach?

bootBuildImage is a build tool, while docker-compose expects an existing image or a Dockerfile for context. By building the image first, Compose can easily use it.

Additional Notes For more details on bootBuildImage, refer to the Spring Boot Docker Guide.

Upvotes: 1

Related Questions