39

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?

34

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'
  • docker run centos /bin/sh -c 'cd /bin; /bin/ls' prints nothing, while docker run centos /bin/sh -c '/bin/ls /bin' works. – seanmcl Oct 9 '14 at 22:23
  • Actually both commands output the list of files in /bin directory. Tried both. – Glueon Oct 9 '14 at 22:36
  • Not on my box, centos7. – seanmcl Oct 9 '14 at 22:44
  • 1
    Actually, even just removing the quotes around my command to run worked fine. I think docker wraps it in a /bin/sh -c anyway. – schmmd Oct 10 '14 at 18:31
86

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
  • Could I use -w with docker exec ? I am looking for a way to run python script in my container, but can't stop it outside the container. – zx1986 Nov 27 '15 at 8:51
  • oh, I got the answer .... flag provided but not defined: -w – zx1986 Nov 27 '15 at 8:52
  • 1
    Dammit why has this been hiding all the way at the bottom of the docker run reference page! :-) – Amir Abiri Dec 22 '15 at 15:38
  • 5
    This should be the accepted answer – fferri Nov 20 '17 at 14:15
4

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

0

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.