Search code examples
dockeruwsgi

What's the right way to keep a Docker container alive? (uWSGI related)


Noob here. So I'm trying to deploy a web application using AWS + docker + nginx + uWSGI + flask. As far as I know, a docker container must have some sort of foreground job to keep itself alive, otherwise it will kill itself after execution.

I've noticed that uWSGI, when not running as a daemon, is a foreground process so I thought I could use that to keep my container alive. Problem is, once it's running on foreground there is absolutely no way to go back to the shell... (plz lmk if I'm wrong here!) which means I can't exec into my container and run commands on it whenever I need server maintenance.

I've seen practices where you just keep a dummy processing running, echoing a simple string every few seconds or so. But this looks kind of clunky -- Is this really the desirable approach? If it is, is there a better idea of a dummy process than echoing that could possibly keep my console a little bit cleaner?

Thanks in advance.


Solution

  • Docker image is created using Docker file and in Docker file you specify the steps needed to create the image, You noticed the last instruction in Docker file is usually a CMD when you run an image using this command docker run image-name docker executes the command you specified in CMD in container and if this command ends, your container stops. but you can replace this command with something else like docker run -it image-name bash this command gives you an interactive shell and if you exit from it(terminate it) your container also stops.(you replaced the last command you specified in CMD with bash here)

    docker exec -it container-name /bin/bash : this command is when you want to start an interactive shell in a running container