Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker run <IMAGE> <MULTIPLE COMMANDS>

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.

like image 854
ai0307 Avatar asked Feb 13 '15 01:02

ai0307


People also ask

How do I run multiple commands in Docker?

Multiple commands can be executed in a running Docker container using the docker exec command. If the Docker container is stopped, before running the docker exec command it should be started using the docker run command.

Can Dockerfile run 2 commands?

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect. If CMD is used to provide default arguments for the ENTRYPOINT instruction, both the CMD and ENTRYPOINT instructions should be specified with the JSON array format.

Can you run multiple processes in a Docker container?

It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.

How do I run multiple scripts in Dockerfile?

There's two ways to do this: Have a shell script that runs each service as a background job. Launch a full init system inside the container and launch the services under this.


2 Answers

To run multiple commands in docker, use /bin/bash -c and semicolon ;

docker run image_name /bin/bash -c "cd /path/to/somewhere; python a.py" 

In case we need command2 (python) will be executed if and only if command1 (cd) returned zero (no error) exit status, use && instead of ;

docker run image_name /bin/bash -c "cd /path/to/somewhere && python a.py" 
like image 111
anhlc Avatar answered Sep 22 '22 06:09

anhlc


You can do this a couple of ways:

  1. Use the -w option to change the working directory:

    -w, --workdir="" Working directory inside the container

    https://docs.docker.com/engine/reference/commandline/run/#set-working-directory--w

  2. Pass the entire argument to /bin/bash:

    docker run image /bin/bash -c "cd /path/to/somewhere; python a.py" 
like image 44
John Petrone Avatar answered Sep 23 '22 06:09

John Petrone