Push a Spring Boot app to Dockerhub |Video upload date:  · Duration: PT10M32S  · Language: EN

Step by step guide to build a Spring Boot jar create a Docker image and push the image to Docker Hub with practical commands and tips

If you want your Spring Boot service to leave your laptop and live in the wild you need a jar a Docker image and a Docker Hub repo. This guide walks through building a Maven jar creating a Dockerfile building the image and pushing it to Docker Hub with a few practical tips for smaller images and smoother CI CD.

Build the Spring Boot jar

Use your normal build tool to produce the runnable artifact. For Maven run the familiar command and try not to cry when the tests fail.

mvn clean package

The build produces a jar in the target folder. Make a note of the jar name and path because the Dockerfile will need to copy it into the image.

Create a Dockerfile for the app

Keep the Dockerfile minimal but sensible. For production use prefer a multi stage build so your final image does not include all the build baggage. Here is a compact example using a small runtime base image.

# Multistage build to keep final image small
FROM maven:3.8.8-jdk-17 AS builder
WORKDIR /workspace
COPY pom.xml mvnw .
COPY src ./src
RUN mvn clean package -DskipTests

FROM openjdk:17-jdk-slim
WORKDIR /app
COPY --from=builder /workspace/target/myapp.jar ./app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

If you do not want a multistage build you can copy the jar produced by your CI pipeline directly into a simpler runtime image. The goal is a compact image that starts fast and behaves predictably in containers and Kubernetes.

Use a .dockerignore file

A .dockerignore file prevents copying Maven artifacts source files and node modules into the image build context. Typical entries include target .idea .git and local config files. This speeds up builds and avoids accidentally leaking secrets into images.

Build the Docker image locally

Tag the image to match the Docker Hub repository name that you will push to. Keep the tag simple while you are testing.

docker build -t username/spring-boot-app .

The image will be stored locally. Use small base images and make sure your jars are trimmed of unnecessary libraries if you care about image size and startup speed.

Login to Docker Hub and push the image

Authenticate with Docker Hub from the command line and then push. If you have a CI system you will automate these steps later so you do not have to type them while bleary eyed at 2 a m.

docker login
# then
docker push username/spring-boot-app

Pushing uploads layers to Docker Hub where your repository becomes the source of truth for deployments CI pipelines and other team members.

Tagging strategies and CI CD tips

  • Use semantic tags in CI CD like v1 1.2.3 or commit hashes for reproducibility.
  • Automate image builds in your CI system and push to Docker Hub from the pipeline to avoid human error.
  • Prefer automated vulnerability scans and image signing in production delivery workflows.
  • Keep your Dockerfile and base images up to date to reduce runtime security issues in production.

Summary and practical tips

In short build your Spring Boot jar with Maven copy it into a clean Dockerfile prefer multi stage builds and smaller runtimes login to Docker Hub and push the image. Automate the process in CI CD so you can sleep and still deploy without touching the terminal.

Quick checklist

  • Add a .dockerignore file to speed builds and avoid leaks
  • Use multi stage builds to shrink final image size
  • Tag images consistently and push from CI for repeatability
  • Scan and update base images regularly as part of DevOps hygiene

Now go push that image and pretend you meant to do it all along.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.