Control environment variables inside a Docker container

Article ID:226520788
1 minute readKnowledge base

Issue

Control environment variables inside a Docker container with the Docker Pipeline plugin.

Environment

  • CloudBees Jenkins Enterprise

  • Pipeline plugin

  • Docker Pipeline plugin

Resolution

By default, when using the Docker Pipeline plugin the node’s environment variables will not be propagated to the docker container - since with Docker you want to get isolate environments. You can check this here

However, if at Node level (http/s:///computer//configure), under Node Properties, you add Environment variables, those [will be added to the container](https://github.com/jenkinsci/docker-workflow-plugin/blob/7084130bedfbdbb0b8e433f142571f93708bf61a/src/main/java/org/jenkinsci/plugins/docker/workflow/WithContainerStep.java#L124).

docker node properties

If you would like to override again what is injected at Node level then you will need to use withEnvstep.

node ('linux-agent') {
    withEnv(['PATH=/usr/local/Cellar/coreutils/8.25/libexec/gnubin:/usr/local/Cellar/gnu-sed/4.2.2/libexec/gnubin:/Users/fbelzunc/cloudbees/support/support-shinobi-tools:/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home//bin:/Users/fbelzunc/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/MacGPG2/bin:/usr/local/sbin:/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/bundler-1.6.5/bin:/usr/local/Cellar/ruby/2.1.0/lib/ruby/gems/2.1.0/gems/beaker-1.16.0/bin:/usr/local/Cellar/ruby/2.1.0/bin/:/path/testing']) {
        docker.image('maven:3.3.3-jdk-8').inside {
          sh 'echo $PATH'
          sh 'mvn --version'
        }
    }
    sh 'echo $PATH'
}