Charmcraft failing to pack because of `apt update` failure

A common problem that can occur when running the Charmcraft’s pack command is a failure associated with the apt package manager update inside the build instance.

$ charmcraft pack
2021-08-27 14:20:25,342  charmcraft                               ERROR    Failed to update apt cache.
* Command that failed: 'lxc --project charmcraft exec local:charmcraft-foobar-amd64 -- env PATH=/usr/local/sbin:/usr/bin:/sbin:/bin:/snap/bin CHARMCRAFT_MANAGED_MODE=1 apt-get update'
* Command exit code: 100
* Command output: b'Err:1 focal-security InRelease\n  Cannot initiate the connection to (2001:67c:1562::18). ...
* Command standard error output: b'E: Failed to fetch  Cannot initiate the connection to (2001:67c:1360:8001::24). ...

This problem occurs if the LXC container used for the build cannot access the Internet, and specifically, for apt to update its cache.

If you are facing a similar issue, you can confirm this is the case by creating an empty container in LXC (not related to charmcraft) and trying to run the apt update command there.

We suspect the source of this problem is that Docker changed the host’s iptables rules, causing problems for LXD. Please refer to this post in the Linux Containers Forum for more information and ways to solve the situation.