2

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?

3
  • I am looking to answer this question too. the process that equivalent of "./gradlew bootBuildImage --imageName=imagename" in compose file. Commented Dec 27, 2024 at 21:18
  • I believe a script is required for this: First build Dockerimage using Gradle, then run docker-compose up Commented Dec 28, 2024 at 9:28
  • could not get if u still want to use traditional dockerfile or not, anyways check the answer post Commented Jan 3 at 12:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.