How to run script and keep it running

So I have this setup I’m trying to get working in a container. Basically it’s a homegrown web app that uses php web sockets. Typically on a normal linux vm I can just run a startup script and one of things it does is run php referencing a php file so it runs as a daemon. The issue is if I run this script in my dockerfile it will run the script and then shutdown the container immediately. I was able to get it to run by referencing the php script in the dockerfile but the script actually does things to make sure things are in a good place before running the PHP daemon. Any ideas how to run a script that calls php and it will keep running?

Have you thought about simply running a PHP-Apache image as container for your web service?

Quick & dirty command line:

docker run -d -p 80:80 --name my-php-app -v "$PWD":/var/www/html php:8-apache

Can you show an example what you try to do? There are multiple solutions, but I’m not sure I understand what you want to solve in the first place.

  • Do you use the “php” command in terminal to execute a php script that runs forever?
  • Do you want to run a php script, not keeping it alive but still keeping the container alive? Since you could restart or recreate a container any time, I would not do that. Run containers that you actually use.

In addition to @bluepuma77 's solution,

  • you could use PHPs built-in webserver, so you don’t have to run httpd if you don’t need it.
  • Or you could just execute sleep inf in the container which runs forever and use docker exec to execute scripts.
  • If the php script has to be a daemon, but not a webapp,
    • you could configure a cron in the container as main process and periodically execute the php script
    • you could use s6-init or supervisor as a systemd alternative.