Improving the image build

Improving the image build


3 min read


Peace to you all , Here is the Link to my previous blogs, Where I have posted about Docker, Containers and DevOps.

This is Day 9 of documenting my DevOps journey .

This blog focuses on Improving the docker image build.

Getting Started

As we know , building images follows a layered approach , each instruction in a dockerfile is built as a layer . The result artifact of the previous layer will be used in the next layer.

Let us take the last example where we containerized an angular app .

The Dockerfile of the angular app can be found below.

FROM node:lts-alpine as build

WORKDIR /usr/local/app

COPY ./ /usr/local/app/

RUN npm install

RUN npm run build

FROM nginx

COPY --from=build /usr/local/app/dist/ang /usr/share/nginx/html


Breaking the code

We can say, since it is following a layered approach ,Each instruction will take specific time to complete.

Each and every time an image is built with this dockerfile , node modules are copied to the container . This is not the standard way to do it .

Copying the node modules is not necessary since npm install , installs node modules, in can be done through that command and there will be no need to copy the node modules from the host.

.dockerignore is the file which provides solution for this problem , As like gitignore , this can be set according to our requirements.


Before starting the build , docker looks for .dockerignore files . The name of the files mentioned on dockerignore will be ignored from the docker build.

For example ,

We don’t want the node modules of the angular app and so we can reference the folder in the .dockerignore file like below.


Here the ** tells the docker to take all the subfolders of the folder mentioned.

This way , while building , the node_modules won’t be copied and the process can be simplified.

But that doesn’t stop there.

Best practices on writing .dockerignore

Package.json helps in installing the node modules , So we should not add it in dockerignore.

We can just look at the project and check what we need, to add to the project and what should not be added .

Dockerfile and dockerignore as we can say , are not necessary for the app to run , it is just for creating an image and it will be taken automatically from the current folder while building .

So we don’t necessarily need to copy those files to the container and so we can pass those names in dockerignore along with node_modules.

Auto-generated dockerignore

There are many extensions , tools which auto-generate dockerignore , but at the core , we must also know what should be on the container and what does not need to be .

There are certain standards, where if it is an angular app, we can definitely ignore some files for sure and the rest as we need can be added one by one. App-specific dockerignore files are also available for use and can add more in the list if required.

Now building the image with dockerignore , Optimizes and improves the build.

Dockerignore is a great way for leaving out unwanted files and improve the image build.

What have we done

We left those files which are not required on building an image and this way improving the image build.

See you Tomorrow

Okay . So hold on, stay with me as we get to know docker more and DevOps even more.

Okay Then , Will get back Tomorrow with more!

Peace be upon you!

Did you find this article valuable?

Support Shajith by becoming a sponsor. Any amount is appreciated!