4

Why my docker exit after excute my shell script? Thanks.

Docker version:

docker --version
Docker version 1.12.4, build 1564f02

My images:

docker pull lw96/ubuntu-16.04-lnmp1.3

After I run:

docker run -it -d --name test -p 8080:80 lw96/ubuntu-16.04-lnmp1.3 sh /root/run.sh

And I checked with:docker ps -a

root@ubuntu:/home/liwei# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                  NAMES
68c0ce4c59f7        a57b0c1a60cf        "/bin/bash"         2 minutes ago       Up 2 minutes        0.0.0.0:8080->80/tcp   test

Here's run.sh:

#! /bin/bash
lnmp restart
echo "lnmp already start"
exit 0

And I got logs:

root@ubuntu:/home/liwei# docker logs -f test
+-------------------------------------------+
|    Manager for LNMP, Written by Licess    |
+-------------------------------------------+
|              http://lnmp.org              |
+-------------------------------------------+
Stoping LNMP...
Stoping nginx... nginx is not running.
 * MySQL server PID file could not be found!
Gracefully shutting down php-fpm /etc/init.d/php-fpm: 82: kill: No such process

................................... failed. Use force-quit
Starting LNMP...
Starting nginx...  done
Starting MySQL
.. * 
Starting php-fpm  done
lnmp already start!

AFTER THAT, MY DOCKER CONTAINER EXIT:

root@ubuntu:/home/liwei# docker ps -a
CONTAINER ID        IMAGE                       COMMAND             CREATED             STATUS                     PORTS               NAMES
cb98d7427802        lw96/ubuntu-16.04-lnmp1.3   "sh /root/run.sh"   8 minutes ago       Exited (0) 7 minutes ago                       test

But, when I use:

docker pull lw96/ubuntu-16.04-lnmp1.3

docker run -it -d -p 80:80 --name test  lw96/ubuntu-16.04-lnmp1.3

docker exec -it test /bin/bash

cd ~ && ./run.sh

My container works well. Why?????

wei L.
  • 61
  • 1
  • 4

1 Answers1

3

a docker container runs as long as the main process is running, it will then end as soon as the entry point/command process has exited. In your case as soon as you reach the exit 0 line, your process is done and docker container should exit.

if you want to prevent that, you have to keep your process running. There you have lots of options like running your process in foreground or just keep the process running by doing something like tail -f /some/log/file where the log comes from your background running process.

mohamnag
  • 2,709
  • 5
  • 27
  • 40
  • also `sleep infinity` or `while true; do echo hello world; sleep 1; done` also work fine – user2915097 Dec 15 '16 at 08:54
  • basically anything that keeps going, the more meaningful the better! – mohamnag Dec 15 '16 at 08:55
  • @mohamnag I got your point. And I try to add `sleep infinity` to my run.sh file, then `docker run -it -d --name test -p 8080:80 lw96/ubuntu-16.04-lnmp1.3 sh /root/run.sh` works fine.Also,like `tail -f /dev/null` can do help, more compatitable for some reason from[Bash: infinite sleep (infinite blocking)](http://stackoverflow.com/questions/2935183/bash-infinite-sleep-infinite-blocking) – wei L. Dec 15 '16 at 09:19
  • 1
    they will all do, however I would really go for a better solution like running something in foreground. but for start all are good enough. – mohamnag Dec 15 '16 at 09:38
  • 1
    This question should have been closed as a duplicate of this existing question: http://stackoverflow.com/questions/30209776/docker-container-will-automatically-stop-after-docker-run-d – nwinkler Dec 15 '16 at 10:25
  • @nwinkler may be a duplicate, is that a reason to down vote the answer? – mohamnag Dec 15 '16 at 14:49
  • @nwinkler Thanks a lot. I checked that. After google and stack search for a while, I do the question. – wei L. Dec 16 '16 at 01:05