Search code examples
node.jsdockerforever

How to use the forever CLI in Docker?


I am Dockerizing every project of mine and I use a CLI called forever to re-run my scripts if they fail.

I was using the official node.js Docker tutorial to Dockerize my simpler scripts, but when I got to my more complex one (which were using the forever CLI) I didn't know how to run the CLI in Docker.

Is there any way to make this possible, either with using the forever CLI or the module?


Solution

  • I will reiterate Oliver's comment as I think it is actually a valid answer. When running a process as a Docker container, there are mechanisms already in place to handle what forever does for you.

    A quick gloss over the forever actions reveals that it actually already looks a bit like Docker:

      actions:
        start               Start SCRIPT as a daemon
        stop                Stop the daemon SCRIPT by Id|Uid|Pid|Index|Script
        stopall             Stop all running forever scripts
        restart             Restart the daemon SCRIPT
        restartall          Restart all running forever scripts
        list                List all running forever scripts
        config              Lists all forever user configuration
        set <key> <val>     Sets the specified forever config <key>
        clear <key>         Clears the specified forever config <key>
        logs                Lists log files for all forever processes
        logs <script|index> Tails the logs for <script|index>
        columns add <col>   Adds the specified column to the output in `forever list`
        columns rm <col>    Removed the specified column from the output in `forever list`
        columns set <cols>  Set all columns for the output in `forever list`
        cleanlogs           [CAREFUL] Deletes all historical forever log files
    

    Instead of using forever to manage your process, just use Docker:

    • The --restart=always option for docker run is the same underlying concept of forever, to restart the application and keep it running should it fail.
    • docker run is synonymous with forever start. Add -d to docker run to run in the background.
    • docker ps is synonymous with forever list. In the case of Docker, just make each of your forever processes a Docker container.
    • docker logs is synonymous with forever logs.

    This should make it fairly trivial to just make your process the CMD or ENTRYPOINT in the Dockerfile and do away wth forever entirely.

    Further down the road, when you start getting into container orchestration and deployment, have a look at health checks (HEALTHCHECK instruction), Docker Swarm, and Docker Compose.