Jenkins Pipeline push the Docker Image - docker

I'm trying to push my docker image after building source code, when jenkins push images to docker Hub Registry i am getting below error.
Pipeline Script
stage('Build Docker Image') {
container('docker') {
echo 'docker'
sh "docker build -t ${image_name} ."
sh "docker tag ${image_name} ${image_name}:${image_tag}"
}
}
stage('Push Docker Image') {
container('docker') {
withCredentials([string(credentialsId: 'DOCKER_HUB_CREDENTIALS', variable: 'DOCKER_HUB_CREDENTIALS')]) {
sh "docker login -u user-name -p ${DOCKER_HUB_CREDENTIALS}"
}
sh "docker push ${image_name}:${image_tag}"
}
}
Jenkins Logs
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Push Docker Image)
[Pipeline] container
[Pipeline] {
[Pipeline] withCredentials
Masking supported pattern matches of $DOCKER_HUB_CREDENTIALS
[Pipeline] {
[Pipeline] sh
+ docker login -u user-name -p ****
Login Succeeded
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] sh
+ docker push devopsimage.azure/frontend:bug-fix-2cbb925d
The push refers to repository [devopsimage.azure/frontend]
Get https://devopsimage.azure/v2/: dial tcp: lookup devopsimage.azure.io: Temporary failure in name resolution
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // podTemplate
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
Can you please any one help me on this ?

If you have dot(.) in your image name before the slash, docker treats it as the registry name. So docker push is trying to push to registry named devopsimage.azure. If you actually want to push to Docker Hub instead, remove the dot from the image name. If you want to push to registry named devopsimage.azure, then there is a DNS issue in resolving this registry from your build machine.

Related

Jenkins pipeline docker.push() gives error "docker tag" requires exactly 2 arguments

I'M trying to push the docker image built, onto private registry but encountering this error:
Successfully built 6059208ca310
**Successfully tagged ubuntu:18.04**
[Pipeline] echo
Image Build
[Pipeline] isUnix
[Pipeline] sh
+ docker tag ubuntu:18.04 --build-arg PROXY_ADDRESS=http://proxy-random.net:3100 --build-arg my_image=ubuntu:18.04 x.some.com/ubuntu:18.04 --build-arg PROXY_ADDRESS=http://proxy-random.net:3100 --build-arg my_image=atpdml_pet_docker/ubuntu:ubuntu:18.04
"docker tag" requires exactly 2 arguments.
See 'docker tag --help'.
Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
[Pipeline] }
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Whereas I have not called docker tag anywhere in my pipeline.groovy, I suppose its a part of push(). My docker.build() uses --build-arg and require to push the tagged image (using either name in this case ubuntu:18.04 or the tag id 6059208ca310) onto the registry.
def build_and_upload_image(dockerFileDir, imageName, imageTag) {
script {
dir(dockerFileDir) {
docker.withRegistry('https://some.com','key') {
def image = docker.build("${imageName}:${imageTag} --build-arg PROXY_ADDRESS=http://proxy-random.net:3100 --build-arg my_image=ubuntu:18.04")
echo 'Image Build'
image.push("ubuntu:18.04") ### even tried image.push()
image.push("latest")
echo 'image pushed'
}
}
}
}
pipeline {
agent{
label 'docker-build'
}
stages {
stage('Build format checker docker') {
steps {
build_and_upload_image("docker","ubuntu","18.04")
}
}
}
}
You need change
def image = docker.build("${imageName}:${imageTag} --build-arg PROXY_ADDRESS=http://proxy-random.net:3100 --build-arg my_image=ubuntu:18.04")
to
def image = docker.build("${imageName}:${imageTag}", "--build-arg PROXY_ADDRESS=http://proxy-random.net:3100 --build-arg my_image=ubuntu:18.04 .")
References:
Jenkins Pipeline docker.build() gives error '"docker build" requires exactly 1 argument(s)'
SO the first step was to change the syntax as mentioned by #Tarun Lalwani. After which it was a issue of tag (not a valid repository/tag: invalid reference format), which I was able to resolve by mentioning the {env.BUILD_NUMBER} in the push() as mentioned in this answer (https://stackoverflow.com/a/53858257/7513466), although it does not take the tag name that was required but this does the job of pushing the image to the repo by its tag ID.

Jenkins pipeline fails to login to docker hub

I am trying to use pipeline to run a few things and when i started running my pipeline it failed to login to docker.
the weird thing is that i am able to login on the machine itself but when i run the pipeline is fails with this weird error:
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] stage
[Pipeline] { (Front-end)
[Pipeline] node
Running on test-env in /var/www/test-env/workspace/client-e2e
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
Using the existing docker config file.Removing blacklisted property: auths$ docker login -u ***** -p ******** https://hub.docker.com/?namespace=******
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
Error response from daemon: login attempt to https://hub.docker.com/v2/ failed with status: 404 Not Found
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] }
[Pipeline] // stage
[Pipeline] End of Pipeline
ERROR: docker login failed
Finished: FAILURE
I don't know why it performs a login when this image is publicly available for everyone.
can someone help me ?
this is the pipeline itself:
pipeline {
agent none
stages {
stage('Front-end') {
agent {
docker {
image 'node:8-alpine'
label "test-env"
}
}
steps {
sh 'node --version'
}
}
}
}
You can try with Credentials Binding Plugin in:
steps {
sh 'node --version'
}
You can do:
withCredentials([string(credentialsId: 'mytoken', variable: 'TOKEN')]) {
sh '''
docker login -u '<your_user>' -p '<$TOKEN>'
node --version
'''
}
There is an example here:
https://issues.jenkins-ci.org/browse/JENKINS-41051
Ok, so after a while i found out that it was as simple as doing this
pipeline {
agent none
stages {
stage('Front-end') {
agent {
docker {
image 'node:8-alpine'
registryUrl 'https://index.docker.io/v1/'
label "test-env"
}
}
steps {
sh 'node --version'
}
}
}
}
this was aded: registryUrl 'https://index.docker.io/v1/'

