Docker run to execute script in mount without exiting container automatically?

Stack Overflow Asked by Raj Endla on October 6, 2020

I have a simple bash script ‘’ in the root of mounted folder :

Rscript -e "source('/home/rstudio/mount-folder/src/controller.R')";

However, when i try to mount folder and start the container with docker run as follows:

docker run -d -p 8000:8787 -e ROOT=true -e DISABLE_AUTH=true --name container -v mount-folder/:/home/rstudio/ image_name /home/rstudio/

above run command starts the container but exits automatically.

I am looking for a docker run command that starts the container , mounts the folder and then executes the bash script which is in the mount-folder without exiting the container.

(** dont want to go with docker exec command as it is not suitable for my use case for other reasons)


FROM rocker/rstudio:4.0.2

//some RUN commands to install necessary r packages


CMD tail -f /dev/null

Other details :

  1. Image that i am using is rstudio server from rocker and container runs on AWS ubuntu machine.

Edit :

  1. have also tried adding CMD tail -f /dev/null at the end of dockerfile as suggested in even then the container exits.

2 Answers

When you do docker run [options] image_name [cmd] the command you specify becomes the command for the container and replaces any the command specified in the dockerfile (that's why adding CMD tail -f /dev/null doesn't do anything). If you ran your container without the /home/rstudio/ at the end, it should stay running.

The solution would be to update your script to add the tail command at the end.

Rscript -e "source('/home/rstudio/mount-folder/src/controller.R')";
exec tail -f /dev/null

If you can't update that script, you could instead add it to the command being passed to the container, with something like: docker run [options] image_name bash -c '/home/rstudio/ && exec tail -f /dev/null'

Answered by Andrew Schlei on October 6, 2020

Docker containers shutdown automatically when run in detached mode. I think this article proposes a nice solution:

You could add tail -f /dev/null as the last command in your bash script instead so that the script will never halt unless it is told to do so.

Answered by jboot on October 6, 2020

Add your own answers!

Related Questions

Which is better: O(nm) or O(n log n)?

2  Asked on November 15, 2021 by krankykong


id not assigned in contact list

2  Asked on November 15, 2021 by maxene-graze


Text field problems Xcode

1  Asked on November 15, 2021 by benjo


Javascript map inside of map till condition satisfied

2  Asked on November 15, 2021 by ohsik


How to extract the extract specific key from an dictionaries?

2  Asked on November 15, 2021 by sto-ned


Python – How do you restart a for loop?

2  Asked on November 15, 2021 by cokola7775


One by one making a histogram out each column of a dataframe

2  Asked on November 15, 2021 by dnv89


Setdiff within mutate function

2  Asked on November 15, 2021 by dm89


Center a circle in other device

3  Asked on November 12, 2021 by user13904814


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir