When calling unattened apt update/install, sometimes error

When executing a container unattened apt update/install (i.e. apt -y ...) , sometimes exceptions or errors occur. This behavior happens both from CLI and from pylxd.

However when called as attended, e.g., no -y, the problem has not been observed. Of course, attended is only an option with CLI, not with pylxd.

I’ve got a workable pylxd script which just repeats the executable call (up to a specified number of times) until it works - and that seems to work. A log excerpt:

----------------- 2019-03-22 02:03:53.494656
ERROR: apt-get -y update: Container is not running
----------------- 2019-03-22 02:03:55.447782
SUCCESS:(n=1,p=0): apt-get -y update: exit_code: 0, stdout: Err:1 http://deb.debian.org/debian stretch InRelease
, stderr: W: Failed to fetch http://deb.debian.org/debian/dists/stretch/InRelease  Temporary failure resolving 'deb.debian.org'
W: Failed to fetch http://security.debian.org/debian-security/dists/stretch/updates/InRelease  Temporary failure resolving 'security.debian.org'
W: Some index files failed to download. They have been ignored, or old ones used instead.

In the above log excerpt, apt -y update fails with exception on the first call with an incorrect reply that the container was not running. Then, after a 100ms sleep in a second call the apt -y update execution returns true, but the sydout report shows it actually failed to make the connection from the container to debian. (Calling in attended mode from the CLI this failure was ever observed).

Then:

----------------- 2019-03-22 02:03:56.062899
ERROR: apt-get -y install apt-utils: exit_code: 100, stdout: Reading package lists..., stderr: E
----------------- 2019-03-22 02:03:56.751511
ERROR: apt-get -y install apt-utils: exit_code: 100, stdout: Reading package lists..., stderr: E
----------------- 2019-03-22 02:03:57.423380
ERROR: apt-get -y install apt-utils: exit_code: 100, stdout: Reading package lists..., stderr: E
----------------- 2019-03-22 02:04:03.692604
SUCCESS:(n=3,p=0): apt-get -y install apt-utils: exit_code: 0, stdout: Reading package lists..., stderr: debconf: delaying package configuration, since apt-utils is not installed

it reports 3 failures before finally succeeding.

This is just a typical log - it is not always the same.

My uneducated guess is that in CLI interactive mode there is a lot of pinging going on behind the curtain, and this keeps all the participating agents on their toes and so things work smoothly.