Jenkins is adding private regisrty URL tag while pulling Image

I am getting this issue in Jenkins pipeline where I want to pull 'node' image but jenkins is adding the private docker registry url tag to it so the image is not found (artifactory.x.com/node:7-alpine)
Here is the pipeline
pipeline {
agent {
docker
{
image 'node:7-alpine'
registryUrl 'https://artifactory.x.com/'
registryCredentialsId 'jenkins-artifactory'
}
}
stages {
stage('Test') {
steps {
sh 'node --version'
}
}
}
}
This is the error I am getting
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/jobs/enterprise-master/workspace
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
$ docker login -u jenkins -p ******** https://artifactory.x.com/
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /var/jenkins_home/jobs/enterprise-master/workspace#tmp/f54c8b21-837b-4652-b12c-d489fb7e4c4c/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[Pipeline] {
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . node:7-alpine
Error: No such object: node:7-alpine
[Pipeline] isUnix
[Pipeline] sh
+ docker inspect -f . artifactory.x.com/node:7-alpine
Error: No such object: artifactory.x.com/node:7-alpine
[Pipeline] isUnix
[Pipeline] sh
+ docker pull artifactory.x.com/node:7-alpine
Error response from daemon: unknown: Not Found
[Pipeline] }
[Pipeline] // withDockerRegistry
Now the problem is that there is no image artifactory.x.com/node:7-alpine so it cant be found.
How do I tell jenkins not to add the private repo URL while pulling.
Fixed this by removing Docker Registry URL and setting Registry credentials to none
Jenkins -> Manage Jenkins->Configure > Pipeline Model Definition
Also the pipeline definition is still same
pipeline {
agent {
docker
{
image 'node:7-alpine'
registryUrl 'https://artifactory.X.com/'
registryCredentialsId 'jenkins-artifactory'
}

Jenkins pipeline build a dockerfile which contains a base image of my dockerhub repository

I am extremely new to Jenkins. I tried out few basic pipeline examples which worked.
My concrete use case is following:
I have a base image in my docker hub repository : my_dockerhub_rep/myImage:v1
Now I want to build another image based on this base image through a Jenkins pipeline.
So i wrote the following dockerfile :
FROM my_docker_hub_rep/myImage:v1
RUN /bin/bash -c 'echo entering in MC container'
To build this image from Jenkins, i wrote the following Jenkinsfile:
pipeline {
agent { dockerfile {
filename "/home/user/Desktop/Dockerfile"
registryUrl ""
registryCredentialsId 'dockerHub'
}}
stages {
stage('Build') {
steps {
echo 'hello !'
sh 'echo LM_LICENSE_FILE = $LM_LICENSE_FILE'
}
}
The jenkins server can successfully login to the docker repository at first but then as soon as it tries to fetch the base image it throws an error that pull access denied : repository doesnt exist or requires docker login.
What i dont understand is if it could login into the docker repo once then why not again ?
Here is the console output of jenkins :
Started by user unknown or anonymous
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/docker_test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] withDockerRegistry
$ docker login -u mydockerID-p ******** https://index.docker.io/v1/
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /var/lib/jenkins/workspace/docker_test#tmp/a548cbfa-5d55-4a2c-87a7-4954052d7e5b/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Agent Setup)
[Pipeline] isUnix
[Pipeline] readFile
[Pipeline] sh
+ docker build -t b2f2e9020bdfdbcd1bc3d0a6f0f28b1c7abff41b -f /home/user/Desktop/Dockerfile .
Sending build context to Docker daemon 2.095kB
Step 1/8 : FROM my_docker_rep/myImage:v1
pull access denied for my_docker_rep/myImage, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withDockerRegistry
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
ps: i already added jenkins to the user group.
Your docker file should have a full image reference. <registry>/<repository>/<imagename>:<image_tag> if not, the docker demon will always try to pull image from the default docker registry. Change the FROM part of your DOCKERFILE to contain the full image path. It will work

Docker Container Jenkins Pipeline Script: Permission denied while executing the script

I am running jenkins inside a docker container. I have created a simple pipleline to checkout,build and run docker image, but I am getting the following error.
Below is my pipleline script:
node {
def mvnHome = tool name: 'Maven Path', type: 'maven'
stage('Git CheckOut') {
git branch: '2019_DOCKER_SERVICES', credentialsId: 'git-creds', url: 'http://10.10.10.84:8111/scm/git/JEE_M_SERVICES'
}
stage('Maven Build') {
// Run the maven build
withEnv(["MVN_HOME=$mvnHome"]) {
if (isUnix()) {
sh '"$MVN_HOME/bin/mvn" -f Services/user-service/pom.xml clean install'
} else {
// bat(/"%MVN_HOME%\bin\mvn" -f Services\\user-service\\pom.xml clean install/)
}
}
}
stage('Docker Image Build') {
sh '"Services/user-service/" docker build -t user-service'
}
}
But I am getting the follow error in last stage, the first two stages ran successfully.
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Docker Image Build)
[Pipeline] sh
+ Services/user-service/ docker build -t user-service
/var/jenkins_home/jobs/docker-demo/workspace#tmp/durable-a5c035cf/script.sh: 1: /var/jenkins_home/jobs/docker-demo/workspace#tmp/durable-a5c035cf/script.sh: Services/user-service/: Permission denied
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
You have to set up new Jenkins slaves using Docker
It's weird to run Docker inside the Docker container
To access low-level operations you have to run your Docker container privileged

Resources