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.
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.
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.
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.
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.
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.
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
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.