How to run multiple commands in docker at once

+3 votes

I'm trying to execute multiple commands like this.

docker run image cd /some/path && python a.py

But I get a "No such file or directory" error.

Jul 19, 2018 in Docker by Tyrion anex
• 8,700 points
250,295 views

12 answers to this question.

+3 votes

In order to run multiple commands in docker, use /bin/bash -c with a semicolon ;

docker run image /bin/bash -c "cd /some/path; python a.py"

In this case the second command (python) will be executed only if the first command (cd) returns no error or an exit status. To avoid this use && instead of ; (semi-colon)

docker run image /bin/bash -c "cd /some/path && python a.py"

Ready to level up your Docker skills? Enroll now in our comprehensive Docker Course!

answered Jul 19, 2018 by Sophie may
• 10,620 points
0 votes

Pass the entire argument to /bin/bash:

docker run image /bin/bash -c "cd /path/to/somewhere; python a.py"
answered Nov 28, 2018 by krishti
0 votes

You can pipe commands inside Docker container

bash -c "<command_1> | <command_2>"

answered Nov 28, 2018 by akaash
0 votes

You can pipe commands inside Docker container, bash -c "<command1> | <command2>"for example:

docker run img /bin/bash -c "ls -1 | wc -l"
answered Dec 10, 2018 by Abhi
0 votes

Hey @Tyrion, Replace your entrypoint with sh /www/docker-start.sh. Hope it helps

answered Dec 10, 2018 by Zoo
0 votes

 The issue seems to be with the entrypoint script /docker-entrypoint.sh. The script needs to have executable permission (chmod +x).

answered Dec 10, 2018 by Shrishti
0 votes

Try running the following commad:

["bash", "/start_up.sh"]

answered Dec 10, 2018 by Vedant
0 votes

If non of these solutions work, try adding this in your start_up.sh file to get more details about the failure.

set -x

answered Dec 10, 2018 by Haider
0 votes

Change this in your dockerfile:

CMD ["/run.sh"]
to
ENTRYPOINT ["/bin/bash", "/run.sh"]
answered Dec 10, 2018 by Laksha
0 votes

Hey @Tyrion, Are you running on local machine or remote? If you mount any volume at your source directory (where script is located) it might override entire directory and thus unable to find your script.

answered Dec 10, 2018 by Akaash
0 votes

Try this:

docker run image bash -c "cd /some/path && python a.py"
or /bin/sh
answered Apr 17, 2019 by anonymous

edited Apr 17, 2019 by Kalgi
Thanks, it worked for me.
0 votes

Hi,

You need to use one shell to run the command in docker. You can use bash shell as well. The below example shows how to run multiple commands using a shell in docker.

$ docker run image /bin/bash -c "date; cal"
answered Dec 9, 2020 by MD
• 95,460 points

Related Questions In Docker

0 votes
1 answer
0 votes
1 answer

How to create a container and run images in docker?

Look for what all images you have ...READ MORE

answered Feb 23, 2019 in Docker by Kalgi
• 52,350 points
2,236 views
+1 vote
2 answers

How to run docker containers on different machines

You can use labels and selectors for ...READ MORE

answered Oct 23, 2018 in Docker by Laila
3,604 views
0 votes
1 answer

How to store data in external drive with Docker Postgres:9.3 image?

Apparently, the problem would be in your ...READ MORE

answered Jul 12, 2018 in Docker by Kalgi
• 2,680 points
2,642 views
+2 votes
1 answer
+2 votes
1 answer

Deploy Docker Containers from Docker Cloud

To solve this problem, I followed advice ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
2,731 views
0 votes
1 answer

How to run a docker command from inside the container?

You must have come across the /var/run/docker.sock file, ...READ MORE

answered Jun 28, 2018 in Docker by Sophie may
• 10,620 points
3,536 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP