69

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?

schmmd
  • 793
  • 1
  • 5
  • 5
  • 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. – Android Control Jun 01 '22 at 13:34

5 Answers5

142

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
59

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'
Glueon
  • 3,714
  • 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 09 '14 at 22:23
  • Actually both commands output the list of files in /bin directory. Tried both. – Glueon Oct 09 '14 at 22:36
  • Not on my box, centos7. – seanmcl Oct 09 '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
  • 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 '21 at 09:50
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