theanilpaudel
theanilpaudel

Reputation: 3478

Simplify docker build and deploy process for Spring Boot app

I am using docker to deploy a spring boot app. So first I use

1. mvn package

to create a jar file in the specified directory in Docker file.

Then I delete the previously created docker image file, build new one, tag one and then push

2. docker image ls

3. docker image rm -f IMAGE_ID

4. docker build --tag=APP_NAME .

5. docker tag APP_NAME DOCKER_HUB_REPO/DOCKER_HUB_PROJECT:TAG

6. docker push DOCKER_HUB_REPO/DOCKER_HUB_PROJECT:TAG

Then I go to the server and stop a running container, delete previously created image and then deploy the newly created image

7. docker ps

8. docker stop CONTAINER_ID

9. docker image ls

10. docker image rm -f IMAGE_ID

11. docker run -d -p PORT:PORT DOCKER_HUB_REPO/DOCKER_HUB_PROJECT:TAG

This all seems very tiresome and boilerplate. Is there a better and a simplified way to perform this operation.

Upvotes: 1

Views: 280

Answers (1)

Ortomala Lokni
Ortomala Lokni

Reputation: 62506

You can use the Dockerfile Maven plugin. Add a similar configuration to your pom.xml:

<plugin>
  <groupId>com.spotify</groupId>
  <artifactId>dockerfile-maven-plugin</artifactId>
  <version>${dockerfile-maven-version}</version>
  <executions>
    <execution>
      <id>default</id>
      <goals>
        <goal>build</goal>
        <goal>push</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <username>repoUserName</username>
    <password>repoPassword</password>
    <repository>spotify/foobar</repository>
    <tag>${project.version}</tag>
    <buildArgs>
      <JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
    </buildArgs>
  </configuration>
</plugin>

Then using mvn deploy will build your Docker image. Read the documentation for details.

Upvotes: 1

Related Questions