Skip to content

Docker - completely delete /var/lib/apt/lists when creating image #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

liamjones
Copy link

When ansible.builtin.apt is run with a cache_valid_time it will not do an apt update before attempting to install a package if the cache was recently updated.

It does this by first checking to see if /var/lib/apt/periodic/update-success-stamp exists and, if so, uses its last modified time to compare against now - cache_valid_time. I believe this file is only created if update-notifier-common is installed and ran a periodic update check so isn't directly relevant here.

However, if the above file doesn't exist, the ansible apt task then falls back to looking at the last modified time of /var/lib/apt/lists to work out if the cache has been updated recently. See https://github.com/ansible/ansible/blob/0aa8afbaf4739510a96c9727237792a95c8855c3/lib/ansible/modules/apt.py#L1174

If the docker driver builds a container and then molecule runs an apt task to install a package with a cache_valid_time set, it'll error out stating the package isn't available because it believes the cache is up to date, but it has been removed.

By deleting the whole lists directory, the task will, the first time, realise it needs to run an update first (and subsequent tasks will be able to make use of the recently retrieved cache for speed).

I've checked, and removing the whole directory seems fine; apt recreates it when you next run apt update.

When `ansible.builtin.apt` is run with a `cache_valid_time` it will not do an `apt update` before attempting to install a package if the cache was recently updated.

It does this by first checking to see if `/var/lib/apt/periodic/update-success-stamp` exists and, if so, uses its last modified time to compare against `now - cache_valid_time`. I believe this file is only created if `update-notifier-common` is installed and ran a periodic update check so isn't directly relevant here.

However, if the above file doesn't exist, the ansible apt task then falls back to looking at the last modified time of `/var/lib/apt/lists` to work out if the cache has been updated recently. See https://github.com/ansible/ansible/blob/0aa8afbaf4739510a96c9727237792a95c8855c3/lib/ansible/modules/apt.py#L1174

If the docker driver builds a container and then molecule runs an apt task to install a package with a `cache_valid_time` set, it'll error out stating the package isn't available because it believes the cache is up to date, but it has been removed.

By deleting the whole lists directory, the task will, the first time, realise it needs to run an update first (and subsequent tasks will be able to make use of the recently retrieved cache for speed).

I've checked, and removing the whole directory seems fine; apt recreates it when you next run apt update.
Copy link

Label error. Requires exactly 1 of: bug, enhancement, major, minor, patch, skip-changelog. Found:

@liamjones liamjones marked this pull request as draft April 29, 2025 10:21
@liamjones
Copy link
Author

Ugh, while this works fine for apt being run directly, ansible's apt task falls over when you entirely remove the lists directory.

Seems to be an old issue, moving this PR to draft for now as a result: ansible/ansible#61176

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant