Building Docker Images with Jenkins
Harry
· 12 Sep 2026
· 15 views
The Workflow
Typical CI/CD: build the jar → build an image → push to a registry → deploy. Jenkins has first-class Docker support.
Dockerfile in the Repo
FROM eclipse-temurin:21-jre
WORKDIR /app
COPY target/app-*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]Build and Push
pipeline {
agent any
environment {
IMAGE = "docker.io/you/app"
TAG = "1.0.${BUILD_NUMBER}"
}
stages {
stage('Build jar') { steps { sh 'mvn -B clean package' } }
stage('Build image') {
steps {
sh "docker build -t $IMAGE:$TAG ."
}
}
stage('Push') {
steps {
withCredentials([string(credentialsId: 'dockerhub', variable: 'DOCKER_PASS')]) {
sh 'echo "$DOCKER_PASS" | docker login -u you --password-stdin'
sh "docker push $IMAGE:$TAG"
}
}
}
}
}Docker inside Docker
When the agent is itself a container, mount the host socket carefully: -v /var/run/docker.sock:/var/run/docker.sock. For production prefer a dedicated Docker-in-Docker agent or shared socket workers, and never run builds as root.
Tagging Strategy
- Use unique tags per build:
app:1.0.42or the Git SHA. - Promote a validated tag to
latestonly after tests pass. - Keeps every release reproducible and rollback-friendly.
Key Points
- Images make releases immutable: what you tested is what runs.
- Sign in to the registry with injected credentials.
- Unique tags = auditable, reversible deploys.