How to programmatically control if a docker container exits?

I am running several named docker containers (200+) on my VM Host. I have a script / code manager that should manage containers from the host. I would like to know if there is any event-based mechanism to get notified when a container stops / fails. So that I can restart the stopped container.

One solution I could think of is to periodically docker examine and look at State.Pid or State.Running to confirm the state.

But instead of periodically polling, it would be better if the manager is notified with pid / name when the container fails, so that one specific container can be restarted.

In general, are there ways to programmatically monitor the state of a process from another process that is not a parent?

+8
docker lxc
source share
3 answers

Look at docker events - there is an event for the 'die' container.

There is also an http interface for getting the same information programmatically - see here

You might want to do an online search for docker orchestration - many projects come up to manage multiple containers the way you describe.

+6
source share

If you just want to restart containers, why not use a reload policy ?

 docker run --restart=always IMAGE 
+3
source share

psutil seems to do what you want http://pypi.python.org/pypi/psutil From Python

import psutil psutil.pids () [1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 56, 57, 58, 59, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, 73, 74, 76, 77, 78, 79, 80, 81, 82, 94, 97, 98, 117, 118, 137, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 200, 201, 210, 211, 213, 214, 229, 230, 416, 419, 526, 527, 542, 543, 544, 545, 555, 569, 625, 709, 714, 756, 781, 782, 796, 862, 863, 864, 869, 914, 944, 945, 948, 954, 996, 1052, 1061, 1064, 1067, 1170, 1174, 1179, 1180, 1183, 1234, 1240, 1241, 1245, 1323, 1328, 1340, 1351, 1354, 1390, 1408, 1457, 1507, 1531, 1631, 1662, 1933, 1972, 1981, 1987, 1989, 1993, 2346, 2348, 2413, 2422, 2429, 2442, 2445, 2449, 2451, 2457, 2461, 2471, 2489, 2490, 2491, 2493, 2497, 2501, 2505, 2509, 2513, 2524, 2546, 2549, 2551, 2554, 2563, 2567, 2572, 2573, 2576, 2578, 2586, 2595, 2598, 2624, 2644, 2655 , 2665, 2667, 2687, 2689, 2693, 2699, 2744, 2752, 2785, 2789, 2794, 2798, 2804, 2817, 2820, 2830, 2838, 2856, 2862, 2864, 2886, 2903, 2935, 2972, 2985 , 2986, 3138, 3164, 3211, 3368, 3371, 3557, 4125, 4352, 4443, 4444, 4743, 4818, 4819, 4840, 4841, 4844, 4845, 4866, 4876, 6142, 6363, 6366, 6372, 6378 , 6385, 6391, 6452, 6518, 6524, 6531, 6555, 6558, 6601] p = psutil.Process (2862) p.status () 'Sleeping'

0
source share

All Articles