Reputation: 731
So I'm trying to set up a pipeline in Jenkins to build image and push them to Docker hub. My credentials in Manage 'Jenkins' are called the same as "docker-hub-credentials" and seem to be used.
It can build, but it just doesn't get through the push... Help? I've been on that for hours and I'm not sure what I' missing.
I've already tried using docker login, but jenkins doesn't allow it.
stage('Build image') {
/* This builds the actual image; synonymous to
* docker build on the command line */
bat 'docker build -t username/foldername:build . ' }
stage('Push image') {
/* Finally, we'll push the image with two tags:
docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-credentials') {
bat 'docker push username/foldername:build'
}
}
I expect the image to be pushed, but I have this instead :
The push refers to repository [docker.io/username/foldername]
a73d7d9f4346: Preparing
964bdfb24a54: Preparing
1af124d739c9: Preparing
6cffeea81e5d: Preparing
614a79865f6d: Preparing
612d27bb923f: Preparing
ef68f6734aa4: Preparing
612d27bb923f: Waiting
ef68f6734aa4: Waiting
denied: requested access to the resource is denied
Upvotes: 3
Views: 13217
Reputation: 21
withCredentials([usernamePassword(credentialsId: 'somewhat', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
"echo $PASS | docker login -u $USER --password-stdin"
}
Hello, I suggest more convenient and secure way to pass password as it written in docker documentation, by using stdin
,it prevents the password from ending up in the shell’s history, or log-files
. Good luck ;)
Upvotes: 0
Reputation: 5545
I solved by add :
stage('Push Docker Image') {
steps {
withDockerRegistry([credentialsId: "docker_auth", url: "https://index.docker.io/v1/"]) {
bat "docker push IMAGE_NAME:latest"
}
}
}
Upvotes: 0
Reputation: 137
As of today, Docker pipeline plugin gives groovy methods to login to docker hub, build image and finally push it. Below is a sample code for a declarative pipeline
pipeline {
agent any
stages {
stage('Push container') {
steps {
echo "Workspace is $WORKSPACE"
dir("$WORKSPACE/dir-to-Dockerfile") {
script {
def REGISTRY_URL = "https://index.docker.io/v1/"
def USER_NAME = "your-registry-username"
docker.withRegistry("$REGISTRY_URL", 'DockerHub-Cred-ID-Defined-In-Jenkins') {
def image = docker.build("$USER_NAME/some-image-name:${env.BUILD_ID}")
image.push()
}
}
}
}
}
}
}
Upvotes: 0
Reputation: 182
If you want to push private repository then below commands you have to follow:
1st: you have to make sure dockerhub credentials on jenkins. 2nd: You have creat a job with pipeline project 3rd: Then you have to push your project with jenkinsfile. 4th: now you can build jenkins.
I am giving full Jenkinsfile it will help to solve this permission denied problem and successfully u can create docker image and push to dockerhub.
pipeline {
agent any
stages{
stage('Maven Install'){
agent any
steps{
bat 'mvn -f pom.xml clean install'
}
}
stage('Docker Build'){
agent any
steps{
bat 'docker build -t dockerhub_username/repository_name/docker-jenkins-integration .'
}
}
stage('Push image'){
agent any
steps{
withDockerRegistry([ credentialsId: "id", url: "" ]) {
bat "docker tag dockerhub_username/repository_name/docker-jenkins-integration dockerhub_username/repository_name:docker-jenkins-integration"
bat "docker push dockerhub_username/repository_name:docker-jenkins-integration"
}
}
}
}
}
Upvotes: 0
Reputation: 1
I have spent like half days to solve it. Please make sure Docker Pipeline Plugin
installed.
script {
withDockerRegistry([ credentialsId: "Docker-Hub-Cred", url: "https://index.docker.io/v1/" ]){
}
}
Upvotes: 0
Reputation: 116
I had the same issue recently, it was a wrong the docker registry url
The issue - https://index.docker.io/v1
The fix - https://index.docker.io/v1/
Yep, it was an issue. I was not able to push it to docker registry.
Upvotes: 1
Reputation: 731
I found the answer!!!
stage('Push image') {
withDockerRegistry([ credentialsId: "docker-hub-credentials", url: "" ]) {
bat "docker push devopsglobalmedia/teamcitydocker:build"
}
Upvotes: 6
Reputation: 71
A better option is to use Docker pipeline plugin (it comes in the recommended plugins).
node {
checkout scm
def dockerImage
stage('Build image') {
dockerImage = docker.build("username/repository:tag")
}
stage('Push image') {
dockerImage.push()
}
}
Doing it this way, you must specify the credentials of the docker registry in the Pipeline Model Definition.
Docker pipeline plugin has problems applying the credentials assigned in Pipeline Model Definition to projects with Multi-branch pipeline. That is, if using the above code you continue to receive the error:
denied: requested access to the resource is denied
Then you must specify the credentials in the Jenkinsfile as follows:
node {
checkout scm
def dockerImage
stage('Build image') {
dockerImage = docker.build("username/repository:tag")
}
stage('Push image') {
docker.withRegistry('https://registry-1.docker.io/v2/', 'docker-hub-credentials') {
dockerImage.push()
}
}
}
You can modify the URL to a custom registry if you need it
Upvotes: 1
Reputation: 91
In Image push
stage, you can do docker login
first and then push the image. Try the following for docker login:
stage('Push image') {
withCredentials([usernamePassword( credentialsId: 'docker-hub-credentials', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
def registry_url = "registry.hub.docker.com/"
bat "docker login -u $USER -p $PASSWORD ${registry_url}"
docker.withRegistry("http://${registry_url}", "docker-hub-credentials") {
// Push your image now
bat "docker push username/foldername:build"
}
}
}
Make sure the registry url is correct.
withCredentials([usernamePassword(...)])
method above will set two environment variables USER
and PASSWORD
which are your docker registry credentials from credentials id docker-hub-credentials
.
Upvotes: 5