# Container lifecycle

**URL:** <https://forums.balena.io/t/container-lifecycle/5602>\
**Category:** Product support\
**Created:** [March 12, 2019, 10:19pm UTC](https://forums.balena.io/t/container-lifecycle/5602 "2019-03-12T22:19:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 12, 2019, 10:19pm UTC](https://forums.balena.io/t/container-lifecycle/5602/1 "2019-03-12T22:19:05Z")

</div>

Are there any differences between the docker container lifecycle, for example the flow chart at

> **[Lifecycle of Docker Container](https://medium.com/@nagarwal/lifecycle-of-docker-container-d2da9f85959)**
>
> Though the above pic is self explanatory, we’ll just discuss the basics / CLI commands for working with docker containers.

and the balenaCloud device lifecycle of a service?

---

<div class="post-metadata">

**Author:** ![chrisys](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/chrisys/32/1731_2.png) [@chrisys](https://forums.balena.io/u/chrisys)\
**Post date:** [March 12, 2019, 11:33pm UTC](https://forums.balena.io/t/container-lifecycle/5602/13 "2019-03-12T23:33:41Z")

</div>

@jason10 there’s not a quick answer to this, but the main difference is that the actions and changes are instigated via Docker API requests rather than CLI actions, although the underlying actions are the same. The lifecycle is a subset of the diagram from the Medium post you linked as we do not (currently) support all of the same actions. For example, `pause` and `unpause` of containers is not currently supported.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 12, 2019, 11:39pm UTC](https://forums.balena.io/t/container-lifecycle/5602/16 "2019-03-12T23:39:53Z")

</div>

Thanks, and the diagram does not have any healthcheck arcs.

I was confused earlier because the [balena.io](http://balena.io) dashboard wasn’t showing my container as restarting every 10 seconds as that is too fast for the dashboard. But using `balena ps` or opening a Terminal and watching it get disconnected was sufficient to see that the container was being restarted.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 13, 2019, 12:08am UTC](https://forums.balena.io/t/container-lifecycle/5602/18 "2019-03-13T00:08:52Z")

</div>

@chrisys But when the CMD of the dockerfile.template for a service exits, should we see the container restart?

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [March 13, 2019, 12:10am UTC](https://forums.balena.io/t/container-lifecycle/5602/20 "2019-03-13T00:10:48Z")

</div>

@jason10 yes, the container would exit and, unless you’ve changed the restart policy (we set “always” as default) it will be restarted by balenaEngine.  
Unless you’re running something like systemd inside the container, which can prevent the container from exiting when the CMD process exits.

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [March 13, 2019, 12:13am UTC](https://forums.balena.io/t/container-lifecycle/5602/22 "2019-03-13T00:13:03Z")

</div>

I think the diagram might not have healthcheck arcs because in regular docker, a failed healthcheck triggers no actions other than marking the container as unhealthy.

When a healthcheck fails on a container being run by balenaEngine and a container becomes unhealthy, we restart it. I guess that could also count as a difference with that diagram.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 13, 2019, 12:13am UTC](https://forums.balena.io/t/container-lifecycle/5602/25 "2019-03-13T00:13:38Z")

</div>

@pcarranzav by systemd, do you mean a privileged container? I have initsystem on and establish a udev rule.  
[https://dashboard.balena-cloud.com/devices/ec60d19496afc68878ebc9afbce46205/summary](https://dashboard.balena-cloud.com/devices/ec60d19496afc68878ebc9afbce46205/summary)

Answering my own question:

```
root@orbitty-tx2-ec60d19:/usr/src/app# ps ax
  PID TTY STAT TIME COMMAND
    1 ? Ss 0:00 /sbin/init quiet systemd.show_status=0
   38 ? S 0:00 sleep infinity
   52 ? Ss 0:00 /lib/systemd/systemd-journald
   54 ? Ss 0:00 /lib/systemd/systemd-udevd
  129 ? Ss 0:00 /usr/sbin/cron -f
  134 ? Ss 0:00 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
  146 ? Ss 0:00 avahi-daemon: running [orbitty-tx2-ec60d19.local]
  147 ? Ss 0:00 /usr/sbin/sshd -D
  149 ? S 0:00 avahi-daemon: chroot helper
  272 pts/1 Ss 0:00 /bin/bash
  291 pts/1 R+ 0:00 ps ax
```

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [March 13, 2019, 12:15am UTC](https://forums.balena.io/t/container-lifecycle/5602/26 "2019-03-13T00:15:04Z")

</div>

By systemd I mean “INITSYSTEM = on”. This starts a systemd init process that keeps the container running.

(A container can be privileged but not have systemd)

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 13, 2019, 12:15am UTC](https://forums.balena.io/t/container-lifecycle/5602/28 "2019-03-13T00:15:25Z")

</div>

Since I see systemd running, I guess I should use an api call to restart the container?

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [March 13, 2019, 12:16am UTC](https://forums.balena.io/t/container-lifecycle/5602/29 "2019-03-13T00:16:26Z")

</div>

Yes, you can use an API call to the supervisor API, or you can make a healthcheck command that fails if your main process isn’t running, which will eventually also cause a container restart.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 13, 2019, 12:18am UTC](https://forums.balena.io/t/container-lifecycle/5602/31 "2019-03-13T00:18:09Z")

</div>

ah, like “pgrep mycommand”?!  
I tried a health check command based on the logging of my main process but it isn’t working the way I want it to yet.

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [March 13, 2019, 12:33am UTC](https://forums.balena.io/t/container-lifecycle/5602/32 "2019-03-13T00:33:05Z")

</div>

Yes, something like that could work… depending on what your command does, you could do something more comprehensive.

For instance, if your command exposes an API, you could add an API endpoint that responds whether your app is healthy, like we do in the supervisor container: [https://github.com/balena-io/balena-supervisor/blob/master/Dockerfile#L129](https://github.com/balena-io/balena-supervisor/blob/master/Dockerfile#L129)

Or you could make your command write to a file at regular intervals, and check the file modification time in the healthcheck command, much like a watchdog.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 13, 2019, 12:42am UTC](https://forums.balena.io/t/container-lifecycle/5602/34 "2019-03-13T00:42:01Z")

</div>

I tried checking for modifications to the log file, but either the boost::logger is buffering or stat isn’t getting the right time, etc.

I’ll try pgrep. The service connects to a USB camera and send images to another service. So health is not something the camera service provides.

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 13, 2019, 1:08am UTC](https://forums.balena.io/t/container-lifecycle/5602/35 "2019-03-13T01:08:59Z")

</div>

@pcarranzav is there a way to get output from the healthcheck?

Does the healthcheck have access to files on the shared-volumes specified in the docker-compose?

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [March 13, 2019, 4:40pm UTC](https://forums.balena.io/t/container-lifecycle/5602/36 "2019-03-13T16:40:00Z")

</div>

Hi Jason, Not sure I fully follow the question. What output from the healthcheck? Do you mean you want to be able to query if some container is unhealthy? As per the documentation on healthcheck ([https://docs.docker.com/engine/reference/builder/#healthcheck](https://docs.docker.com/engine/reference/builder/#healthcheck) ) , the healthcheck test command can only have the following outputs:

0: success - the container is healthy and ready for use  
1: unhealthy - the container is not working correctly  
2: reserved - do not use this exit code

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 13, 2019, 4:49pm UTC](https://forums.balena.io/t/container-lifecycle/5602/37 "2019-03-13T16:49:02Z")

</div>

I meant any stdout / stderr from the command or script to help with checking the healthcheck. In the end I copied all output to a log file on /data so that I could see if I had missed anything in my script.

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [March 13, 2019, 5:27pm UTC](https://forums.balena.io/t/container-lifecycle/5602/38 "2019-03-13T17:27:22Z")

</div>

Ah okay, that makes sense. I don’t think there is a super easy way to get the healthcheck commands, but logging to a file is probably a good way.

---

<div class="post-metadata">

**Author:** ![pcarranzav](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/pcarranzav/32/94_2.png) [@pcarranzav](https://forums.balena.io/u/pcarranzav)\
**Post date:** [March 13, 2019, 8:42pm UTC](https://forums.balena.io/t/container-lifecycle/5602/40 "2019-03-13T20:42:26Z")

</div>

@jason10 you can try sending the stdout from your healthcheck command to /dev/console; this should send the output to the container’s logs, so they’d be visible in the balenaCloud dashboard. (disclaimer: I haven’t tried this 😛 )

---

<div class="post-metadata">

**Author:** ![jason10](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/jason10/32/1464_2.png) [@jason10](https://forums.balena.io/u/jason10)\
**Post date:** [March 13, 2019, 8:59pm UTC](https://forums.balena.io/t/container-lifecycle/5602/42 "2019-03-13T20:59:50Z")

</div>

I haven’t seen the healthcheck outputs in the dashboard console. I’m using tee to send the output to both stdout and the log file on shared persistent storage. And then opening a shell to another container on the same device to make sure that the healthcheck and the container it is checking are working together.

---

<div class="post-metadata">

**Author:** ![shaunmulligan](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/shaunmulligan/32/34_2.png) [@shaunmulligan](https://forums.balena.io/u/shaunmulligan)\
**Post date:** [March 14, 2019, 7:47pm UTC](https://forums.balena.io/t/container-lifecycle/5602/43 "2019-03-14T19:47:07Z")

</div>

Is that working for you now @jason10 ? Also wonder if you tried Pablo’s suggestion of directing the Healthcheck command output to /dev/console?

[Next page](https://forums.balena.io/t/container-lifecycle/5602.md?page=2)
