Docker exits CMD on start

2019-09-03 16:12发布

问题:

I have a docker image that runs play web application. In dockerfile there is CMD which starts the server and it waits until you hit Ctrl+D to exit. If I do:

docker run -d  -i -v

It works correctly - starts the server and waits for ctrl+D.

This is however not the case when i start the container:

docker start -i

Instead the server automatically stops:

--- (Running the application, auto-reloading is enabled) ---

[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000

(Server started, use Ctrl+D to stop and go back to the console...)


[success] Total time: 1 s, completed Jul 27, 2016 11:54:13 AM <--- this indicates that the server was stopped.

How can I force docker start to not stop server?

回答1:

A docker container exits when its main process finishes. Without having an insight into your docker-file (I have no experience with the play framework), you need to make sure that at least one process stays alive.

You have a couple of options:

  • Docker Way
    Try using -it like docker run -it <your framework image> bash to get into the container you are starting. This should keep your window open and allow you to run commands in the container.
  • Docker "debug" way
    Try using the docker inspect <your container> (use docker ps -a to find your container) command to investigate why the container exited. In case you have a start script like start.sh you can try to add while true; do sleep 1000; done to keep the container up to investigate on what it was doing before it exited.
  • Try using the one of the published docker images like - https://hub.docker.com/r/ingensi/play-framework/

P.S. I can not loose the feeling that you are new to docker and are mixing the docker start and the docker run command.