With jenkins you can use docker in two contexts.
1 - use docker container from a private repo as the jenkins node to build your code
2 - use jenkins node to build your docker container and probably push it to your private repo
This was confusing when i started and so i want to declutter this :
Assuming
- you have a jenkins single agent cluster with docker daemon running on that agent.
- a private docker registry with credentials to pull and push
use docker container from a private repo as the jenkins node to build your code
Install plugin pipeline model
(https://jenkins.io/doc/book/pipeline/)
(https://plugins.jenkins.io/pipeline-model-definition/)to specify that you want Jenkins to pull the node:6.0 image from a private registry, and run the build inside it. This can pretty much be any image you like, your build steps and stages will run inside it. the registry credentials are stored in the jenkins credentials store for this purpose.
pipeline {
/* start with outmost.*/
agent any
/* This method fetches a container defination from the registered repo and build and runs the steps inside it.*/
stages {
stage('Test') {
agent {
docker {
image 'node:7-alpine'
registryUrl 'https://my-docker-virtual.artifactory.rogers.com/'
registryCredentialsId 'ARTIFACTORY_USER'
}
}
steps {
sh 'node --version'
}
}
}
}
- IMPORTANT: Inside this container, i have no context of the docker which is running on bare metal jenkins. hence i have no context of the registry . I can not write a push command in the steps as i am inside the container.
- I can also use a docker file as a agent definition instead of fetching a docker definition from registry
pipeline {
agent { dockerfile true }
stages {
stage('Test') {
steps {
sh 'node --version'
sh 'svn --version'
}
}
}
}
- Here it is looking into the root directory for the Dockerfile which it will use as a build agent.
use jenkins node to build your docker container and probably push it to your private repo
node {
checkout scm
docker.withRegistry('https://registry.hub.docker.com','dockerhubcredentails'){
def myImage= docker.build("digitaldockerimages/busybox:0.0.1")
/*push container to the registry*/
myImage.push()
}
}
Here i am using a private registry and fetching the credentials for the registry from jenkins secret storage.
Ref:
https://www.youtube.com/watch?v=z32yzy4TrKM
https://jenkins.io/doc/book/pipeline/docker/#sidecar
https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Controlling-your-build-environment
https://issues.jenkins-ci.org/browse/JENKINS-39684
Top comments (0)