How do I remove the apt package index?

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?


Solution 1:

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:

  1. rm -rf /var/cache/apt/archives.

OR

  1. 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)

Solution 2:

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/lists it 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.