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

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?

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'
}
}
}

No such found container error at Jenkins job even I was able to list the containers

I get "No such container error" at Jenkins even I was able to list containers before executing the command.
Here is the command that I executed for verifying. Maybe I am doing something wrong at my docker build command
docker run -d --rm --name $containerName -v **$workSpace/target:/build $imageName**
Whole Command:
ansiColor('xterm') {
def containerName = "dcktest" + (dockerContainerNumber + 1)
sh "docker container ls"
sh "docker run -d --rm --name $containerName -v $workSpace/target:/build $imageName"
sh "docker container ls"
try {
sh "docker exec -t $containerName /bin/bash \"/build/target/scripts/dockertest.sh\""
} finally {
sh "docker stop $containerName"
}
}
}
The result in the Jenkins pipeline is :
Thank you for helps

Is it possible to log-in a container (apache) with Jenkins pipeline, I tried the following:

pipeline {
agent any
options {
timeout(time: 5, unit: 'MINUTES')
disableConcurrentBuilds() }
stages {
stage('Checkout') {
steps {
script {
checkout([
$class: 'GitSCM',
branches: [[name: "origin/master"]],
userRemoteConfigs: [[url: 'https://github.com/mmmmmmmmm.git']]
])
} } }
stage('Cleanup') {
steps {
echo 'Starting the Pipeline'
sh 'docker rm -f $(docker ps --all --quiet) || true'
sh 'docker rmi -f $(docker images --quiet) || true'
}
}
stage('build') {
steps {
sh 'docker build -t test --no-cache .'
}
}
stage('Run') {
steps {
sh 'docker run -d --name test -p 80:80 test '
}
}
stage('Login') {
steps {
sh 'docker container exec -it test /bin/bash '
sh 'ls -ltr'
}}}}
Error: docker container exec -it test /bin/bash
the input device is not a TTY
And how to run curl command for the jenkins server port 80?
Thank you
You can directly pass the curl command with exec like below.
sh "docker exec containerName /bin/bash -c 'curl http://jenkins-server:80'"

How to push docker image to Nexus repository through Jenkinsfile?

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

Resources