Docker Jenkins 管道中的凭据

Docker credentials in Jenkins pipeline

我正在尝试创建一个基本的 Jenkins 管道,它创建一个 Docker 图像并推送到 Docker 集线器。

我找不到如何将 Docker 集线器凭据传递给管道 bash 脚本

所以我在 Jenkins 中创建了带有“dockerlogin”ID 的凭证。

我的管道脚本如下。

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

如何将此凭据(用户名和密码)传递给 bash 脚本?

谢谢!

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.

示例: 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 运行 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