How to execute a bash script in the docker container from the host such that it should not get exited from the container after execution of script ?

2 Answers 2


You can try to use bash as an interactive non-login shell with option --rcfile file.


When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force Bash to read and execute commands from file instead of ~/.bashrc.

So use

bash --rcfile <(echo '. ~/.bashrc; some_command')

as mentioned in this post.

  • Thank you Egor. I could reach into the container from host through the script file but commands are not getting executed in the container Commented Aug 22, 2017 at 14:58
  • What command do you use to start rontainer? Commented Aug 23, 2017 at 5:13

Look at using docker exec along with -d for the detached flag i.e.

docker exec -d <containerid> /path/to/script 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .