Jenkins Docker Configurations

Hello,
I have to combine jenkins and docker. I am new to both of them. I dont want to use ssh but I have to make auto deployment.

I have a maven spring boot project in github repo. I have to build it in jenkins at ServerA. Then I have private docker hub repo, I have to push this image which is built by jenkins to this private docker hub. And I have production server, ServerB. ServerB has docker-ce. ServerB has to delete old containers produced from the old version of this image. And It has to run new container. It must do that by jenkins.

The needs and what I did as list above:

DONE

– Jenkins is running on ServerA. It has maven and other needed plugins. (Also It connects to AWS EC2 perfectly it looks like configurations dont have problem.)
– docker ce and needed repos are downloaded to ServerB
– builds the docker image for my github repo by jenkins
– this image is pushed to docker daemon as docker hub registry
– configured docker remote api has tls certificates. (it is reachable from another docker terminal also jenkins docker plugin is reaching it when I test connection)

QUESTION :

I am using Jenkins 2.164.2. I am using Docker agent (Version = 18.09.5, API Version = 1.39.) The problem I dont know how I can manage docker daemon command line by jenkins without ssh. May I manage docker by docker remote api ? and how? I searched many files but I am so new to both of these technologies cause of that I confused.

Can you suggest some ways to handle with this ?

Thank you

You can use script for this. Shell. write all commands in the script order.
Then in Jenkins file

node{
stage (‘execute shell’){
sh ‘/absolute path to the script/script name.sh’
}
}

For sending docker image to your docker repo

stage(‘Push image’) {
docker.withRegistry(‘https://registry.hub.docker.com’, ‘docker-hub-credentials’) {
app.push("${env.BUILD_NUMBER}")
app.push(“latest”)
}
}
}

Also refer to this link.

for sending docker image

Thank you for your reply

Actually, I am doing sth like below now;

node {
def mvnHome
stage(‘Git Connection’) {
// checkout SCM commands
mvnHome = // My Maven Tool
}
stage(‘Maven Build’) {
// Run the maven build
if (isUnix()) {
sh “’${mvnHome}/bin/mvn’ // my options”
} else {
bat( // for maven can not build )
}
}
stage(‘Docker Build’){
sh ‘docker build --build-arg argss -f path -t nameOfRepoWithoutTag .’
sh ‘docker tag nameOfRepoWithoutTag nameOfrepo $BUILD_NUMBER’
sh ‘docker images’
withDockerServer(uri:‘tcp://uri’, credentialsId:‘credentialsId’){
withCredentials([usernamePassword(credentialsId: ‘credentialsId2’, usernameVariable: ‘USR’, passwordVariable:‘PSW’)]){
sh ‘docker login -u $USR -p $PSW’
sh ‘docker pull nameOfrepo’
sh ‘docker container ls’
}
}
}
}

In this case; it connects to tls docker and reaches the images. I checked the article before. I couldn’t manage with run.
When I wrote “docker run -p 8080:8080 nameOfrepo” it runs but from inside the container. I am seeing all container details. I am doing sth in wrong way right?

docker run -d --name containername -p 8080:8080 imagename:tag

From my understanding. I wrote this command. -d helps you to run container in BG.

Yes I forgot it. Thank you for your help :slight_smile:

Products that dominate the market for years tend to be stable and very feature rich. Jenkins is no exception. However, with age come some downsides as well. In the case of Jenkins, automation setup is one of the things that has a lot to be desired Dollar General Survey