Jenkins Docker pipeline plugin with docker run

1k views Asked by At

I have a Jenkins pipeline with the Docker Pipeline plugin installed.

I would like to run a docker container with the plugin using the following command:

docker.image("$uri/$name:$tag").run("-it --name myWebApp -v /myData:/mywebapp/data") 

However, however, Jenkins'log console shows me nothing. I use -it to show the log and the process until it is finished, however it seems not to work.

Looking at the official documentation, it seems to use -d, in detached mode. I have seen that type withRun and I have tried the following command:

docker.image("$uri/$name:$tag").run("-it --name myWebApp -v /myData:/mywebapp/data") { c-> 
   sh "docker logs ${c.id}"
}

However it stops after a few seconds showing this:

docker logs 897sdfhdv87sdXX

How do I start a docker container normally? By showing the logs and synchronously? It ends in exit 0 type when the container ends successfully

1

There are 1 answers

0
xalves On

Hi I know this probably is no longer needed, but I believe this is achievable by doing something like:

docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw"') { c ->
    sh "docker logs ${c.id}"
} 

This example was adapted from https://www.jenkins.io/doc/book/pipeline/docker/