Answers: <p>I actually figured out the solution. In the ImageA pipeline, I added the 'main.py' to the artifacts and then in ImageA, I cloned 'main.py' and executed it.</p>
  <p>ImageA:</p>
  <pre><code>always {
          archiveArtifacts artifacts: 'main.py', onlyIfSuccessful: true
      }
  </code></pre>
  <p>Current pipeline:</p>
  <pre><code>sh &quot;curl {url to imageA artifact}/main.py -o main.py&quot;
  docker.image(ImageA).inside {
  
      sh &quot;python main.py &quot;
  
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2021-11-16T15:53:26.150Z
Answers: 2021-11-16T15:53:26.150Z
Answers: Taziamoma Abraham
Body: <p>I have an Image A, which has a python file &quot;main.py&quot;. I want to know how I can use Jenkins to run that file from a different pipeline. I want to use Jenkins to go inside that image and then run that file which is inside Image A.</p>
  <p>I tried using .withRun but I don't think I'm doing this right.</p>
  <pre><code>docker.image(ImageA).withRun() {
      sh &quot;main.py ${parameters to pass}&quot;
  }
  </code></pre>
  <p>When I run it like this, it executes the local main.py that I have and executes that inside the container which isn't what I want. I want to execute the main.py that is already inside the container and pass in parameters.</p>
  <p>Any ideas?</p>
  
Comments: CC BY-SA 4.0
Comments: 2021-11-14T22:57:26.773Z
Comments: You can use the `inside` command (see [more info](https://www.jenkins.io/doc/book/pipeline/docker/#advanced-usage-with-scripted-pipeline)) which will execute your shell command inside the docker image using the local image files as you want. Something like: `docker.image(ImageA).inside() {...}`
Comments: Noam Helmer
Comments: CC BY-SA 4.0
Comments: 2021-11-15T18:15:26.060Z
Comments: @NoamHelmer when I use the .inside() and I use a sh command and pass in my main.py, it's looking for main.py from within the current build and not within Image B. I heard a better way to do this would be to somehow use build artifacts to access files from another workspace (imageB).
Comments: Taziamoma Abraham
Tags: <docker><jenkins><jenkins-pipeline><groovy>
Title: Execute python file existing inside image with Jenkins
UserDisplayName: Taziamoma Abraham
ThreadText: # Execute python file existing inside image with Jenkins
  
  (Asked by: username_0 on 2021-11-04)
  
  I have an Image A, which has a python file "main.py". I want to know how I can use Jenkins to run that file from a different pipeline. I want to use Jenkins to go inside that image and then run that file which is inside Image A.
  
  I tried using .withRun but I don't think I'm doing this right.
  
  ```
  docker.image(ImageA).withRun() {
      sh "main.py ${parameters to pass}"
  }
  
  ```
  
  When I run it like this, it executes the local main.py that I have and executes that inside the container which isn't what I want. I want to execute the main.py that is already inside the container and pass in parameters.
  
  Any ideas?
  
  **Tags:** docker, jenkins, jenkins-pipeline, groovy
  
  > **Comments:**
  > **username_1**: You can use the `inside` command (see [more info](https://www.jenkins.io/doc/book/pipeline/docker/#advanced-usage-with-scripted-pipeline)) which will execute your shell command inside the docker image using the local image files as you want. Something like: `docker.image(ImageA).inside() {...}`  
  > **username_0**: @username_1 when I use the .inside() and I use a sh command and pass in my main.py, it's looking for main.py from within the current build and not within Image B. I heard a better way to do this would be to somehow use build artifacts to access files from another workspace (imageB).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-11-16. Score: 0 | Accepted answer
  
  I actually figured out the solution. In the ImageA pipeline, I added the 'main.py' to the artifacts and then in ImageA, I cloned 'main.py' and executed it.
  
  ImageA:
  
  ```
  always {
          archiveArtifacts artifacts: 'main.py', onlyIfSuccessful: true
      }
  
  ```
  
  Current pipeline:
  
  ```
  sh "curl {url to imageA artifact}/main.py -o main.py"
  docker.image(ImageA).inside {
  
      sh "python main.py "
  
  }
  
  ```
  
  
  ---
  
  