Hey everyone! I have a quick question about my Dockerfile. Should I include these lines to update my packages? If not, could you explain why?
RUN apt update && apt upgrade -y
RUN apt clean && apt autopurge -y
Thanks for your input!
2 Answers
You definitely should if you want to have newer versions of system packages than what your base image provides. Just a heads up, if you're working with a Debian/Ubuntu image, you use 'apt', whereas for Alpine-based images, you'd use 'apk'. Also, it's better to combine those commands into a single RUN command, separated by '&&', to keep your image smaller by avoiding extra cache layers.
Doesn't Docker cache those RUN commands after they're executed? So even if new packages are available later, that command would just be pulled from the cache, right? You'd actually need to update the base image itself to get newer dependencies instead.
But I've seen advice suggesting that you shouldn't upgrade packages in the Dockerfile. If you rebuild your image, you lose the previous state of the system, which might cause some issues.