Answers: <p>You can use withRun.</p>
  <pre><code>docker.image(&quot;mysql:latest&quot;).withRun('-e MYSQL_ROOT_PASSWORD=root)
  </code></pre>
  <p>Documentation for this is <a href="https://www.jenkins.io/doc/book/pipeline/docker" rel="nofollow noreferrer">here</a>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2021-08-19T11:52:15.317Z
Answers: As mush as I understand, we use "withRun" to send OPTIONS not COMMAND, and we can use it in scripted not declarative pipelines, do we?
Answers: MRashad
Answers: CC BY-SA 4.0
Answers: 2021-08-19T08:33:45.200Z
Answers: 2021-08-19T08:33:45.200Z
Answers: storm
Answers: <p>First of all, I'm not sure if you want to keep the container in the block because the container will be stopped as soon as its body exits if you use <code>withRun</code>.</p>
  <p>So you can run the COMMAND that specified after the IMAGE can be run within either <code>Image.run([arg, command])</code> or <code>withRun</code> according to Jenkins pipeline-syntax page.</p>
  <p><code>$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]</code></p>
  <blockquote>
  <p>Image.withRun[(args[, command])] {…}<br />
  Like run but stops the container as soon as its body exits, so you do not need a try-finally block.</p>
  </blockquote>
  <blockquote>
  <p>Image.run([args, command])<br />
  Uses docker run to run the image, and returns a Container which you could stop later. Additional args may be added, such as '-p 8080:8080 --memory-swap=-1'. Optional command is equivalent to Docker command specified after the image.</p>
  </blockquote>
  <p>If you see the <code>Image.run()</code> by visiting Jenkins <a href="https://opensource.triology.de/jenkins/pipeline-syntax/globals" rel="nofollow noreferrer">pipeline-syntax</a>, it's described that you can run the image with Docker COMMAND.</p>
  <p>For instance here's an example as below:</p>
  <pre><code>stages {
          // docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:latest  --default-authentication-plugin=mysql_native_password
          stage(&quot;Run MySQL&quot;) {
              steps {
                  script {
                      docker.image('mysql:latest').withRun('-p 3306:3306 -e &quot;MYSQL_ROOT_PASSWORD=root&quot;', '--default-authentication-plugin=mysql_native_password') {c -&gt;
                          // Run command
                      }
                  }
              }
          }
  </code></pre>
  <p>Edit: I tried running the MySQL container using <code>withRun()</code> with two parameters, and succeed to run the command. Try the code snippet above, and see the pipeline as shown in the attached file.</p>
  <p><a href="https://i.sstatic.net/Vn9US.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Vn9US.png" alt="enter image description here" /></a></p>
  
Answers: CC BY-SA 4.0
Answers: 2021-09-21T00:44:19.563Z
Answers: `Image.run()` was failed in the Declarative syntax with error `Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod `. So I assume that `Image.withRun()` should be able to run the command with args, and command as parameter.
Answers: Chachatonel Hashimotto
Answers: CC BY-SA 4.0
Answers: 2021-11-20T10:17:46.427Z
Answers: I didn't know that we can send more than one parameter to it, thank you
  But it is still scripted, is there any declarative approach?
Answers: MRashad
Answers: CC BY-SA 4.0
Answers: 2021-09-19T08:35:03.903Z
Answers: 2021-09-21T00:41:41.797Z
Answers: 2021-09-21T00:41:41.797Z
Answers: Chachatonel Hashimotto
Body: <p>In scripted Jenkinsfile if I need to send a command to Docker image, I can do like this:</p>
  <pre><code>docker.image('mysql --default-authentication-plugin=mysql_native_password')
  </code></pre>
  <p>How can I do the same in declarative pipeline? Something like the following:</p>
  <pre><code>agent {
      docker {
          image 'mysql'
          reuseNode true
          args &quot;-e MYSQL_ROOT_PASSWORD=root&quot;
          command &quot;--default-authentication-plugin=mysql_native_password&quot;
      }
  }
  </code></pre>
  
Tags: <docker><jenkins><jenkins-pipeline><jenkinsfile>
Title: Send command to docker image while start in Jenkins file
UserDisplayName: MRashad
ThreadText: # Send command to docker image while start in Jenkins file
  
  (Asked by: username_0 on 2021-08-15)
  
  In scripted Jenkinsfile if I need to send a command to Docker image, I can do like this:
  
  ```
  docker.image('mysql --default-authentication-plugin=mysql_native_password')
  
  ```
  
  How can I do the same in declarative pipeline? Something like the following:
  
  ```
  agent {
      docker {
          image 'mysql'
          reuseNode true
          args "-e MYSQL_ROOT_PASSWORD=root"
          command "--default-authentication-plugin=mysql_native_password"
      }
  }
  
  ```
  
  **Tags:** docker, jenkins, jenkins-pipeline, jenkinsfile
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2021-08-19. Score: 0
  
  You can use withRun.
  
  ```
  docker.image("mysql:latest").withRun('-e MYSQL_ROOT_PASSWORD=root)
  
  ```
  
  Documentation for this is [here](https://www.jenkins.io/doc/book/pipeline/docker).
  
  > **Comments:**
  > **username_0**: As mush as I understand, we use "withRun" to send OPTIONS not COMMAND, and we can use it in scripted not declarative pipelines, do we?  
  
  ---
  
  ### Answer by username_2 on 2021-09-19. Score: 0
  
  First of all, I'm not sure if you want to keep the container in the block because the container will be stopped as soon as its body exits if you use `withRun`.
  
  So you can run the COMMAND that specified after the IMAGE can be run within either `Image.run([arg, command])` or `withRun` according to Jenkins pipeline-syntax page.
  
  `$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]`
  
  > Image.withRun[(args[, command])] {…}  
  > Like run but stops the container as soon as its body exits, so you do not need a try-finally block.
  
  > Image.run([args, command])  
  > Uses docker run to run the image, and returns a Container which you could stop later. Additional args may be added, such as '-p 8080:8080 --memory-swap=-1'. Optional command is equivalent to Docker command specified after the image.
  
  If you see the `Image.run()` by visiting Jenkins [pipeline-syntax](https://opensource.triology.de/jenkins/pipeline-syntax/globals), it's described that you can run the image with Docker COMMAND.
  
  For instance here's an example as below:
  
  ```
  stages {
          // docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql:latest  --default-authentication-plugin=mysql_native_password
          stage("Run MySQL") {
              steps {
                  script {
                      docker.image('mysql:latest').withRun('-p 3306:3306 -e "MYSQL_ROOT_PASSWORD=root"', '--default-authentication-plugin=mysql_native_password') {c ->
                          // Run command
                      }
                  }
              }
          }
  
  ```
  
  Edit: I tried running the MySQL container using `withRun()` with two parameters, and succeed to run the command. Try the code snippet above, and see the pipeline as shown in the attached file.
  
  [![enter image description here](https://i.sstatic.net/Vn9US.png)](https://i.sstatic.net/Vn9US.png)
  
  > **Comments:**
  > **username_2**: `Image.run()` was failed in the Declarative syntax with error `Scripts not permitted to use method groovy.lang.GroovyObject invokeMethod `. So I assume that `Image.withRun()` should be able to run the command with args, and command as parameter.  
  > **username_0**: I didn't know that we can send more than one parameter to it, thank you But it is still scripted, is there any declarative approach?  
  
  ---
  
  