How to pass a command to a container at startup, or get a program to auto-run in the container at startup?

    • OMV 4.x

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • How to pass a command to a container at startup, or get a program to auto-run in the container at startup?

      This is a nagging little thing I can't seem to get sorted out, but per the last post in my topic here:

      Here's a working PHP 7.x and Apache setup for Docker

      I have a PHP/Apache container in which cron in installed, but for some reason cron is not auto-starting on container startup. It works great other than this one issue.

      Can someone tell me how to either:

      1) build the container such that cron auto-starts, or

      2) at startup of the built container, pass this command to it as (or preferably 10-30 seconds after) it starts: "service cron restart"

      Thanks in advance.
    • Thanks for trying to help, but I entered that command within the container running PHP/Apache in Docker, and it says systemctl is not a valid command.


      docker exec -it webserver bash ("webserver" is my container's name, and this command gets me into the container via shell)

      systemctl enable crond.service

      bash: systemctl: command not found

      In that container, rc.d and rc.local don't exist, though there are /etc/rc0.d, rc1.d..... rc6.d and rcS.d folders with what appears to be init-type symlinked commands in them.

      One odd thing is that in the container, if I enter "service cron restart" it first says the process it tries to kill doesn't exist, then it starts cron normally:

      ervice cron restart
      [....] Restarting periodic command scheduler: cron[....] Stopping periodic command scheduler: cron/sbin/start-stop-daemon: warning: failed to kill 77: No such process
      . ok
      [ ok ] Starting periodic command scheduler: cron.


      Has me baffled.
    • systemd doesn't exist in the docker. The only program that is run is apache. You should have cron technically running in its own container.
      omv 4.1.22 arrakis | 64 bit | 4.15 proxmox kernel | omvextrasorg 4.1.15
      omv-extras.org plugins source code and issue tracker - github

      Please read this before posting a question and this and this for docker questions.
      Please don't PM for support... Too many PMs!
    • Hmm... well I have cron installed and active in the container I created, "my-php-custom:latest" and with which you greatly helped, thanks again. ( Here's a working PHP 7.x and Apache setup for Docker )

      I just need some way to run this command:

      service cron restart

      at startup of that container, but see no way to do that.

      I can SSH into that container with "docker exec -it webserver bash" and run the command, so I know that command works. Is there no way to "automate" that within the container?

      If not, how would I run cron in its own container? Searching available docker images shows a few results but I have yet to find one with any meaningful documentation.

      (And since I already have cron working in my container, I'm fearful of interference by a new instance of it.)
    • TheSidewinder wrote:

      service cron restart

      at startup of that container, but see no way to do that.
      That is what I am trying to say... There are no services. The container is executing apache and nothing else. Dockers are supposed to only have one parent PID. So, you should have another container that just runs cron.

      TheSidewinder wrote:

      If not, how would I run cron in its own container? Searching available docker images shows a few results but I have yet to find one with any meaningful documentation.
      Create your own Dockerfile like the other thread that uses something like the Ubuntu starting image, installs cron, and executes cron at the end.

      What is cron doing? Maybe I can help you go a different direction.
      omv 4.1.22 arrakis | 64 bit | 4.15 proxmox kernel | omvextrasorg 4.1.15
      omv-extras.org plugins source code and issue tracker - github

      Please read this before posting a question and this and this for docker questions.
      Please don't PM for support... Too many PMs!
    • It's running a command every minute which my php software needs to maintain its database and the information within it.

      Source Code

      1. /usr/local/bin/php -d memory_limit=-1 -d max_execution_time=0 /var/www/html/applications/core/interface/task/task.php ccxxxxxxxxxxxxxxxxxxxxxxxxxxxxaf
      (The task ID at the end of the line above is redacted for security.)


      And that's what I did do. Check the most-recent Dockerfile I posted in that topic.

      Cron is installed, but for some reason does not appear to be actually running. Or at least not running correctly.

      When I issue the restart command, note what it says on the first line.

      service cron restart
      [....] Restarting periodic command scheduler: cron[....] Stopping periodic command scheduler: cron/sbin/start-stop-daemon: warning: failed to kill 77: No such process
      . ok
      [ ok ] Starting periodic command scheduler: cron.

      If I run "top" before I perform the restart, cron is not listed. After I issue the command, top does show it.

      That's why I just want to issue the service restart command and be done with it, lol.
    • TheSidewinder wrote:

      Cron is installed, but for some reason does not appear to be actually running. Or at least not running correctly.
      That is because the package assumes you have an init system running that dockers don't have. You need the docker file to actually do what the init script or unit file does - execute the cron binary.

      TheSidewinder wrote:

      When I issue the restart command, note what it says on the first line.

      service cron restart
      [....] Restarting periodic command scheduler: cron[....] Stopping periodic command scheduler: cron/sbin/start-stop-daemon: warning: failed to kill 77: No such process
      . ok
      [ ok ] Starting periodic command scheduler: cron.

      If I run "top" before I perform the restart, cron is not listed. After I issue the command, top does show it.

      That's why I just want to issue the service restart command and be done with it, lol.
      That is never going to work. Instead of service cron start, you just want to execute /usr/sbin/cron -f

      I found this for creatng a cron docker - stackoverflow.com/questions/37…inside-a-docker-container
      omv 4.1.22 arrakis | 64 bit | 4.15 proxmox kernel | omvextrasorg 4.1.15
      omv-extras.org plugins source code and issue tracker - github

      Please read this before posting a question and this and this for docker questions.
      Please don't PM for support... Too many PMs!
    • I must be confused about how containers interact with one another.... pardon some rambling with my observations this morning.

      First, my container is named "webserver" and contains the items/programs in that topic I linked in my first post.

      1) If I use /usr/sbin/cron -f from the "webserver" container shell, that results in the command prompt never coming back:



      until I use CTRL-C. However, cron is running at that point; it shows up in "top" whereas before it did not.

      But running /usr/sbin/cron (without the -f) does work, the command prompt does comes back, and it does start cron (which shows in "top" whereas it did not before).

      What is the "-f" parameter doing?

      2) I guess I'm not seeing the difference between my command "service start (or restart) cron" and your suggested command "/usr/sbin/cron" (with or without the -f) with respect to getting either command to run at startup. They both produce the same result; cron is then running after either command is executed.

      I already have the cron job entered for the user "www-data" (the account which my software uses to run), the job is persistent between reboots (crontab -l -u www-data shows it in place), and once I get cron started, the cron job works as required.

      3) Can what I want to do be accomplished simply by adding a line to the Dockerfile I made previously, perhaps right before the end of the file, and recreating the container from the image? Something like this, using your command and formatted properly as "&& /usr/sbin/cron \"?

    • TheSidewinder wrote:

      What is the "-f" parameter doing?
      That is the flag to run in the foreground. If it is was the only thing running in a docker, it would be required to keep the container running. Since you are trying it on exec, you should leave it off so it starts in daemon or background mode.

      TheSidewinder wrote:

      I guess I'm not seeing the difference between my command "service start (or restart) cron" and your suggested command "/usr/sbin/cron" (with or without the -f) with respect to getting either command to run at startup. They both produce the same result; cron is then running after either command is executed.

      I already have the cron job entered for the user "www-data" (the account which my software uses to run), the job is persistent between reboots (crontab -l -u www-data shows it in place), and once I get cron started, the cron job works as required.
      It was more systemctl that wouldn't work since it has a daemon that wouldn't be running. service is an older command that must still work (I don't use it anymore). Executing /usr/sbin/cron is what you would use if cron was the only thing running in the container which is what a container is supposed to do.

      TheSidewinder wrote:

      Can what I want to do be accomplished simply by adding a line to the Dockerfile I made previously, perhaps right before the end of the file, and recreating the container from the image? Something like this, using your command and formatted properly as "&& /usr/sbin/cron \"?
      Yes but you are getting away from the concept of docker.
      omv 4.1.22 arrakis | 64 bit | 4.15 proxmox kernel | omvextrasorg 4.1.15
      omv-extras.org plugins source code and issue tracker - github

      Please read this before posting a question and this and this for docker questions.
      Please don't PM for support... Too many PMs!
    • ryecoaaron wrote:

      Yes but you are getting away from the concept of docker.
      I tried adding that line to Dockerfile as my image showed, and while it didn't produce any errors during the build, cron was not running when the container started. This was the same result when I earlier tried adding the line "&& service cron restart \" to the Dockerfile.

      At this point I think I'll try and muddle my way through creating a cron docker from the example you posted.

      And thank you for trying to help, I do appreciate it.
    • Users Online 1

      1 Guest