I could not achieve changing the working directory using the suggestion by Vishwas, although I did try creating a Jenkins user inside the container with the same uid as the host. It just didn't work for me. I did however, realize that I didn't actually need to change the directory on the container because the workspace was already in the container.

I'm using the Docker Plugin from CloudBees. After reading their documentation, I realized the workstation folder I thought was on the host machine, was actually in the container. Part of the documentation stated:

The above is a complete Pipeline script. inside will:

  1. Automatically grab an agent and a workspace (no extra node block is required).
  2. Pull the requested image to the Docker server (if not already cached).
  3. Start a container running that image.
  4. Mount the Jenkins workspace as a "volume" inside the container, using the same file path.

After seeing item number four, I ran docker container inspect <containerID> and I saw the workspace /home/jenkins/workspace was actually a volume mount that was mapped to the /home/jenkins/workspace on the Docker host machine as well. Using this as my workspace, I was able to build the various applications within the container and then moved the results to a second volume used for application collection later in the pipeline. It's not an exact answer to my original question, but it works!

Answer from Argyle on Stack Exchange
🌐
Baeldung
baeldung.com › home › jenkins › jenkins pipeline – change to another folder
Jenkins Pipeline – Change to Another Folder | Baeldung on Ops
November 6, 2023 - Using the dir function, the script switches to the directory “scripts”, then runs the build steps. In addition, we used “any” agent in the above script, which means that any available Jenkins agent can execute this pipeline job.
Top answer
1 of 2
2

I could not achieve changing the working directory using the suggestion by Vishwas, although I did try creating a Jenkins user inside the container with the same uid as the host. It just didn't work for me. I did however, realize that I didn't actually need to change the directory on the container because the workspace was already in the container.

I'm using the Docker Plugin from CloudBees. After reading their documentation, I realized the workstation folder I thought was on the host machine, was actually in the container. Part of the documentation stated:

The above is a complete Pipeline script. inside will:

  1. Automatically grab an agent and a workspace (no extra node block is required).
  2. Pull the requested image to the Docker server (if not already cached).
  3. Start a container running that image.
  4. Mount the Jenkins workspace as a "volume" inside the container, using the same file path.

After seeing item number four, I ran docker container inspect <containerID> and I saw the workspace /home/jenkins/workspace was actually a volume mount that was mapped to the /home/jenkins/workspace on the Docker host machine as well. Using this as my workspace, I was able to build the various applications within the container and then moved the results to a second volume used for application collection later in the pipeline. It's not an exact answer to my original question, but it works!

2 of 2
1

If your master is running as Jenkins user, you should make a user in the docker image which you use as to create your node and do the key exchange. For example jenkinsuser with same UID needs to exist on both master and node docker container.

Discussions

