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"

2 Answers 2


You can use withRun.

docker.image("mysql:latest").withRun('-e MYSQL_ROOT_PASSWORD=root)

Documentation for this is here.

  • 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?
    – MRashad
    Commented Aug 19, 2021 at 11:52

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, 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

  • 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. Commented Sep 21, 2021 at 0:44
  • 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?
    – MRashad
    Commented Nov 20, 2021 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.