'Why does Jenkins returns and exit code 127 when I run sh 'python --version'?

I get the same error with sh 'python --version' and sh 'python3 --version' And I don't know why Jenkins doesn't recognize the command when the Dockerfile image has python installed, my Jenkins version: 2.319.3 I added the label to the node, I tried without label in the pipeline... Echo and ls command works and I don't know if I understand it very much hahahaha HELP!

My pipeline:

pipeline {
    agent {
        label 'python'
    }
    stages {
        stage ('Test') {
            steps {
                sh 'echo Aloha!!!!!'
            }
        }
        stage ('Is there any python?') {
            steps {
                sh 'python3 --version'
            }
        }
    }
}

My Dockerfile

FROM ubuntu:21.10

ARG DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -qy git wget software-properties-common openssh-server && \
    sed -i 's|session    required     pam_loginuid.so|session    optional     pam_loginuid.so|g' /etc/pam.d/sshd && \
    mkdir -p /var/run/sshd && \
    apt-get install -qy openjdk-8-jdk && \
    apt-get install -qy maven && \
    apt-get install -qy apt-utils && \
    useradd -ms /bin/bash jenkins && \
    echo "jenkins:jenkins" | chpasswd && \
    mkdir /home/jenkins/.m2

RUN mkdir /home/jenkins/.ssh/ && \
    touch /home/jenkins/.ssh/authorized_keys

RUN chown -R jenkins:jenkins /home/jenkins/.m2/ && \
    chown -R jenkins:jenkins /home/jenkins/.ssh/

RUN apt-get install python3

RUN apt-get update && apt-get install python3-pip python3.9-venv -y

RUN cd "$(dirname $(which python3))" && \
    ln -s idle3 idle && \
    ln -s pydoc3 pydoc && \
    ln -s python3 python && \
    ln -s python3-config python-config

COPY .pypirc /home/jenkins/

EXPOSE 22

CMD ["/usr/sbin/sshd", "-D"]

Jenkins

[Pipeline] { (Is there any python?)
[Pipeline] sh
+ python3 --version
/var/jenkins_home/workspace/Jobs/Pipeline Github Example@tmp/durable-a28fa974/script.sh: 1: python3: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE


Solution 1:[1]

Jenkins agent label represents one or one group worker node(s) on which Jenkins master will schedule job/pipeline run.

Therefore your pipeline is assigned to run on worker node 'python', the error told you it can't find python executable on the worker node.

You need to register machine/vm to Jenkins master to make it serve as Jenkins worker node, when register, you can specify labels for the worker node, then you can use label to reference worker node in your job/pipeline.

One option is to prepare python on worker node 'python'

Another option is to use your Dockerfile as agent for whole pipeline or single stage. More detail: https://www.jenkins.io/doc/book/pipeline/syntax/#agent

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 yong