skip to Main Content

I am having an issue with finding or creating a docker image that I can use on GitLab CI that will allow me to run mvn package, that in turn calls spring-boot:build-image

I have specified in my config that I need the dind container as a service, but I am unsure the best way to find a docker container that has docker, java and maven installed.

I am also not sure exactly how to create my own that I can then share for this purpose.

I am using dev containers for this project so I might try to snapshot that but that doesn’t seem the best for repeatability.

2

Answers


  1. Chosen as BEST ANSWER

    Using the docker image docker:24.0.7 I was able to add java 21 to it as part of the before_script to allow me to run both the docker commands and the java/maven (using the wrapper) ones.

    The Docker image is built on Alpine so you can use before_script: - apk add --update openjdk21

    You will need to pass the parameters to the pom file as variables for it to authenticate with GitLab for the push, logging on via the docker cli did not work.

                      <configuration>
                        <image>
                            <name>registry.gitlab.com/ssmale-spring-boot/springboot:${project.version}</name>
                        </image>
                        <docker>
                            <publishRegistry>
                                <url>${docker.publishRegistry.url}</url>
                                <username>${docker.publishRegistry.username}</username>
                                <password>${docker.publishRegistry.password}</password>
                            </publishRegistry>
                        </docker>
                    </configuration>
    

    .gitlab-ci.yaml

    variables:
      # Use TLS https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled
      DOCKER_HOST: tcp://docker:2376
      DOCKER_TLS_CERTDIR: "/certs"
    
    stages: 
      - build
    
    create-and-publish-docker-image:      
      stage: build
      image: docker:24.0.7
      before_script:
        - apk add --update openjdk21 
      services:
        - name: docker:24.0.7-dind
          alias: docker
      script:
        - echo "Compiling the code, building docker image and publishing..."
        - ./mvnw package 
          -Ddocker.publishRegistry.username=$CI_REGISTRY_USER 
          -Ddocker.publishRegistry.password=$CI_REGISTRY_PASSWORD 
          -Ddocker.publishRegistry.url=$CI_REGISTRY
        - echo "Maven Package complete"
      artifacts:
        name: "Learning"
        when: on_success
        expire_in: "30 days"
        paths:
          - "distro/target/distro-*.jar"
    

  2. Why not wrap maven in your project? If you then use a docker image with a jdk you can build and run it easily.

    Please be aware that running with jdk is not best practice for production code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search