0

I was trying to follow the tutorial here using upStart (Run php script as daemon process), but I ran into some snags when I get the error 'startserver: unrecognized service'

Here is some images to show what I tried

enter image description here

enter image description here

At the bottom of the console is where the errors are. I also show proof that my file is in /etc/init where it should be in the docker container. I logged in using docker exec -it draftandpermit_web_1 bash

Am I missing something?

Other Reference Data:

startserver.conf

# Info
description "Start Server"
author      "Joseph Astrahan"

# Events
start on startup
stop on shutdown

# Automatically respawn
respawn
respawn limit 20 5

# Run the script!
# Note, in this example, if your PHP script returns
# the string "ERROR", the daemon will stop itself.
script
    [ $(exec /usr/bin/php -f /var/www/callcenter/livesite/bin/startwebsocketserver.php) = 'ERROR' ] && ( stop; exit 1; )
end script

I activate it either manually as you saw in the images or using my convenience script

echo "Copying startserver.conf to /etc/init"
docker exec -it draftandpermit_web_1 bash -c "cd /app/docker; cp -f startserver.conf /etc/init/"

echo "Stopping & Starting the WebSocket & HTTP Server"
docker exec -it draftandpermit_web_1 bash -c "service startserver stop"
docker exec -it draftandpermit_web_1 bash -c "service startserver start"

Keep in mind paths are correct for where the file is since I manually went into the container to look at /etc/init as shown in the images.

Community
  • 1
  • 1
Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154
  • 'running as a deamon' and docker is a warning sign: docker needs to be running something, if you run it in the background, your docker will say: well, there's no process, so I can quit. Why do you want to run it as daemon? – Nanne Mar 30 '17 at 15:25
  • I don't want to run docker as a daemon but a process in the background in docker – Joseph Astrahan Mar 30 '17 at 15:26
  • Basically trying to use upstart in a docker container to start my php service – Joseph Astrahan Mar 30 '17 at 15:30
  • I meant: you shouldn't be running background processes in docker. Docker stops if your command exits. you should probably not run it troufh upstart, just do your php command as the docker command? – Nanne Mar 30 '17 at 16:22
  • I can't do this because if I close the terminal window the process stops for the php server – Joseph Astrahan Mar 30 '17 at 17:53
  • I'm basically running a rachet php web socket server – Joseph Astrahan Mar 30 '17 at 17:54
  • I can't really make it any clearer. If you have a docker container, you want it to run one single process. An actual process, not a daemon. That's how its supposed to work, so you should try and figure out how to run your process without the server. I think you should start looking for a different way to run whatever you want to run, maybe ask that as a question? I'm not too confident this can work out. Anyway, good luck :) – Nanne Mar 30 '17 at 20:09
  • I think i get what you are saying now I just didn't realize docker run has an option to ignore output with -d essentially being a background task – Joseph Astrahan Mar 31 '17 at 04:19

1 Answers1

1

Docker is neither an init system nor does it run traditional init systems without some hacks. The first process you run in the Docker container will be PID 1. This could be a supervisor (like supervisord, s6, dumb-init).

However, more generally, you can just run the process you want and handle running in the foreground or background with options to the docker run command. In this example your Docker command (or CMD in the Dockerfile) can just be php -f /var/www/callcenter/livesite/bin/startwebsocketserver.php. Then, run your container with the -d option and it will run in the background. You can attach to it with docker attach or just watch the output with docker logs.

If you actually need to use some sort of process supervisor (such as, when you need to have multiple processes running in the container), then I'd start looking to Docker init options out there. I mentioned a couple and there are more. Upstart won't work inside a container.

Andy Shinn
  • 26,561
  • 8
  • 75
  • 93
  • Currently I am using docker compose with the -d option to start the containers. I then volume in my Code the same way. Are you saying the run command also has a -d option? – Joseph Astrahan Mar 31 '17 at 04:15
  • If so that would work for sure. I didn't even think of that will have to try it – Joseph Astrahan Mar 31 '17 at 04:16
  • If I run with -d how do I stop the server to restart it? – Joseph Astrahan Mar 31 '17 at 04:28
  • Look up `docker stop`, `docker kill`, and `docker rm -f` – Andy Shinn Apr 01 '17 at 16:20
  • Thanks for responding, however, that will kill the entire docker container, what if I only want to kill just the php server running process that is running in the container? – Joseph Astrahan Apr 02 '17 at 06:28
  • If you are running a supervisor as PID 1 in the container then you would have to control the other processes via that supervisor control. If you need to exec a command to do that you can do something like `docker exec -it bash` to get a shell and then execute your supervisor control command. – Andy Shinn Apr 03 '17 at 18:44