7 Tips to Optimize Your Docker Images
#1 Design Container Images for a Single Job
By focusing a container image for single job it will be focused and lightweight. Making a container image multi-purpose will bloat the size
#2 Install Required Packages Only
Install the bare minimum packages for the single job the image will be used for
#3 Reduce Number of Layers
Each RUN command creates a new layer. Combining the layers can reduce the image size. So, smart combinations of commands can lead to smaller images.
#4 Avoid Storing Application data
Storing application data in the container will balloon up your images. For production environments, always use the volume feature to keep the container separate from the data
#5 Avoid Using :latest
Using specific tags can ensure that you know the exact image being used from the Docker registry and don’t have any surprises if the :latest changes
#6 Sort Multi-line Arguments
Whenever you have a multi-line argument, sort the arguments alphanumerically to improve maintenance of the code. Haphazard arguments can lead to duplications. They are also harder to update
#7 Use .dockerignore
Use the .dockerignore to exclude unnecessary files and folders that complicate the build process and bloat the image