90

I used this command:
docker exec compassionate_mclean cd /root/python
The error returned is

docker-exec: failed to exec: exec: "cd": executable file not found in $PATH

Kindly help me out

sabarish
  • 1,059
  • 1
  • 11
  • 14

5 Answers5

188

cd is a built-in shell command, you can't set it as the command to run. You have to use:

docker exec -i compassionate_mclean bash -c "cd /root/python && python myscript.py"

If you want to see the output make sure to add the -i flag as shown above. In this case however, you can simply run python as your entrypoint:

docker exec -i compassionate_mclean python /root/python/myscript.py
Abdullah Jibaly
  • 53,220
  • 42
  • 124
  • 197
  • But after this command I need to run a python script located at that directory.How do I do that? – sabarish Jan 20 '15 at 05:11
  • But I can't stop the script by Ctrl + C in my local box, right? Because `bash -c` will run another child shell that I can't outside the docker container. – zx1986 Nov 27 '15 at 08:48
4

You can't do that, you can do either docker exec -it my_container /bin/bash and then issue several commands with this interactive sessions, or docker exec -d my_container touch myfile if you just want to create a file, see the examples at https://docs.docker.com/reference/commandline/cli/#examples_3

user2915097
  • 30,758
  • 6
  • 57
  • 59
3

You can use the -w option of the 'docker exec'-command to set the working-folder as an absolute path in the container. But you would have to set it on every docker-call!

e.g.

docker exec -w /root/python compassionate_mclean python myscript.py
dertom
  • 780
  • 5
  • 5
0

In my case I should cd into the subfolder first, where my celery file located. Then run the command

MisterCat
  • 1,531
  • 3
  • 13
  • 23
-3

If u execute docker container exec --help , it will show the options and method to execute the command Usage: docker container exec [OPTIONS] CONTAINER COMMAND [ARG...]

U have to use docker container exec -it [container_name] bash

Once u are in bash then u can execute any command you wish. Doing CD wont work.

whysoseriousson
  • 196
  • 2
  • 16