groovy - Jenkins pipeline how to change to another folder - Stack Overflow
Currently i am using Jenkins pipeline script. For running one command, I need to access a folder outside its workspace directory. I tried sh "cd $workspace/", but it returned current workspace f... More on stackoverflow.com
🌐 stackoverflow.com
Proper way to use the dir() function inside docker image
Hello I read through a few cases including this and this but I still couldn't resolve my issue My environment is: Jenkins 2.107.1 Docker plugin Using official docker ssh slave docker image Dock... More on github.com
🌐 github.com
1
December 19, 2018
Jenkins pipeline create directory - Stack Overflow
The exception is thrown because you need to explicitly whitelist that script/module: jenkins.io/doc/book/managing/script-approval ... also, if running on a node, that directory will be created on the master. Groovy CPS means that groovy code executes on master and is transported over to the node. More on stackoverflow.com
🌐 stackoverflow.com
java - How to run jenkins pipeline relative to a sub-directory? - Stack Overflow
I have a git repository with 2 modules in it. One is SpringBoot based backend module and another one is VueJS based frontend module. app-root - backend - frontend I have a declarative style More on stackoverflow.com
🌐 stackoverflow.com
🌐
Jenkins
jenkins.io › doc › pipeline › steps › workflow-basic-steps
Pipeline: Basic Steps
Reply-To email address. Defaults to the admin address globally configured for the Jenkins instance. ... To email address list. Comma separated list of email addresses. Returns the current directory path as a string.
🌐
Jenkins
jenkins.io › doc › book › pipeline › syntax
Pipeline Syntax
In order to use this option, the Jenkinsfile must be loaded from either a Multibranch Pipeline or a Pipeline from SCM. Conventionally this is the Dockerfile in the root of the source repository: agent { dockerfile true }. If building a Dockerfile in another directory, use the dir option: agent { dockerfile { dir 'someSubDir' } }. If your Dockerfile has another name, you can specify the file name with the filename option.
Find elsewhere
🌐
Medium
medium.com › @dipali26N › day-39-creating-directories-in-jenkins-pipelines-278953b74f48
DAY:39 Creating Directories in Jenkins Pipelines | by Dipali Naygaonkar | Medium
July 23, 2025 - dir('reports/test_output') { ... } This step tells Jenkins to change the current working directory to reports/test_output inside the workspace.
🌐
Leyaa
leyaa.ai › home › how to use dir in jenkins pipeline: syntax and examples
How to Use dir in Jenkins Pipeline: Syntax and Examples | Leyaa.ai
March 9, 2026 - In Jenkins Pipeline, use the dir step to change the working directory for a block of commands. Wrap the commands inside dir('folderName') { ...
🌐
Jenkins
issues.jenkins.io › browse › JENKINS-46636
Loading...
It's said here, https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#code-dir-code-change-current-directory
🌐
GitHub
github.com › jenkinsci › docker-plugin › issues › 710
Proper way to use the dir() function inside docker image · Issue #710 · jenkinsci/docker-plugin
December 19, 2018 - stage('Build Docker Image') { docker.withRegistry("http://${docker_registry}",'cred-id-from-jenkins') { dockerImage = docker.build("myImage:${env.Build_ID}", "-f mydockerfile") } } stage('Test') { dockerImage.inside('-u root') { sh ''' cd /service npm run testx ''' junit 'xunit.xml' } } ... stage('Test') { dockerImage.inside('-u root') { dir("/service") { npm run testx junit 'xunit.xml' } } }
Author   jenkinsci
🌐
Packtpub
subscription.packtpub.com › book › cloud_and_networking › 9781838824327 › 10 › ch10lvl1sec84 › understanding-the-directory-structure
10 Shared Libraries | Jenkins Administrator's Guide
The common utility functions such as helper functions for Slack are stored in the vars directory. It may seem strange that the files containing function definitions are stored in a directory named vars, because vars is short for variables, not functions. This is because the files in the vars directory become available to Jenkinsfiles as variables.
🌐
Jenkins
jenkins.io › doc › pipeline › steps › pipeline-utility-steps
Pipeline Utility Steps
Ant style pattern of file paths that should match. If this property is set all descendants of the current working directory will be searched for a match and returned, if it is omitted only the direct descendants of the directory will be returned.
🌐
Jenkins
jenkins.io › doc › pipeline › steps › workflow-durable-task-step
Pipeline: Nodes and Processes
The relative path of the directory in the workspace to use as a new working directory.
🌐
Google Groups
groups.google.com › g › jenkinsci-dev › c › 19URbEGNKMk
workflow issue with "dir"
From the error message, it seems "dir" requires a block containing the commands to run in the specified directory. Try something like dir ('foobar') { sh 'pwd' > -- > You received this message because you are subscribed to the Google Groups "Jenkins Developers" group.
🌐
Jenkins-ci
javadoc.jenkins-ci.org › hudson › class-use › FilePath.html
Uses of Class hudson.FilePath (Jenkins core 2.545 API)
Gets the FilePath representation of the "~" directory (User's home directory in the Unix sense) of the given channel.
🌐
Reddit
reddit.com › r/jenkinsci › how to use a directory across stages?
r/jenkinsci on Reddit: How to use a directory across stages?
October 26, 2020 -

Is there any way to create a directory in jenkins declarative pipeline and use it across stages?

i.e

node(){
checkout scm
sh "mkdir ${WORKSPACE/conan}" def String dockerImage = 'development-tools'
stage('Testing') {

parallel (
'Release Build': { docker.image(dockerImage).inside(dockerArgs) { 

sh "touch file.txt && mv file.txt  ${WORKSPACE/conan"
 }
 }
stage('Deploy')
 // use file.txt here again
}