[JIRA] (JENKINS-49076) Cannot set custom PATH inside docker container

628 views
Skip to first unread message

kieranwebber@gmail.com (JIRA)

unread,
Mar 25, 2018, 9:16:01 AM3/25/18
to jenkinsc...@googlegroups.com
Kieran Webber commented on Bug JENKINS-49076
 
Re: Cannot set custom PATH inside docker container

Just ran into this issue using withEnv in a docker container. Works for everything but PATH currently.

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.3.0#73011-sha1:3c73d0e)
Atlassian logo

steven@sclark.me (JIRA)

unread,
Apr 20, 2018, 10:09:02 PM4/20/18
to jenkinsc...@googlegroups.com

I believe this may be the root cause as well with the failures I'm seeing within a declarative pipeline. Running this PATH from the env command is the normal value.

pipeline {
    agent {
        docker {
            image 'debian:stretch'
        }
    }
    
    environment {
        PATH = "/opt/test/bin:/usr/bin/:/bin"
    }
    
    stages {
        stage('Example') {
            steps {
                sh 'env'
            }
        }
    }
}

steven@sclark.me (JIRA)

unread,
Apr 20, 2018, 10:14:03 PM4/20/18
to jenkinsc...@googlegroups.com
Steven Clark edited a comment on Bug JENKINS-49076
I believe this may be the root cause as well with the failures I'm seeing within a declarative pipeline. Running this PATH from the env command is the normal value and not the value within my pipeline .
This only seems to break when the agent is a docker
{code:java}

pipeline {
    agent {
        docker {
            image 'debian:stretch'
        }
    }
    
    environment {
        PATH = "/opt/test/bin:/usr/bin/:/bin"
    }
    
    stages {
        stage('Example') {
            steps {
                sh 'env'
            }
        }
    }
}{code}

shenrong87@gmail.com (JIRA)

unread,
Aug 6, 2018, 8:39:03 PM8/6/18
to jenkinsc...@googlegroups.com

I'm seeing same issue here.

It is definitely necessary to have a way to set PATH variable inside docker container. We have a PATH environment variable is dynamic changing with Jenkins workspace path that can only set through code.

