Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove APT cache (for Dockerfile)
apt-get clean autoclean
apt-get autoremove --yes
rm -rf /var/lib/{apt,dpkg,cache,log}/
@pklapperich

This comment has been minimized.

Copy link

@pklapperich pklapperich commented Jul 9, 2019

Note that doing this makes it impossible to use apt on any images that inherit from this image. Docker best practices recommend this style:

RUN apt-get update && apt-get install -y \
    aufs-tools \
    automake \
    build-essential \
    curl \
    all\
    your\
    other\
    packages\
 && rm -rf /var/lib/apt/lists/*```
@redthor

This comment has been minimized.

Copy link

@redthor redthor commented Sep 22, 2020

See rocker-org/rocker#35 (comment)

in my test adding

apt-get clean autoclean
apt-get autoremove --yes
rm -rf /var/lib/{apt,dpkg,cache,log}/

only saved 200 bytes to the image size. So it seems we don't need this in standard docker images.

@JimmyChatz

This comment has been minimized.

Copy link

@JimmyChatz JimmyChatz commented Nov 29, 2020

Please, never do this. See https://askubuntu.com/questions/383339/how-to-recover-deleted-dpkg-directory . I did it on the first day I installed my distribution, because after the updates I wanted to clear the cache. I don't know why I listened to this.

@redthor

This comment has been minimized.

Copy link

@redthor redthor commented Nov 29, 2020

The gist is titled (for Dockerfile) - it was meant for docker images. Not your local OS.

@Karasiq

This comment has been minimized.

Copy link

@Karasiq Karasiq commented Jan 12, 2021

Please, never do this. See https://askubuntu.com/questions/383339/how-to-recover-deleted-dpkg-directory . I did it on the first day I installed my distribution, because after the updates I wanted to clear the cache. I don't know why I listened to this.

Lol

@gwpantazes

This comment has been minimized.

Copy link

@gwpantazes gwpantazes commented Jan 22, 2021

@redthor, I think @JimmyChatz point still stands for docker though; if you rm -rf /var/lib/{apt,dpkg,cache,log}/ and make it impossible to use apt after that point, you are preventing anyone from using your image as a base image and making modifications with apt.

If you making a conscious decision to do that in exchange for 200 bytes and provide documentation warning people about this, it's probably fine. I, however, think that 200 bytes vs ruining the image's ability to be a base image is a bad tradeoff.


Also, apt-get clean is the superset of apt-get autoclean, so you only need to run clean. As per the docs (emphasis mine): https://linux.die.net/man/8/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/.

autoclean
Like clean, autoclean clears out the local repository of retrieved package files. The difference is that it only removes package files that can no longer be downloaded, and are largely useless. This allows a cache to be maintained over a long period of time without it growing out of control. The configuration option APT::Clean-Installed will prevent installed packages from being erased if it is set to off.

@redthor

This comment has been minimized.

Copy link

@redthor redthor commented Jan 24, 2021

@gwpantazes my point was that we do not need this it because it is already included in rocker-org/rocker#35 (comment)

My test was just to show that it a waste of time - 200 bytes is similar to a rounding error - inconsequential.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment