yatta
yatta

Reputation: 519

Docker credentials in Jenkins pipeline

I'm trying to create a basic Jenkins pipeline which create a Docker image and push to the Docker Hub.

I couldn't find how to pass Docker Hub credentials to the Pipeline bash script

So I created credential with "dockerlogin" ID in Jenkins.

My pipeline script is below.

cd /home/jenkins-workspace/workspace/Demo
docker build -t test123/demo:latest . 
docker login --username=MYUSERNAME --password=MYPASSWORD
docker push test123/demo:latest

How can I pass this credentials(username and password) to the bash script?

Thanks!

Upvotes: 0

Views: 5239

Answers (1)

Altaf
Altaf

Reputation: 3076

To push an image to Docker Hub, you must first name your local image using your Docker Hub username and the repository name that you created through Docker Hub on the web.

When pushing an image to Docker Hub, you must specify your Docker Hub username as part of the image name.

 docker push <hub-user>/<repo-name>:<tag>

The reason for this is because Docker Hub organizes repositories by user name. Any repository created under my account includes my username in the Docker image name.

Example: If my dockerhub userid is mydockerhubuser, then :

# docker build -t <your-dockerhub-username>/push-example .
docker build -t mydockerhubuser/demo:latest .
# Enter your DOCKER_USERNAME and DOCKER_PASSWORD
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"
# docker push <username>/<tagname>
docker push mydockerhubuser/demo:latest 

For more info : https://docs.docker.com/docker-hub/repos/
For login non-interactively you can also refer: https://docs.docker.com/engine/reference/commandline/login/#provide-a-password-using-stdin

To run the docker login command non-interactively, you can set the --password-stdin flag to provide a password through STDIN. Using STDIN prevents the password from ending up in the shell’s history, or log-files.
The following example reads a password from a file, and passes it to the docker login command using STDIN:
cat ~/my_password.txt | docker login --username mydockerhubuser --password-stdin

Upvotes: 1

Related Questions