Skip to main content

This post explains that/why environment variables don't propagate to Docker agent.

If you need a custom container, you could define path and whatever else as layers on top of base image in a Dockerfile. Then using that image in pipeline would be what I think is usual way of doing it. You can build that image outside of Jenkins, but it's also possible to build container from within pipeline too if you want (get Dockerfile from SCM or some other use case).

In the context of your question, I think you can still use base image, and if you know full path you'd like to set, do that at container (agent) startup with args. But $PATH variable won't be available at host to be resolved within container at startup. If you must dynamically set path in container, could adjust it in shells.

pipeline {
    agent {
        docker { 
            image 'python:3.9'
            // cannot resolve $PATH var in container (becasue HOST does not know what it is).
            args '-e PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/i/know/my/full/path/ahead/of/time'
        }
    }
    stages {
        stage('Test') {
            steps {
                
                // as set by args above. Applies to all shells
                sh 'echo $PATH'
                
                // You can also adjust it within container in each shell
                sh '''
                    export PATH=/even/more/path:$PATH
                    echo $PATH
                   '''

                // But this is back to what's in args in a new shell instance
                sh 'echo $PATH'
            }
        }
    }
}

}

This post explains that/why environment variables don't propagate to Docker agent.

If you need a custom container, you could define path and whatever else as layers on top of base image in a Dockerfile. Then using that image in pipeline would be what I think is usual way of doing it. You can build that image outside of Jenkins, but it's also possible to build container from within pipeline too if you want (get Dockerfile from SCM or some other use case).

In the context of your question, I think you can still use base image, and if you know full path you'd like to set, do that at container (agent) startup with args. But $PATH variable won't be available at host to be resolved within container at startup. If you must dynamically set path in container, could adjust it in shells.

pipeline {
agent {
    docker { 
        image 'python:3.9'
        // cannot resolve $PATH var in container (becasue HOST does not know what it is).
        args '-e PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/i/know/my/full/path/ahead/of/time'
    }
}
stages {
    stage('Test') {
        steps {
            
            // as set by args above. Applies to all shells
            sh 'echo $PATH'
            
            // You can also adjust it within container in each shell
            sh '''
                export PATH=/even/more/path:$PATH
                echo $PATH
               '''

            // But this is back to what's in args in a new shell instance
            sh 'echo $PATH'
        }
    }
}

}

This post explains that/why environment variables don't propagate to Docker agent.

If you need a custom container, you could define path and whatever else as layers on top of base image in a Dockerfile. Then using that image in pipeline would be what I think is usual way of doing it. You can build that image outside of Jenkins, but it's also possible to build container from within pipeline too if you want (get Dockerfile from SCM or some other use case).

In the context of your question, I think you can still use base image, and if you know full path you'd like to set, do that at container (agent) startup with args. But $PATH variable won't be available at host to be resolved within container at startup. If you must dynamically set path in container, could adjust it in shells.

pipeline {
    agent {
        docker { 
            image 'python:3.9'
            // cannot resolve $PATH var in container (becasue HOST does not know what it is).
            args '-e PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/i/know/my/full/path/ahead/of/time'
        }
    }
    stages {
        stage('Test') {
            steps {
                
                // as set by args above. Applies to all shells
                sh 'echo $PATH'
                
                // You can also adjust it within container in each shell
                sh '''
                    export PATH=/even/more/path:$PATH
                    echo $PATH
                   '''

                // But this is back to what's in args in a new shell instance
                sh 'echo $PATH'
            }
        }
    }
}
Source Link
apo
  • 86
  • 4

This post explains that/why environment variables don't propagate to Docker agent.

If you need a custom container, you could define path and whatever else as layers on top of base image in a Dockerfile. Then using that image in pipeline would be what I think is usual way of doing it. You can build that image outside of Jenkins, but it's also possible to build container from within pipeline too if you want (get Dockerfile from SCM or some other use case).

In the context of your question, I think you can still use base image, and if you know full path you'd like to set, do that at container (agent) startup with args. But $PATH variable won't be available at host to be resolved within container at startup. If you must dynamically set path in container, could adjust it in shells.

pipeline {
agent {
    docker { 
        image 'python:3.9'
        // cannot resolve $PATH var in container (becasue HOST does not know what it is).
        args '-e PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/i/know/my/full/path/ahead/of/time'
    }
}
stages {
    stage('Test') {
        steps {
            
            // as set by args above. Applies to all shells
            sh 'echo $PATH'
            
            // You can also adjust it within container in each shell
            sh '''
                export PATH=/even/more/path:$PATH
                echo $PATH
               '''

            // But this is back to what's in args in a new shell instance
            sh 'echo $PATH'
        }
    }
}

}