Why can't I cd to a directory with docker run?

I need to run an application from a specific directory.

$ sudo docker run -P ubuntu/decomposer 'cd /local/deploy/decomposer; ./decomposer-4-15-2014'
2014/10/09 21:30:03 exec: "cd /local/deploy/decomposer; ./decomposer-4-15-2014": stat cd /local/deploy/decomposer; ./decomposer-4-15-2014: no such file or directory

That directory definitely exists, and if I connect to docker by running bash interactively I can run the above command.

$ sudo docker run -i -t ubuntu/decomposer /bin/bash
# cd /local/deploy/decomposer; ./decomposer-4-15-2014

I can run my program by specifying the full path, but then it crashes as it expects to be launched from the current directory. What can I do?

You can use -w option to change your working directory.

docker run

  -w, --workdir=""           Working directory inside the container

So, in your case, you'd run:

sudo docker run -w /local/deploy/decomposer -P ubuntu/decomposer ./decomposer-4-15-2014

Pass your command as an argument to /bin/sh like this:

sudo docker run -P ubuntu/decomposer /bin/sh -c 'cd /local/deploy/decomposer; ./decomposer-4-15-2014'

Use WORKDIR in your Dockerfile to set the working directory. Then you can run your command with EXEC.

This might me be due to the permission issue or the instance is not able to find the executable available path. To check this try adding the executable available location in path and try running the script

to add the current directory in path kindly use the below command

$ export PATH=$PATH:

I have a similar example from this website https://djangostars.com/blog/what-is-docker-and-how-to-use-it-with-python/#header13:

docker run -d --name "test-nginx" -p 8080:80 -v $(pwd):/usr/share/nginx/html:ro nginx:latest

This command does not work on Git Bash and Windows.

The solution was to use cd instead of $(pwd):

docker run -d --name "test-nginx" -p 8080:80 -v cd:/usr/share/nginx/html:ro nginx:latest