65

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?

1
  • cd is not a command - but a shell built-in - ie. you need to run a shell first. ie. the exec'ed command did not exist, not the directory. Quoting everything passed to the container breaks things - ie. the whole line will be treated as the command to exec', rather than the first item, with the remaining being passed as arguments to exec'ed command. Hence: docker run --rm alpine '/bin/sh -c cd /etc; ls -l' - will fail. While: docker run --rm alpine /bin/sh -c 'cd /etc; ls -l' - will succeed. And: docker run --rm alpine /bin/sh -c cd\ /etc\;\ ls\ -l - will also succeed. Jun 1, 2022 at 13:34

5 Answers 5

57

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'
5
  • 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, 2014 at 22:23
  • Actually both commands output the list of files in /bin directory. Tried both.
    – Glueon
    Oct 9, 2014 at 22:36
  • Not on my box, centos7.
    – seanmcl
    Oct 9, 2014 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, 2014 at 18:31
  • That would depend on your Dockerfile. Doing that would be highly unusual, and some things would still not work (you can't have an unquoted semicolon because the shell interprets that as the end of the docker run command and the beginning of a new one).
    – tripleee
    Dec 20, 2021 at 9:50
137

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
4
  • 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, 2015 at 8:51
  • oh, I got the answer .... flag provided but not defined: -w
    – zx1986
    Nov 27, 2015 at 8:52
  • 3
    Dammit why has this been hiding all the way at the bottom of the docker run reference page! :-)
    – Amir Abiri
    Dec 22, 2015 at 15:38
  • 11
    This should be the accepted answer
    – fferri
    Nov 20, 2017 at 14:15
7

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:
-1

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

1
  • If it's not working, you are not actually using Bash.
    – tripleee
    Dec 20, 2021 at 9:49

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