Where is the documentation for "Building container" using Descriptive pipeline

I found documentation for building containers here under " Advanced Usage with Scripted Pipeline".

But where can I find documentation for building containers for “Descriptive pipeline”?

Thank you.

For building containers using a Declarative Pipeline in Jenkins, you can refer to the official Jenkins documentation on Declarative Pipeline syntax.

Here is an untested example of how you could build and use Docker containers in a Declarative Pipeline:

pipeline {
    agent {
        any
    }
    stages {
        stage('Build') {
            steps {
                script {
                    // Build your application here
                    sh 'mvn clean install'
                }
            }
        }
        stage('Test') {
            steps {
                script {
                    // Run your tests here
                    sh 'mvn test'
                }
            }
        }
        stage('Build Docker Image') {
            steps {
                script {
                    // Build Docker image
                    sh 'docker build -t my-app:latest .'
                }
            }
        }
        stage('Push Docker Image') {
            steps {
                script {
                    // Push Docker image to registry
                    withCredentials([usernamePassword(credentialsId: 'dockerhub-credentials', usernameVariable: 'DOCKERHUB_USER', passwordVariable: 'DOCKERHUB_PASS')]) {
                        sh 'docker login -u $DOCKERHUB_USER -p $DOCKERHUB_PASS'
                        sh 'docker push my-app:latest'
                    }
                }
            }
        }
    }
}
1 Like

Thank you @poddingue .

I had to use the sh ‘docker …’ command in a separate stage instead of first stage. OR else it will throw “docker not found” error.

I also got the same answer in the Jenkins Gitter chat.

Thank you.

Thanks for your feedback. :+1: