Santosh Hegde
Santosh Hegde

Reputation: 3520

Build locally and the output to docker image vs Build while creating docker image

I hava java application which needs to be dockerized now and I have 2 options.

  1. Build locally and add the jar generated at target folder to docker image.
  2. Build it while creating the image. like,
    FROM maven:3-jdk-11 as builder

    COPY propel_settings_local.xml /root/.m2/settings.xml

    RUN mkdir -p /build

    ADD . /build/
    WORKDIR /build

    RUN mvn -B dependency:resolve dependency:resolve-plugins

    RUN mvn clean install

Which is better approach among these two options?

Upvotes: 0

Views: 752

Answers (2)

Adiii
Adiii

Reputation: 60104

Both options seems fine but I will say it totaly depend on your case but you can consider some of them below.

Build locally and add the jar generated at target folder to docker image.

  • This option suit when you are working on a local machine and need a frequent build and update of the image.
  • For that, you also need a proper local environment which is able to generate the jar
  • The developer also needs to set up a local java environment, in short for development copying build and mounting jar file with the container is fine to go with this approach.

Build it while creating the image.

  • This approach is good for CI/CD but follow the multi-stage to keep your docker image small or also you can check @Tushar Answer.
  • With this approach, your developer does not need to have Java Environment on the local system

Further, you can explore best practices and 7-best-practices-for-building-containers for your self.

Upvotes: 2

Tushar Mahajan
Tushar Mahajan

Reputation: 2160

I have springboot applications and have been following multi-stage build concept of docker

FROM maven:3-jdk-11 as builder

WORKDIR chooseTheOneYouWant

COPY . .  ( i.e. copy whole of your code into the work directory)

RUN mvn -B package

FROM openjdk:8

RUN mkdir /usr/app

COPY --from=build .jarFile /usr/app

WORKDIR /usr/app

CMD ["java", "-jar", "/usr/app/.jarFile"]

Upvotes: 1

Related Questions