Stop Synology notification "Docker container stopped unexpectedly"
Asked Answered
S

3

13

I have a container with one Node.js script which is launched with CMD npm start. The script runs, does some work, and exits. The node process exits because no work is pending. The npm start exits successfully. The container then stops.

I run this container on a Synology NAS from a cronjob via docker start xxxx. When it finishes, I get an alert Docker container xxxx stopped unexpectedly from their alert system. docker container ls -a shows its status as Exited (0) 5 hours ago. If I monitor docker events I see the event die with exitCode=0

It seems like I need to signal to the system that the exit is expected by producing a stop event instead of a die event. Is that something I can do in my image or on the docker start command line?

Sterculiaceous answered 18/5, 2020 at 20:42 Comment(0)
S
14

The Synology Docker package will generate the notification Docker container xxxx stopped unexpectedly when the following two conditions are met:

  • The container exits with a die docker event (you can see this happen by monitoring docker events when the container exits). This is any case where the main process in the container exits on its own. The exitCode does not matter.
  • The container is considered "enabled" by the Synology Docker GUI. This information is stored in /var/packages/Docker/etc/container_name.config:
    {
       "enabled" : true,
       "exporting" : false,
       "id" : "dbee87466fb70ea26cd9845fd79af16d793dc64d9453e4eba43430594ab4fa9b",
       "image" : "busybox",
       "is_ddsm" : false,
       "is_package" : false,
       "name" : "musing_cori",
       "shortcut" : {
          "enable_shortcut" : false,
          "enable_status_page" : false,
          "enable_web_page" : false,
          "web_page_url" : ""
       }
    }

How to enable/disable containers with Synology's Docker GUI

Containers are automatically enabled if you start them from the GUI. All of these things will cause the container to become "enabled" and start notifying on exit:

  • Sliding the "toggle switch" in the container view to "on"
  • Using Action start on the container.
  • Opening the container detail panel and clicking "start"

This is probably how your container ended up "enabled" and why it is now notifying whenever it exits. Containers created with docker run -d ... do not start out enabled, and will not initially warn on exit. This is probably why things like docker run -it --rm busybox and other ephemeral containers do not cause notifications.

Containers can be disabled if you stop them while they are running. There appears to be no way to disable a container which is currently stopped. So to disable a container you must start it and then stop it before it exits on its own:

  • Slide the toggle switch on then off as soon as it will let you.
  • Use Action start and then stop as soon as it will let you (this is hard because of the extra click if your container is very shortlived).
  • Open the controller detail panel, click start, and then as soon as "stop" is not grayed out, click "stop".

Check your work by looking at /var/packages/Docker/etc/container_name.config.

Sterculiaceous answered 25/5, 2020 at 18:59 Comment(1)
Thanks for the detailed explanation, but isn't there an actual solution for this? In my case, I'm running the container with the task scheduler. This isn't "if you start them from the GUI", but it still produces this message. Docker containers are often meant to do one thing and then terminate. Is there nothing we can do to get rid of this message in Synology DS?Ceolaceorl
M
6

Another option for stopping/starting the container without the notifications is to do it via the Synology Web API.

To stop a container:
synowebapi --exec api=SYNO.Docker.Container version=1 method=stop name="CONTAINER_NAME"

Then to restart it:
synowebapi --exec api=SYNO.Docker.Container version=1 method=start name="CONTAINER_NAME"

Notes:

  • The commands need to be run as root
  • You will get a warning [Line 255] Not a json value: CONTAINER_NAME but the commands work and give a response message indicating "success" : true

I don't really have any more information on it as I stumbled across it in a reddit post and there's not a lot to back it up, but it's working for me on DSM 7.1.1-42962 and I'm using it in a scheduled task.

Source and referenced links:

Milburt answered 11/11, 2022 at 2:30 Comment(0)
M
0

I'm not familiar with Synology so I'm not sure which component is raising the "alert" you mention, but I guess this is just a warning and not an error, because:

  • an exit status of 0 is very fine from a POSIX perspective;
  • a "die" docker event also seems quite common, e.g. running docker events then docker run --rm -it debian bash -c "echo Hello" yields the same event (while a "kill" event would be more dubious).

So maybe you get one such warning just because Synology assumes a container should be running for a long time?

Anyway, here are a couple of remarks related to your question:

  • Is the image/container you run really ephemeral? (regarding the data the container handles) because if this the case, instead of doing docker start container_name, you might prefer using docker run --rm -i image_name … or docker run --rm -d -i image_name …. (In this case thanks to --rm, the container removal will be automatically triggered when the container stops.)
  • Even if the setup you mention sounds quite reasonable for a cron job (namely, the fact that your container stops early and automatically), you might be interested in this other SO answer that gives further details on how to catch the signals raised by docker stop etc.
Metaphor answered 18/5, 2020 at 21:13 Comment(2)
As it happens (see my self-answer), this problem can be solved on Synology using ephemeral containers because they do not start out as "enabled" and so they don't cause notifications. What eventually happens to longer-lived containers is that someone ends up starting them from the GUI one time which causes the GUI to start notifying.Sterculiaceous
@JenJackson OK that makes sense! thanks for following-up.Metaphor

© 2022 - 2024 — McMap. All rights reserved.