Reduce the size of custom Docker image – How to keep it small?
Custom Docker Images are a core component for Docker, You can use Dockerfiles to create custom Docker images which helps you create custom Docker containers
While creating custom Docker images, one of the main pain points is the size of the Docker image. If you don’t follow the right steps while writing Dockerfile, you may end up with a very large size of the docker image, which in the end results in a high time of container creation, which is not necessary.
In this article, we will show you steps which you should take care of while creating a custom Docker image, which helps you keep it small, and if you already have Dockerfile created, how can you reduce the size of the custom docker image
1. Use a smaller base image
- While creating Dockerfile, make sure you are using a minimal version of the base image
- Base images are mostly built upon OS distro, Always choose alpine version if possible.
- The alpine version of OS is based on
busyboxand is very lightweight with minimal dependencies installed in it, you can install the required dependencies in the next steps
- If your code has OS-related dependencies, only then use specific base image
example: nodejs official image with alpine and stretch version has below size difference
node:14-stretch - 943MB node:14-alpine - 116MB
2. Don’t add unnecessary files and folders
- While using
COPYcommand, only mention required file, don’t use (.) until you want to add everything
- Make use of
.dockerignorefile when necessary, it will ignore files and folders written in this file
3. Use multi-stage dockerfile
- Docker has a feature called multi-stage docker file
- Multi-stage docker file has multiple stages starting with
FROMcommand, using which you can only copy required packages or files generated from the previous stage and use it in the next stage
- Read this article to understand how to create multi-stage dockerfile
4. Use Docker Caching functionality
- Docker image has in-built caching capabilities.
- Docker image caches output of every line as a cache stage and use it every time new docker image is built
- Example, If we talk about NodeJS project, below is sample dockerfile
FROM node:14-alpine WORKDIR /var/NodejsDemo COPY package.json . RUN npm install ADD . . EXPOSE 3000 CMD [ "node", "index.js" ]
Here, While working with NodeJS,
package.json file doesn’t change daily, while our code changes can be more frequent
In this case, whenever you re-build your dockerfile, steps till 4th command will not be performed again, which can save time.
If you directly use
ADD . . at step 3,
npm install run command will execute everytime, resulting in more build creation time
5. Don’t merge multiple functionalities into one
- Suppose you need to run LAMP stack using Docker Images,
- Don’t merge Apache, PHP, MySQL all in one docker image
- Use different image for all the above part of a LAMP stack, and make them communicate with each other
6. Always use official images as a base image
- while creating custom docker images from Dockerfile, always use official docker image as a base image.
- By doing this, you are securing your container and you can upgrade images as and when new releases have come up
- You can trust official images while creating new docker images for the production environment
7. Avoid unnecessary layers
- Dockerfile treats every new step as a new layer for docker image
- If you need to install some packages in your dockerfile, always use one line to install those packages
- For example, the below-mentioned command will install
zipboth in one layer only
RUN apt-get update && apt-get install -y \ git \ zip \
Let us know in the comment section, your views on the same.
Share it if you find it useful
22nd January 2022