๐Ÿ’พThananjhayan
โ† Back to notes

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

  1. Jenkins โ†’ Manage Jenkins โ†’ Credentials โ†’ System โ†’ Global โ†’ Add Credentials
  2. 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
  3. 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]].

๐Ÿ’ฌ Comments