Docker Hub Credentials in Jenkins
July 16, 2025
Never hardcode your Docker Hub password in the Jenkinsfile โ store it in Jenkins Credentials and reference it by ID.
Add the credentials
- Jenkins โ Manage Jenkins โ Credentials โ System โ Global โ Add Credentials
- Kind = Username and password
- Username = your Docker Hub username
- Password = Docker Hub password or, better, an access token
- ID =
dockerhub-credsโ the Jenkinsfile refers to this ID
- Save.
How the pipeline uses it
In the Jenkinsfile, credentials('dockerhub-creds') injects two variables:
$DOCKERHUB_CREDS_USR= the username$DOCKERHUB_CREDS_PSW= the password
environment {
DOCKERHUB_CREDS = credentials('dockerhub-creds')
}
Then log in securely, piping the password via stdin so it never appears in logs:
echo $DOCKERHUB_CREDS_PSW | docker login -u $DOCKERHUB_CREDS_USR --password-stdin
See also [[jenkins-jenkinsfile-explained]] and [[docker-hub-push-images]].