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

dir('foo') inside "docker.image().inside{}" does not affect CWD of launched processes

    XMLWordPrintable

Details

    Description

      The "dir('foo')" step inside "docker.image('my-image').inside" creates a directory "foo@tmp" instead of changing the current directory as expected.

      In the test below, ">>> SHOULD LS THE foo DIR" is showing an "ls" is the parent dir instead of changing to the "foo" dir and we see a dir "foo@tmp" that have been created.

      This problem is specific to "docker.image('my-image').inside", it does not happen with a standard "node".

      docker.image('cloudbees/java-build-tools:0.0.7.1').inside {
          sh 'rm -rf foo*' // RE INIT TEST
          println '>>> LS THE PARENT DIR'
          sh 'mkdir -p foo/bar'
          sh 'ls -al'
          dir('foo') {
              println '>>> SHOULD LS THE foo DIR'
              sh 'ls -al'
          }
      }
      
      Started by user admin
      [Pipeline] Allocate node : Start
      Running on jenkins-agent.beesshop.org in /home/ubuntu/jenkins-aws-home/workspace/tests/test-change-dir-4
      [Pipeline] node {
      [Pipeline] sh
      [test-change-dir-4] Running shell script
      + docker inspect -f . cloudbees/java-build-tools:0.0.7.1
      .
      [Pipeline] Run build steps inside a Docker container : Start
      $ docker run -t -d -u 1000:1000 -w /home/ubuntu/jenkins-aws-home/workspace/tests/test-change-dir-4 -v /home/ubuntu/jenkins-aws-home/workspace/tests/test-change-dir-4:/home/ubuntu/jenkins-aws-home/workspace/tests/test-change-dir-4:rw -v /home/ubuntu/jenkins-aws-home/workspace/tests/test-change-dir-4@tmp:/home/ubuntu/jenkins-aws-home/workspace/tests/test-change-dir-4@tmp:rw -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** cloudbees/java-build-tools:0.0.7.1 cat
      [Pipeline] withDockerContainer {
      [Pipeline] sh
      [test-change-dir-4] Running shell script
      + rm -rf foo foo@tmp
      [Pipeline] echo
      >>> LS THE PARENT DIR
      [Pipeline] sh
      [test-change-dir-4] Running shell script
      + mkdir -p foo/bar
      [Pipeline] sh
      [test-change-dir-4] Running shell script
      + ls -al
      total 12
      drwxrwxr-x 3 jenkins jenkins 4096 Mar 13 21:50 .
      drwxr-xr-x 4 root    root    4096 Mar 13 21:50 ..
      drwxr-xr-x 3 jenkins jenkins 4096 Mar 13 21:50 foo
      [Pipeline] Change current directory : Start
      Running in /home/ubuntu/jenkins-aws-home/workspace/tests/test-change-dir-4/foo
      [Pipeline] dir {
      [Pipeline] echo
      >>> SHOULD LS THE foo DIR
      [Pipeline] sh
      [foo] Running shell script
      + ls -al
      total 16
      drwxrwxr-x 4 jenkins jenkins 4096 Mar 13 21:50 .
      drwxr-xr-x 4 root    root    4096 Mar 13 21:50 ..
      drwxr-xr-x 3 jenkins jenkins 4096 Mar 13 21:50 foo
      drwxrwxr-x 3 jenkins jenkins 4096 Mar 13 21:50 foo@tmp
      [Pipeline] } //dir
      [Pipeline] Change current directory : End
      

      Attachments

        Issue Links

          Activity

            amcsi Attila Szeremi added a comment - - edited

            duemir oh, so it needs to create a directory like that as a sort of preparation.

            But it doesn't make sense to me how it's expecting for the parent of the workdir to be writable.

            I thought `/var/www` is fine for a workdir, but of course `/var` wouldn't be writable to the default user under normal circumstances. A popular article suggests to use `/app` as the workdir, where of course `/` wouldn't be writable. Why isn't Docker writing to `/var/tmp` instead which is writable? What am I (or others) expected to do instead?

             

            amcsi Attila Szeremi added a comment - - edited duemir oh, so it needs to create a directory like that as a sort of preparation. But it doesn't make sense to me how it's expecting for the parent of the workdir to be writable. I thought `/var/www` is fine for a workdir, but of course `/var` wouldn't be writable to the default user under normal circumstances. A popular article suggests to use `/app` as the workdir, where of course `/` wouldn't be writable. Why isn't Docker writing to `/var/tmp` instead which is writable? What am I (or others) expected to do instead?  
            jglick Jesse Glick added a comment -

            amcsi dir with an absolute path is not supported inside a Docker container. Not really related to this issue. Simply start your sh script with cd. Or avoid using the withDockerContainer step alogether—if it works perfectly for you out of the box, great, otherwise forget about it.

            jglick Jesse Glick added a comment - amcsi dir with an absolute path is not supported inside a Docker container. Not really related to this issue. Simply start your sh script with cd . Or avoid using the withDockerContainer step alogether—if it works perfectly for you out of the box, great, otherwise forget about it.
            cowlinator p cowlinator added a comment - - edited

            FYI, you need Docker Pipeline plugin v 1.15+ and Docker v 17.12+ in order to see this fix.

            cowlinator p cowlinator added a comment - - edited FYI, you need Docker Pipeline plugin v 1.15+ and Docker v 17.12+ in order to see this fix.
            jinzoo john dow added a comment -

            I am still seeing this issue with Docker Pipeline v 1.22 and Docker v 19.03

            Jenkinfile:

            pipeline {
               agent { docker { 
                 image 'ruby'
                 args '-u root'
                } 
             }
             stages {
               stage('build') {
                 steps {
                   sh '$PWD'
                   sh 'ruby --version'
                 }
               }
             }

            I get the following error:

             $ docker run -t -d -u 1000:996 -u root -w /var/jenkins_home/workspace/build-chef-pkgs --volumes-from 0d3a069ee976a6e027b7605d0b4d3abada6bcc9c1b1afcaf5ffa0610e4e83b76 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** ruby cat $ docker top c9b3ddcd8df5ea3e447f163006cd42a057ca55eea49f6df1c806d0e010459bfc -eo pid,comm [Pipeline] { [Pipeline] stage [Pipeline] { (build) [Pipeline] sh + /var/jenkins_home/workspace/build-chef-pkgs /var/jenkins_home/workspace/build-chef-pkgs@tmp/durable-e13f4856/script.sh: 1: /var/jenkins_home/workspace/build-chef-pkgs@tmp/durable-e13f4856/script.sh: /var/jenkins_home/workspace/build-chef-pkgs: Permission denied

             

            The working directory is mounted as root and I cannot override the default user or working directory arguments passed to the docker run command

            jinzoo john dow added a comment - I am still seeing this issue with Docker Pipeline v 1.22 and Docker v 19.03 Jenkinfile: pipeline {    agent { docker {      image 'ruby'      args '-u root'     } } stages {    stage( 'build' ) {      steps {        sh '$PWD'        sh 'ruby --version'      }    } } I get the following error: $ docker run -t -d -u 1000:996 -u root -w / var /jenkins_home/workspace/build-chef-pkgs --volumes-from 0d3a069ee976a6e027b7605d0b4d3abada6bcc9c1b1afcaf5ffa0610e4e83b76 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** ruby cat $ docker top c9b3ddcd8df5ea3e447f163006cd42a057ca55eea49f6df1c806d0e010459bfc -eo pid,comm [Pipeline] { [Pipeline] stage [Pipeline] { (build) [Pipeline] sh + / var /jenkins_home/workspace/build-chef-pkgs / var /jenkins_home/workspace/build-chef-pkgs@tmp/durable-e13f4856/script.sh: 1: / var /jenkins_home/workspace/build-chef-pkgs@tmp/durable-e13f4856/script.sh: / var /jenkins_home/workspace/build-chef-pkgs: Permission denied   The working directory is mounted as root and I cannot override the default user or working directory arguments passed to the docker run command
            weakcamel Waldek M added a comment - - edited

            jinzoo Have you given the example you wanted here? The Jenkinsfile you posted doesn't use dir () . Also, did you mean to use `sh "echo $PWD"` ?

             

            Also, you did specify "-u root" in run args, which is exactly what you got.

            weakcamel Waldek M added a comment - - edited jinzoo Have you given the example you wanted here? The Jenkinsfile you posted doesn't use dir () . Also, did you mean to use `sh "echo $PWD"` ?   Also, you did specify "-u root" in run args, which is exactly what you got.

            People

              ndeloof Nicolas De Loof
              cleclerc Cyrille Le Clerc
              Votes:
              49 Vote for this issue
              Watchers:
              74 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: