In my Dockerfile, I run a script:

RUN /bin/sh -c scripts/init.sh

Inside init.sh, all commands ending with & are not executed: I cannot run background processes. Any idea why?

  • have tried to use the full path of init.sh ?
    – ebal
    Aug 1, 2016 at 17:40
  • @ebal No, why? Relative paths are ok in the other commands of init.sh. I'll try tomorrow.
    – clemtoy
    Aug 1, 2016 at 18:49
  • To solve my problem, I use docker exec -d my_command after the build...
    – clemtoy
    Aug 3, 2016 at 13:55
  • Can you explain what are you trying to do a bit more? Is this a background process you need running at image build time or to run inside the container? Does your init script exit after running the background tasks?
    – Matt
    Jan 9, 2018 at 13:30

3 Answers 3


I had the similar issue and something like the following helped me.

RUN nohup bash -c "scripts/init.sh &" && sleep 4

In many cases the server you started isn’t yet fully ready. To allow the server a bit more time to get ready add a sleep command. How large the argument sleep needs to depend on the service you start and you probably need to tweak it.

Read more on this Doc


To run something in the background:

RUN bash -c "sh ./scripts/init.sh & sleep 5 && tail -F /dev/null"

This will run your script and immediately sleeps for 5 seconds, then keeps container running forever. Using this technique, you can run multiple commands in a Docker.

You can remove tail -F /dev/null or replace with the next command/service which doesn't exit and keeps container running.


My first idea is to create services inside the container instead of running them with nohup or &, run them as system service and you don't need to handle them in init.sh.

But this is not a "real" docker approach. If you need more than 1 service to run, separate them to different containers (1 container - 1 service) and put all of them together with a docker-compose solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.