<p>I have created an image and uploaded to docker:</p>
<pre class="lang-dockerfile prettyprint-override"><code>FROM jenkins/inbound-agent:jdk11 as agent

LABEL maintainer=&quot;JoSSte &lt;re@dacted&gt;&quot;

USER root
ENV TZ=Europe/Copenhagen
RUN ln -snf /usr/share/zoneinfo/&quot;${TZ}&quot; /etc/localtime &amp;&amp; echo &quot;${TZ}&quot; &gt; /etc/timezone \
&amp;&amp;  dpkg-reconfigure -f noninteractive tzdata 

# update apt cache
RUN apt-get update \
&amp;&amp;  apt-get upgrade -qy \
&amp;&amp;  apt-get install -y curl lsb-release ca-certificates apt-transport-https software-properties-common gnupg2

# add php8 repo
RUN curl -sSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg \
&amp;&amp;  sh -c 'echo &quot;deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main&quot; &gt; /etc/apt/sources.list.d/php.list' \
&amp;&amp;  apt-get update

# install php8 &amp; composer
RUN apt-get install -qy rsync zip php8.2-curl php8.2-gd apache2 php8.2 unzip php8.2-mysql php8.2-zip php8.2-mbstring php-xdebug php-pear* \
&amp;&amp;  curl -sSLo composer-setup.php https://getcomposer.org/installer \
&amp;&amp;  php composer-setup.php --install-dir=/usr/local/bin --filename=composer \
&amp;&amp;  composer self-update  

# Cleanup old packagess
RUN apt-get -qy autoremove

USER jenkins

ENTRYPOINT [&quot;jenkins-slave&quot;]
</code></pre>
<p>Which I have used fine in a Jenkins build environment with docker agents.</p>
<p>Now I am changing my setup to work in kubernetes, so I made sure that things work as expected:</p>
<pre class="lang-groovy prettyprint-override"><code>podTemplate(containers: [
    containerTemplate(name: 'php8builder', image: 'jstevnsvig/jenkins-build-slave-php:v8.2', command: 'sleep', args: '99d')
  ]) {

    node(POD_LABEL) {
        container('php8builder') {
            stage('list apt packages') {
                sh '/bin/sh -c &quot;dpkg-query -l | cat&quot;'
            }
            stage('dump php version') {
                sh '/bin/sh -c &quot;php --version&quot;'
            }
            stage('dump /usr/local/bin files') {
                sh 'ls /usr/local/bin'
            }
            stage('dump composer version') {
                sh '/bin/sh -c &quot;composer --version&quot;'
            }
        }
    }
}
</code></pre>
<p>This works beautifully:</p>
<blockquote>
<pre><code> + /bin/sh -c php --version
 PHP 8.2.1 (cli) (built: Jan 13 2023 10:38:46) (NTS)
 Copyright (c) The PHP Group
 Zend Engine v4.2.1, Copyright (c) Zend Technologies
     with Zend OPcache v8.2.1, Copyright (c), by Zend Technologies
     with Xdebug v3.2.0, Copyright (c) 2002-2022, by Derick Rethans
</code></pre>
</blockquote>
<p>...</p>
<blockquote>
<pre><code>  + ls /usr/local/bin
  composer
  jenkins-agent
  jenkins-slave
</code></pre>
</blockquote>
<p>...</p>
<blockquote>
<pre><code> + /bin/sh -c composer --version
 Composer version 2.5.1 2022-12-22 15:33:54
</code></pre>
</blockquote>
<p>BUT</p>
<p>When I then tried to run it in a pipeline (this has worked before with all the agents defined in the Jenkins configuration - but I want to have less configuration in case of things going wrong), the same image , on the same Jenkins server in the same kubernetes cluster, can't find php or composer... :</p>
<pre class="lang-groovy prettyprint-override"><code>pipeline {
    agent { 
        kubernetes {
            yamlFile 'KubernetesPod.yaml'
            retries 2
          }
        }
    options {
        disableConcurrentBuilds()
    }
    stages {
        stage ('Staging'){
            steps {
                echo &quot;looking for composer&quot;
                sh 'ls /usr/local/bin'
            }
        }
        stage('run php') {
            steps {
                container('phpbuilder') {
                    sh 'php --version'
                    sh 'php src/test.php'
                }
            }
        }
    }
}
</code></pre>
<h2>KubernetesPod.yaml</h2>
<pre class="lang-yaml prettyprint-override"><code># https://github.com/jenkinsci/kubernetes-plugin/blob/master/examples/declarative_from_yaml_file/KubernetesPod.yaml
metadata:
  labels:
    some-label: some-label-value
spec:
  containers:

  - name: phpbuilder
    image: jstevnsvig/jenkins-build-slave-php:v8.2
    command:
    - sleep
    args:
    - 99d
    #env:
    #- name: CONTAINER_ENV_VAR
    #  value: maven
</code></pre>
<p>Then composer isn't found:</p>
<blockquote>
<pre><code>  looking for composer
  + ls /usr/local/bin
  jenkins-agent
  jenkins-slave
</code></pre>
</blockquote>
<p>...</p>
<blockquote>
<pre><code>  + php --version
  PHP 8.2.1 (cli) (built: Jan 13 2023 10:38:46) (NTS)
  Copyright (c) The PHP Group
  Zend Engine v4.2.1, Copyright (c) Zend Technologies
      with Zend OPcache v8.2.1, Copyright (c), by Zend Technologies
      with Xdebug v3.2.0, Copyright (c) 2002-2022, by Derick Rethans
</code></pre>
</blockquote>
<p>As I stated bove, I have used this image in my previous build setup with the jenkins cloud config built in</p>
<pre class="lang-groovy prettyprint-override"><code>
pipeline {
    agent { label 'php8' }
...
</code></pre>
<p>and I have confirmed that it still works on this cluster - so WHY doesn't it work when i provide kubernetes yaml?</p>

JoSSte
<p>Your pipeline configuration is missing one thing which is <code>defaultContainer</code> directive.</p>
<p>Please note that by default <code>Jenkins</code> will run 2 containers in one pod. One will be <code>jnlp</code> even if you don't define it and second you container defined in your <code>KubernetesPod.yaml</code>.</p>
<p>So to be able to run things on your custom container your pipeline should looks like this:</p>
<pre class="lang-groovy prettyprint-override"><code>pipeline {
    agent { 
        kubernetes {
            defaultContainer '&lt;&lt;you container name from yaml file&gt;&gt;'
            yamlFile 'KubernetesPod.yaml'
            retries 2
          }
        }
    options {
        disableConcurrentBuilds()
    }
    stages {
        stage ('Staging'){
            steps {
                echo &quot;looking for composer&quot;
                sh 'ls /usr/local/bin'
            }
        }
        stage('run php') {
            steps {
                container('phpbuilder') {
                    sh 'php --version'
                    sh 'php src/test.php'
                }
            }
        }
    }
}

</code></pre>
<p>Please see more examples in <a href="https://plugins.jenkins.io/kubernetes/#plugin-content-declarative-pipeline" rel="nofollow noreferrer">plugin doc</a>.</p>
<p>I forgot to mention that you can also run single container in pod by replacing default <code>jnlp</code> since you where just adding stuff to <code>jenkins-agent</code> image. You can use following manifest to accomplish that</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  labels:
    role: agent
  name: jenkins-slave
  namespace: jenkins
spec:
  serviceAccountName: jenkins-agent
  containers:
  - name: jnlp
    image: &lt;&lt;your custom container&gt;&gt;
    args: ['\$(JENKINS_SECRET)', '\$(JENKINS_NAME)']

</code></pre>
