I've noticed with many Debian/ubuntu based docker images, you have to do an apt-get update before you install any packages. The cache is completely clear. I feel like I should do the same myself after I do my RUN apt-update && apt-get install -y whatever. I can't figure out how to do that. Is there a command, or do I just rm -rf q folder?
- 827
-
2Maybe you should kill the urge to want to remove the apt package index? There is no need to do so. – Rinzwind Jun 29 '18 at 18:16
5 Answers
When you're building a docker image you try to keep it the smallest possible.
So who builds an image installing a package removes the cache to keep il small. The consequence is that you must run apt-get update if you want to install any package.
And as each docker command executed produces a layer, and its content will stay there "forever", once you finish to install the package you want to remove the cache too.
The two ways to do that are either one of the following:
rm -rf /var/cache/apt/archives /var/lib/apt/lists/*.
OR
apt-get clean
clears out the local repository of retrieved package files. It removes everything but the lock file from /var/cache/apt/archives/ and /var/cache/apt/archives/partial/.
(source: man page)
- 794
-
AFAICT
apt-get cleandoes not remove/var/lib/apt/lists, at least on theubuntu:22.04Docker image. – rhaps0dy Oct 07 '24 at 19:30 -
Option 1 is the cleanest. If you have
RUN apt-get update && apt-get install -y --no-install-recommends wget && rm -rf /var/cache/apt/archives /var/lib/apt/lists/*in your Dockerfile, then the difference in the layer before and after this is run will just be wget, and the layers will be non-the-wiser about any apt cache or lists.apt-get cleanwill leave a delta behind in the layers in regards to apt's cache. – David Baucum Apr 03 '25 at 18:06
rm -rf /var/lib/apt/lists/*
It's in the Docker best practices:
when you clean up the apt cache by removing
/var/lib/apt/listsit reduces the image size, since the apt cache is not stored in a layer.
/var/cache/apt/archives doesn't have much data in it. Compare the du between those 2 dirs.
- 2,772
- 32
- 17
As said in David's answer , you usually apt-get clean or rm -rf /var/cache/apt/archives.
This removes the package cache, which, according to the manual, contains downloaded packages themselves.
However, according to Docker's best practices, apt-get clean is done for you on any official Ubuntu/Debian image.
The only thing left to clean in this case is the repository index cache : You would only have to
rm -rf /var/cache/apt/lists.
- 101
Had a try with docker. apt-get clean still keep something(e.g. the index files). So, IMO, it is better to rm -rf /var/cache/apt/archives /var/lib/apt/lists to get a smaller image.
- 61
-
image size went down by 40MB when removing the dirs directly vs. running
apt-get clean– ams Dec 30 '22 at 15:37
While you can delete the apt cache to free up space as noted in other answers, the downside of this is that any time that step needs to be rebuilt, it needs to rebuild the cache again. You can get the best of both worlds if you are using Buildkit (for example if you are using Docker buildx or if you are using the default Docker builder in Docker version 23+) by using cache mounts. These come in handy not just for the apt cache, but caches for other package managers (ex: pip, gem, go, npm), or even compiler caches (ex: ccache).
For example:
# syntax=docker/dockerfile:1
RUN --mount=type=cache,target=/var/cache/apt
--mount=type=cache,target=/var/lib/apt
apt-get update && apt-get install ...
Note that there is no need to rm the contents of the apt cache, because it will not end up in the final image. But it will be persisted on the build system for the next time that you build.
Note: Additionally, you may want to consider segregating these caches using the id and sharing cache mount options, in case you are building for multiple environments that both use apt (ex: Debian vs Ubuntu, or different versions of those).
Lastly, any time you are having issues with caching in Docker builds, remember that you can both disable caching for specific builds with the --no-cache flag or --no-cache-filter flag. Also you can clean up the build cache in your system with docker buildx prune
- 141
- 3