This message was sent by Atlassian JIRA (v7.10.1#710002-sha1:6efc396)

shenrong87@gmail.com (JIRA)

unread,
Aug 6, 2018, 8:39:03 PM8/6/18
to jenkinsc...@googlegroups.com
Rong Shen edited a comment on Bug JENKINS-49076
I'm seeing same issue here.

It is definitely necessary to have a way to set PATH variable inside docker container. We have a PATH environment variable is dynamic changing with Jenkins workspace path that can only set through code.


Is there a workaround for this issue?

theuno@gmail.com (JIRA)

unread,
Aug 7, 2018, 2:51:05 AM8/7/18
to jenkinsc...@googlegroups.com

I did something like this as workaround:

    // Get the full PATH variable from a started container.
    def pathInContainer
    docker.image(DockerImage).inside() {
      pathInContainer = steps.sh(script: 'echo $PATH', returnStdout: true).trim()
      parameters += "-e PATH=${pathInContainer}:/misc/AddExtraPath "
    }    

    docker.image(DockerImage)
                .inside(parameters) {
      runA()
      runB()
    }
 

It extracts the PATH from a started container, then stops the container, and starts a new one with the extra PATH environment variable (which I extended with the things required by us).

shenrong87@gmail.com (JIRA)

unread,
Aug 7, 2018, 2:12:02 PM8/7/18
to jenkinsc...@googlegroups.com

dodoentertainment@gmail.com (JIRA)

unread,
Nov 5, 2019, 11:57:03 AM11/5/19
to jenkinsc...@googlegroups.com

Any update on this issue? The workaround by Paul Theunissen works, but its very ugly and inpractical for some use cases.

 

This message was sent by Atlassian Jira (v7.13.6#713006-sha1:cc4451f)
Atlassian logo

lukasz.walewski@itsv.at (JIRA)

unread,
Feb 25, 2020, 8:37:02 AM2/25/20
to jenkinsc...@googlegroups.com

Another workaround is to run commands in a modified environment:

pipeline {
    agent {
        docker {
            image 'debian'
        }
    }
    environment {
        PATH = '/some/dir'
    }
    stages {
        stage('Test') {
            steps {
                sh "env PATH=$PATH:\$PATH"
            }
        }
    }
}

The resulting PATH variable contains /some/dir prepended to the PATH value defined inside the container (observe double quotes in the steps directive).

I fully agree with the others, that the option to manipulate the PATH variable inside a container is a must-have. Note, that this feature has nothing to do with setting the PATH inside a container to the value this variable has outside of it, i.e. on the Jenkins node, which would be obviously wrong in most cases.

daniel.sorensen@clarivate.com (JIRA)

unread,
Apr 16, 2020, 8:19:05 AM4/16/20
to jenkinsc...@googlegroups.com

It now works to set custom PATH inside Docker container in a declarative pipeline by using the environment argument -e to the Docker commands.

e.g.

agent {
    docker {
      image 'amazoncorretto:8u202'
      label 'docker'
      args '''
        -v $HOME/tools:$HOME/tools
        -v $HOME/.m2/:$HOME/.m2
        -e PATH="$PATH:/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/MVN-360/bin"
      '''
    } 

[Pipeline] sh
+ env
+ grep PATH
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin:/var/lib/jenkins/tools/hudson.tasks.Maven_MavenInstallation/MVN-360/bin 
This message was sent by Atlassian Jira (v7.13.12#713012-sha1:6e07c38)
Atlassian logo

orion-ua@ukr.net (JIRA)

unread,
Apr 16, 2020, 12:35:05 PM4/16/20
to jenkinsc...@googlegroups.com

Daniel Sorensen, indeed, it works, and that's a good example.

I just want to note that passing environment argument to the docker command will override original PATH value defined in image with the one from Jenkins host, and in some conditions it may produce not the result you expect (this was actually described in JENKINS-43590). Here is such example:

pipeline {
    agent any

    environment {
        NODEJS_VERSION = "NodeJS 12.16.2"
    }

    stages {
        stage('Nodejs inside OpenJDK container 1') {
            agent {
                docker {
                    reuseNode true
                    image 'openjdk:11.0-jdk-slim'
                    args """
                        -v ${JENKINS_HOME}/tools:${JENKINS_HOME}/tools
                        -e PATH="${PATH}:${tool("${NODEJS_VERSION}")}/bin"
                    """
                    // Original PATH from openjdk image:
                    // PATH=/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
                }
            }
            tools { nodejs "${NODEJS_VERSION}" }

            environment {
                PATH_JENKINS_ORIGINAL = "${PATH}"
            }

            steps {
                sh 'env | sort'
                sh 'node --version'

                catchError(buildResult: 'SUCCESS',
                           stageResult: 'FAILURE',
                           message: 'cannot find java') {
                    sh 'java --version'
                }
            }
        }

        stage('Nodejs inside OpenJDK container 2') {
            agent {
                docker {
                    reuseNode true
                    image 'openjdk:11.0-jdk-slim'
                    args """
                        -v ${JENKINS_HOME}/tools:${JENKINS_HOME}/tools
                    """
                }
            }

            tools { nodejs "${NODEJS_VERSION}" }

            environment {
                NODEJS_BIN_PATH = "${tool("${NODEJS_VERSION}")}/bin"
                PATH_JENKINS_ORIGINAL = "${PATH}"
            }

            steps {
                sh '''
                    env | sort
                    export PATH=${PATH}:${NODEJS_BIN_PATH}
                    node --version
                    java --version
                '''
            }
        }
    }
}

 

Reply all
Reply to author
Forward
0 new messages