I'm trying to run MULTIPLE commands like this.
docker run image cd /path/to/somewhere && python a.py
But this gives me "No such file or directory" error because it is interpreted as...
"docker run image cd /path/to/somewhere" && "python a.py"
It seems that some ESCAPE characters like "" or () are needed.
So I also tried
docker run image "cd /path/to/somewhere && python a.py"
docker run image (cd /path/to/somewhere && python a.py)
but these didn't work.
I have searched for Docker Run Reference but have not find any hints about ESCAPE characters.
To run multiple commands in docker, use
/bin/bash -c
and semicolon;
In case we need command2 (python) will be executed if and only if command1 (cd) returned zero (no error) exit status, use
&&
instead of;
For anyone else who came here looking to do the same with
docker-compose
you just need to prependbash -c
and enclose multiple commands in quotes, joined together with&&
.So in the OPs example
docker-compose run image bash -c "cd /path/to/somewhere && python a.py"
If you want to store the result in one file outside the container, in your local machine, you can do something like this.
The result of your commands will be available in
/tmp/result.txt
in your local machine.You can do this a couple of ways:
Use the -w option to change the working directory:
https://docs.docker.com/engine/reference/commandline/run/#set-working-directory--w
Pass the entire argument to /bin/bash:
You can also pipe commands inside Docker container,
bash -c "<command1> | <command2>"
for example:But, without invoking the shell in the remote the output will be redirected to the local terminal.