SoFunction
Updated on 2025-03-04

Analysis of the difference between dockerExecute function

1. Introduction to dockerExecute

dockerExecuteUsually a custom function or a specific Jenkins library (e.g.piper-lib) provides for executing specific commands in a Docker container. Such functions can help manage the life cycle of a Docker container during the build or test phase, ensuring that steps run within the container. Its purpose is to start a Docker container and execute the provided commands in that container.

1. Use scenarios:

  • Perform tasks that require isolation of the environment: When you need to run specific commands in the specified Docker container, such as running test scripts, building projects, etc.
  • Ensure environmental consistency: Ensure that tasks are run in an isolated, standardized Docker container, eliminating the problems caused by environmental differences.
  • Dynamic settings: Allows more fine-grained control of the container configuration, such as dynamically setting environment variables, mount volumes, etc.

2. Introduction to the use of dockerexecute method

Because my testing background happens to be that there is a ready-made image dedicated to testing in the Docker image repository within the company; so I directly used this method in this pipline script to show you an example and explain it by the way:

stage('test') {
    node {
        dockerExecute(script: this,
        dockerImage: "your-docker-image",
        dockerName: 'your-docker-name'{
// Execute the following command inside the Docker container                    sh '''
                        //For example, install dependencies, set environment variables, execute key scripts, etc.                        python 
                        '''
}

3. DockerExecute accept parameters:

  • script: this:
    • BundlethisPass toscriptparameter. It is usually used indockerExecuteThe function internally refers to the current script or context.
  • dockerImage: "your-docker-image",
    • Specifies the name of the Docker image. This image often contains the environment you need (such as specific versions of Python and related tools and dependencies).
  • dockerName: 'your-docker-name'
    • Sets the name of the Docker container. This helps identify running containers in Jenkins logs and elsewhere.

Function summary

  • Start a Docker container based on the specified Docker image.
  • parameterscript: thisReferences to the current script context.
  • parameterdockerImageSpecifies the Docker image to use.
  • parameterdockerNameName the started container.

2. Introduction

is a method provided by Jenkins' Docker plugin for building Docker images. It will read the current directoryDockerfileAnd build a new Docker image that helps encapsulate your application and all dependencies into a standard image.

1. Use scenarios:

  • Build a new Docker image: Used when you need to build a new Docker image based on Dockerfile.
  • Version control and deployment: Package the application into a Docker image, submitted to Docker Registry as part of the CI/CD process or for deployment.

2. Use in combination

  • A typical workflow may involve first useBuild the image and then run certain tasks, such as test suites, with that image.

3. Define the scene

  • Suppose you have a Python project that you want to build and run tests in a Docker container.
  • You need to create a Jenkinsfile and a dockerfile in the project root directory

(1) Dockerfile example:

# Use existing basic imagesFROM your-docker-image
# Set up the working directoryWORKDIR /web-test
# Install system dependencies include chromedriver and google-chromeRUN apt-get update && apt-get install -y \
    xvfb \
    x11-utils \
    libgl1-mesa-glx \
    wget \
    unzip \
    curl \
    gnupg \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*
# Install chromedriverRUN wget -O /tmp/ /113.0.5672.63/chromedriver_linux64.zip \
    && unzip /tmp/ -d /usr/local/bin/ \
    && rm /tmp/
# Download and install google-chromeRUN wget -O /tmp/ /linux/direct/google-chrome-stable_current_amd64.deb \
    && apt-get update && apt-get install -y --no-install-recommends /tmp/ \
    && rm /tmp/
# Copy to the working directoryCOPY  .
# Install Python dependenciesRUN pip install --no-cache-dir -r 
# Copy the project file to the working directoryCOPY . .
# Set environment variablesENV DISPLAY=:99
# Start Xvfb and run the applicationCMD Xvfb :99 -screen 0 1920x1080x24 & python PANA/

(2) Jenkinsfile example:

@Library(["piper-lib", "piper-lib-os"]) _
pipeline {
    agent any
    stages {
        stage('checkout') {
            steps {
                checkout scm
            }
        }
        stage('build-image') {
            steps {
                script {
                    // Use the Dockerfile in the current directory to build a new Docker image                    ('my-repo/my-python-app:latest', '-f Dockerfile .')
                }
            }
        }
        stage('test') {
            steps {
                script {
                    dockerExecute(script: this,
                    dockerImage: "my-repo/my-python-app:latest",
                    dockerName: 'python') {
                        sh '''
                            set -e
                            # Set DISPLAY environment variables                            export DISPLAY=:99
                            # Start Xvfb and run in the background                            Xvfb :99 -screen 0 1920x1080x24 &
                            # Wait for Xvfb to fully start                            sleep 5
                            # Print Python version information                            python --version
                            # Upgrade pip                            python -m pip install --upgrade pip
                            # Switch to the working directory                            cd $WORKSPACE
                            # Install Python dependencies                            python -m pip install -r 
                            # Run Python scripts                            python PANA/
                            # Optional: Make sure to stop Xvfb after completion                            killall Xvfb
                        '''
                    }
                }
            }
        }
    }
    post {
        always {
            echo 'Cleaning up...'
            // Place any cleaning steps        }
        success {
            echo 'Pipeline succeeded'
        }
        failure {
            echo 'Pipeline failed'
        }
    }
}

3. Key differences

1. Build and run

  • : Used to build Docker images, based on Dockerfile within the project.
  • dockerExecute: Used to run commands based on built Docker images.

2. Specific use

  • : Mainly used for the construction steps in the CI process, ensuring that application code and dependencies are packaged in one image.
  • dockerExecute: Used to execute tasks in a specific Docker image, suitable for testing, executing scripts and other steps that require isolation of the environment.

This is the end of this article about the difference between dockerExecute functions. For more related dockerExecute functions and content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!