How to push docker image to Nexus repository through Jenkinsfile? - docker

I have a Jenkinsfile pipeline creating 3 docker images/containers as below:
stage('Build images')
{
echo "workspace directory is ${workspace}"
dir ("$workspace/build/virtuoso")
{
sh 'docker build -t virtuoso -f $WORKSPACE/build/virtuoso/Dockerfile .'
}
dir ("$workspace/build/wildfly")
{
sh 'docker build -t wildfly -f $WORKSPACE/build/wildfly/Dockerfile .'
}
dir ("$workspace/build/postgres")
{
sh 'docker build -t postgres -f $WORKSPACE/build/postgres/Dockerfile .'
}
}
I need to push these 3 images to Nexus repository manager. Can somebody help me on this?

The solution to this is:
stage('Push Docker Images to Nexus Registry'){
sh 'docker login -u user -p password NexusDockerRegistryUrl'
sh 'docker push NexusDockerRegistryUrl/Imagename}'
sh 'docker rmi $(docker images --filter=reference="NexusDockerRegistryUrl/ImageName*" -q)'
sh 'docker logout NexusDockerRegistryUrl'
}
The above stage in Jenkins pipeline would push the docekr image to nexus docker registry and remove existing image.
Thanks

Related

Docker Permission denied script sh

I'm using jenkins docker image to run jenkins.
When i try to build docker image it is saying to Docker Permission Denied.
I'm running it on MAC OS! How can i add jenkin user to Docker Group?
Dockerfile:
FROM python:3.7-alpine
CMD [ "python", "-c", "print('Hi there!')"]
Jenkinsfile:
pipeline {
agent any
stages {
stage("build") {
steps {
sh """
docker build -t hello_there .
"""
}
}
stage("run") {
steps {
sh """
docker run --rm hello_there
"""
}
}
}
}
Try run your jenkins container like this
It works in Ubuntu I think it should be ok in other OS too.
sudo docker run -d -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 -v /var/run/docker.sock:/var/run/docker.sock 4oh4/jenkins-docker

How to access within Docker Container in Jenkins Pipeline

Current Setup requires Docker Components (Dockerfile, requirements.txt, Jenkinsfile) to be separate from Source Code. Docker Components resides in a directory whereas the Source Code resides in another directory. Design so far is to build the Docker Image with the Docker Components and run the Docker Image, and the Source Code is inserted into the root directory of the container. Model so far works on the CMD terminal however when translating over to the Jenkins Pipeline, the docker exec command is not recognised on the Jenkins Pipeline
Stages run well on the Jenkins Pipeline until the stage to insert Source Code as a volume. When running $ docker exec -it source-container bash, below is the error log:
C:\WINDOWS\system32\config\systemprofile\AppData\Local\Jenkins\.jenkins\workspace\Source-Code-Pipeline>docker exec -ti source-container bash
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
When prefixing with 'winpty', pipeline does not execute well too. How can I go about in resolving this issue?
Below is the Jenkinsfile to depict the Pipeline flow:
node {
checkout scm
stage ('Create Docker Registry') {
bat 'docker run -d -p 5000:5000 --restart=always --name registry registry:2'
}
stage ('Build Docker Image') {
def image = docker.build("docker-csv", '.')
}
stage ('Tag and Push Docker Image') {
bat 'docker tag docker-csv localhost:5000/docker-csv'
bat 'docker push localhost:5000/docker-csv'
}
stage ('Pull Docker Image from Local Registry') {
bat 'docker pull localhost:5000/docker-csv'
}
stage ('Insert Source Code as Volume into Container') {
bat 'docker run --name source-container -d -v /c/Users/z0048yrk/Desktop/Source-Code:/root localhost:5000/docker-csv tail -f /dev/null'
bat 'docker exec -it source-container bash'
bat 'cd root'
bat 'python test.py > output.csv'
}
stage ('Copy output.csv into desired directory') {
dir("C:\\Users\\z0048yrk\\Desktop\\LTA\\new-demo") {
bat 'docker cp source-container:/root/output.csv'
}
}
}

How to pass insecure registry as args in pipeline using DIND image

can someone please let me know on how to pass insecure registry as an argument when using DIND image as runtime container.
Please find the below sample script which i had been using as docker run time agent.
'''
pipeline {
agent {label 'jenkins-docker-slave'}
stages{
stage('maven version'){
agent {docker { image 'maven:latest'}}
steps{
script{
sh "mvn --version"
}
}
}
stage('docker build'){
agent {
docker {
image 'docker:dind'
args '-v /var/run/docker.sock:/var/run/docker.sock'
}
}
steps{
script{
sh 'docker version'
}
}
}
}
}
'''
Docker version works fine but my motive is to set insecure registry as below during the runtime so that i can use docker login and docker push commands successfully.
{
"insecure-registries": [
"ec2-52-39-183-6.us-west-2.compute.amazonaws.com:8123"
]
}
I want to make the above condition work during the runtime. Can someone please advise on how this can be achived with DIND?
I have found a solution for my query on my own. Below is the solution:
sh 'docker exec --tty $(docker ps -ql) sh -c "mkdir -p /etc/docker"'
sh 'docker exec --tty $(docker ps -ql) sh -c "mkdir -p /root/.docker"'
sh '''
docker exec --tty $(docker ps -ql) sh -c "cat <<EOF > /etc/docker/daemon.json
{
"insecure-registries": [
"ec2-52-36-87-109.us-west-2.compute.amazonaws.com:8123"
]
}"
'''
But the only problem is , the above used port 8123 for the docker repository connector is not opened due to which getting the below error.
Error response from daemon
Is there any way to open ports within docker container to get this worked?

how to create docker container using jenkins pipeline script

node{
stage('Scm Checkout'){
git credentialsId: 'git-creds', url: 'https://github.com/mouthik/jenkinsfile.git'
}
stage('Build docker image'){
sh 'docker build -t mouthik/my-app:2.0.0 .'
stage('Run docker container'){
sh 'docker run -p 8080:8080 -d -name my-app mouthik/my-app:2.0.0'
}
}
}
You need to install docker on the slave.
Try
sudo dnf install docker-ce
and start the docker service
to build a docker image you need to write Dockerfile
and then docker build https://docs.docker.com/engine/reference/commandline/build/

Jenkins can not execute docker login via ssh-agent

I create the Jenkins pipeline to deploy my app. I built and push docker image to AWS ECR. The final step is executing ssh to deployment server (EC2) and run docker container based on last built image.
This is my script:
stage('Deploy') {
steps {
script {
sshagent(['ssh-cridentials']) {
sh "ssh -o StrictHostKeyChecking=no jenkins#host sudo docker rm -f myapp"
sh "ssh -o StrictHostKeyChecking=no jenkins#host sudo docker image prune -a -f"
sh "ssh -o StrictHostKeyChecking=no jenkins#host \"cat /opt/aws/password.txt | sudo docker login --username AWS --password-stdin $ecrURI & sudo docker run -p 80:80 -d --name=myapp $imageURI\""
}
}
}
}
However, Jenkins built fail and I got the error:
docker: Error response from daemon: Get https://xxx: no basic auth credentials.
This command couldn't login to ECR.
But it works successfully if I execute the same command on deployment server.
Looks like something wrong with your escape character, try without using that (I believe you have valid ecr url in variable $ecrURI)
sh "ssh -o StrictHostKeyChecking=no jenkins#host cat /opt/aws/password.txt | sudo docker login --username AWS --password-stdin $ecrURI & sudo docker run -p 80:80 -d --name=myapp $imageURI"

Resources