1

I run that:

docker run -it --restart always  --net=host myImage /bin/bash 

And I stay interactive then I run (inside the docker container)

/start.sh 

and everything is ok

My idea is to run /start.sh directly with docker run command

I tried that:

docker run -it --restart always  --net=host myImage /bin/bash -c "/start.sh"

Which seems to work, but once the start.sh finishes tasks, docker quits the interactive mode

start.sh only contains starting service

service apache start;
service mysql start;
pm2 start /chat.js;
yarek
  • 11,278
  • 30
  • 120
  • 219
  • This is very common problem that everyone bumps into when starting with docker. The way you are trying to do it makes your script run as `PID 1` and after it stops, the container stops too. This happens because containers are up & running as long as PID 1 is running. – tgogos Jun 29 '18 at 10:35
  • In the Dockerfile [reference](https://docs.docker.com/engine/reference/builder/#exec-form-entrypoint-example) there is a section that describes how you can run a starter script and then pass the Unix signals to a different executable by using `exec "$@"`. The idea here is that, this executable will be responsible to catch the signals and gracefully stop all the services that you are running. – tgogos Jun 29 '18 at 10:40
  • You can also take a look at this: [Docker container will automatically stop after “docker run -d”](https://stackoverflow.com/questions/30209776/docker-container-will-automatically-stop-after-docker-run-d) where `tail -f /dev/null` is proposed to keep the container running. – tgogos Jun 29 '18 at 10:48
  • Understand the issue, but cannot really understand why there is no simple solution for such basic and common task – yarek Jun 29 '18 at 10:52
  • Would it work with tail -f /dev/null & My goal is to have access to the console – yarek Jun 29 '18 at 10:58
  • Why don't you give it a try, put it as the last command of your `start.sh` file to see what happens. To access your container console use [this](https://stackoverflow.com/questions/20932357/how-to-enter-in-a-docker-container-already-running-with-a-new-tty). – tgogos Jun 29 '18 at 11:01

0 Answers0