Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-42369

agent docker parameters do not honor the current environment

      Currently I need to use multiple docker containers which need to be linked together.
      I tried using the --link option to set the linked image. In my use-case I don't want to set a fixed name but need to use a variable for the name.

      In this case the args parameter is evaluated to "--link null:linkedimage" and the job fails.

      Or is there another way to pass dynamic parameters (from variables) to the docker args?

      pipeline {
          agent any
          environment {
              LINK_DOCKER_IMAGE = "linkedimage"
          }
          stages {
              stage ('Compose') {
                  agent {
                     docker {
                          image 'alpine'
                          args  "--link ${env.LINK_DOCKER_IMAGE}:linkedimage"
                      }
                  }
                  steps {
                          sh 'printenv'
                  }
              }
          }
      }
      

          [JENKINS-42369] agent docker parameters do not honor the current environment

          Hi !
          This bug is very troubelsome for us and the workarounds does not work in all cases... and are workarounds
          Would it be possible to change the priority to accelerate the fix please ?

          Guillaume Dupin added a comment - Hi ! This bug is very troubelsome for us and the workarounds does not work in all cases... and are workarounds Would it be possible to change the priority to accelerate the fix please ?

          Same issue. I am using a dockerfile agent and there is no way (or I don't know one) to pass environment variables to the "docker build" command. Example:

          pipeline {
              environment {
                  SSH_KEY = credentials('ssh-key')
              }
              agent {
                  dockerfile {
                      additionalBuildArgs "--build-arg SSH_KEY=${SSH_KEY}"
                  }
              }
              stages {
                  stage('Test') {
                      steps {
                          sh 'go test'
                      }
                  }
              }
          }
          

          Anatoliy Vlassov added a comment - Same issue. I am using a dockerfile agent and there is no way (or I don't know one) to pass environment variables to the "docker build" command. Example: pipeline { environment { SSH_KEY = credentials( 'ssh-key' ) } agent { dockerfile { additionalBuildArgs "--build-arg SSH_KEY=${SSH_KEY}" } } stages { stage( 'Test' ) { steps { sh 'go test' } } } }

          Vlad Fratila added a comment -

          It would be great for env vars to work with env vars.

          Vlad Fratila added a comment - It would be great for env vars to work with env vars.

          Jorge Machado added a comment -

          I have another use case for this. Running builds with nvidia-docker we need to pass NV_GPU= 0,1 at container start ... This does not work with the environment directive

          Jorge Machado added a comment - I have another use case for this. Running builds with nvidia-docker we need to pass NV_GPU= 0,1 at container start ... This does not work with the environment directive

          Philipp Jung added a comment -

          Same issue here. Any chance to change the priority?

          Philipp Jung added a comment - Same issue here. Any chance to change the priority?

          Omer Sen added a comment - - edited

          Same issue here. Any chance to change the priority? 

           

          Also haven't used it but some one found a workaround for this

           

          https://stackoverflow.com/questions/53882492/jenkins-how-to-use-variables-inside-agent-docker-args

          Omer Sen added a comment - - edited Same issue here. Any chance to change the priority?    Also haven't used it but some one found a workaround for this   https://stackoverflow.com/questions/53882492/jenkins-how-to-use-variables-inside-agent-docker-args

          Sergi Mus added a comment -

          Please fix.

          Sergi Mus added a comment - Please fix.

          Stefan Puiu added a comment -

          It gets more interesting if you need to infer some arguments from shell commands. Say I want to run the Docker container as a certain user, as it's sane, I usually do that using '-u <user_id>', but it would be nice to be able to infer that user id on every node (e.g. '-u $(id -u)').

          Stefan Puiu added a comment - It gets more interesting if you need to infer some arguments from shell commands. Say I want to run the Docker container as a certain user, as it's sane, I usually do that using '-u <user_id>', but it would be nice to be able to infer that user id on every node (e.g. '-u $(id -u)').

          I have exactly the same problem, with the transfer of uid to the container (((

          Sergey Beketov added a comment - I have exactly the same problem, with the transfer of uid to the container (((

          Liam added a comment -

          I have exactly the same problem, with the transfer of uid to the container

          This was our issue too. Our current workaround might help you; we've had to abandon the agent: dockerfile directive for now and instead we use docker build via sh and then tell later steps to use the docker agent & reuse the node it built the image on:

          pipeline {
          
              // Cannot use the agent dockerfile directive at the root as there's no access to environment variables or sh() to be able to
              // set the JENKINS_USER_NAME, etc into build-args. We have to build the image manually from a sh() directive instead and then
              // get the later stages to do reuseNode true - see JENKINS-42369
              agent {
                  label 'BuildNode1'
              }
          
              stages {
          
                  stage('Build image') {
                      steps {
                          sh '''#!/bin/bash
                                set -o errexit
                                set -o nounset
                                set -o pipefail
                                set -o xtrace
                                JENKINS_USER_NAME=$(id -u -n)
                                JENKINS_USER_ID=$(id -u)
                                JENKINS_GROUP_ID=$(id -g)
                                docker build --pull \
                                             --tag="iris-build:latest" \
                                             --build-arg JENKINS_USER_NAME="$JENKINS_USER_NAME" \
                                             --build-arg JENKINS_USER_ID="$JENKINS_USER_ID" \
                                             --build-arg JENKINS_GROUP_ID="$JENKINS_GROUP_ID" \
                                             .
                             '''
                      }
                  }
          
                  stage('Use image') {
                      agent {
                          docker {
                              image 'iris-build:latest'
                              reuseNode true
                          }
                      }
                      stages {
                          // other stages to run inside the container
                      }
                  }
                  
              }
              
          }

          Liam added a comment - I have exactly the same problem, with the transfer of uid to the container This was our issue too. Our current workaround might help you; we've had to abandon the agent: dockerfile directive for now and instead we use docker build via sh and then tell later steps to use the docker agent & reuse the node it built the image on: pipeline { // Cannot use the agent dockerfile directive at the root as there's no access to environment variables or sh() to be able to // set the JENKINS_USER_NAME, etc into build-args. We have to build the image manually from a sh() directive instead and then // get the later stages to do reuseNode true - see JENKINS-42369 agent { label 'BuildNode1' } stages { stage( 'Build image' ) { steps { sh '''#!/bin/bash set -o errexit set -o nounset set -o pipefail set -o xtrace JENKINS_USER_NAME=$(id -u -n) JENKINS_USER_ID=$(id -u) JENKINS_GROUP_ID=$(id -g) docker build --pull \ --tag= "iris-build:latest" \ --build-arg JENKINS_USER_NAME= "$JENKINS_USER_NAME" \ --build-arg JENKINS_USER_ID= "$JENKINS_USER_ID" \ --build-arg JENKINS_GROUP_ID= "$JENKINS_GROUP_ID" \ . ''' } } stage( 'Use image' ) { agent { docker { image 'iris-build:latest' reuseNode true } } stages { // other stages to run inside the container } } } }

            Unassigned Unassigned
            sven_carstens_udg Sven Carstens
            Votes:
            29 Vote for this issue
            Watchers:
            31 Start watching this issue

              Created:
              Updated: