Access command line of a Docker Slave running Matlab via Jenkins pipeline

I am using the Matlab Docker Image to run Matlab in a Container and do calculations within it.

When you commonly start the container with docker run() it spins up a command line with running Matlab which is waiting for user Input.

My goal is to use the Docker Image as a build slave in Jenkins, so it spinns up the Container and kills it automatically after usage. The desired Matlab commands should be implemented in the Jenkins Job. So far it is all set up and Jenkins is starting Matlab in a Docker slave environment and then exits:

[Pipeline] sh
+ chmod +x [Pipeline] sh
+ ./ MATLAB is selecting SOFTWARE OPENGL rendering.
                            < M A T L A B (R) >
                  Copyright 1984-2019 The MathWorks, Inc.
                  R2019b ( 64-bit (glnxa64)
                              August 21, 2019

  To get started, type doc. For product information, visit

[Pipeline] }  
[Pipeline] //  stage 
[Pipeline] }  
[Pipeline] //  node 
[Pipeline]  End of Pipeline 
[withMaven]  pipelineGraphPublisher - triggerDownstreamPipelines 
[withMaven]  downstreamPipelineTriggerRunListener - completed in 58 ms Finished: SUCCESS

The is the script provided by Mathworks to start matlab in the container and looks like this.

# Copyright 2019 The MathWorks, Inc.

build_cmd () { # Takes the cmd input string and outputs the same
    # string correctly quoted to be evaluated again.
    # Always returns a 0
    # usage: build_cmd

    # Use version of echo here that will preserve
    # backslashes within $cmd. - g490189

            $ECHO "$1" | awk '
        BEGIN { squote = sprintf ("%c", 39)   # set single quote
                dquote = sprintf ("%c", 34)   # set double quote
          NF != 0 { newarg=dquote             # initialize output string to
                                              # double quote
          lookquote=dquote                    # look for double quote
          oldarg = $0
          while ((i = index (oldarg, lookquote))) {
             newarg = newarg substr (oldarg, 1, i - 1) lookquote
             oldarg = substr (oldarg, i, length (oldarg) - i + 1)
             if (lookquote == dquote)
                lookquote = squote
                lookquote = dquote
             newarg = newarg lookquote
          printf " %s", newarg oldarg lookquote }
            return 0


while [ $# -gt 0 ]; do
    case "$1" in
            QUOTED_CMD=`build_cmd "$2"`
            ARGLIST="${ARGLIST} $1 `$ECHO ${QUOTED_CMD}`"
            ARGLIST="${ARGLIST} $1"

eval exec "matlab ${ARGLIST}"


In my Jenkins Job I defined a stage that is just executing that script and starts Matlab which is working fine:

    sh 'chmod +x'
    sh './'
    sh '1+1'

The only thing is that I am not able to get any commands from the pipeline in Jenkins to be executed within the containers command line. I tried to just do a simple ‘1+1’ and put it under the ‘./’ without success.

/home/jenkins/workspace/DR/[email protected]/durable-f5af635b/ 1: /home/jenkins/workspace/DR/[email protected]/durable-f5af635b/ 1+1: not found

So my question is if anyone can help me resolve how or where I have to put my commands to be executed here…

My Thanks in advance!

Source: Docker Questions