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?


Solution 1:

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'

Solution 2:

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

Solution 3:

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