In my previous blog post, we learned how to dockerize an application by writing a Dockerfile to create an image. However, the resulting image was very large, which is not ideal for industry-level projects.
In this blog post, we will learn how to reduce the size of your Docker image by using multi-stage builds. This technique will help improve the efficiency and performance of your application.
We will follow the same steps, but we need to change our Dockerfile.
Get Started
Clone a sample git repository using the below command or use your project for the demo:
git clone https://github.com/piyushsachdeva/todoapp-docker.git
cd into the
todoapp-docker
directory.Create a file named Dockerfile using the command below.
Paste the following code into your Dockerfile.
FROM node:18-alpine AS installer WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build FROM nginx:latest AS deployer COPY --from=installer /app/build /usr/share/nginx/html
Explanation of this code:
Stage 1: Installer
Specify Base Image for Installer Stage
FROM node:18-alpine AS installer
This line sets the base image to
node:18-alpine
, which is a lightweight version of Node.js 18. TheAS installer
part names this stageinstaller
.Set Working Directory
WORKDIR /app
This sets the working directory inside the container to
/app
. All subsequent commands will be run within this directory.Copy Package Files
COPY package*.json ./
This copies
package.json
andpackage-lock.json
(if it exists) from your local machine to the working directory (/app
) inside the container.Install Dependencies
RUN npm install
This runs
npm install
to install all the dependencies listed inpackage.json
. These dependencies are installed within the container.Copy Application Code
Copy codeCOPY . .
This copies the rest of your application code from your local machine to the container’s working directory (
/app
).Build the Application
RUN npm run build
This runs the build script defined in your
package.json
, typically creating a production-ready version of your application in abuild
directory.
Stage 2: Deployer
Specify Base Image for Deployer Stage
FROM nginx:latest AS deployer
This sets the base image to
nginx:latest
, which is the latest version of the Nginx web server. TheAS deployer
part names this stagedeployer
.Copy Built Application to Nginx
COPY --from=installer /app/build /usr/share/nginx/html
This copies the build artifacts from the
installer
stage (specifically from/app/build
) into the Nginx container’s web root directory (/usr/share/nginx/html
). The--from=installer
flag specifies that the source files should be taken from theinstaller
stage.
We write our Dockerfile in two stages-
First Stage (
installer
):Uses a Node.js image to install dependencies and build the application.
The build artifacts are created in the
/app/build
directory.
Second Stage (
deployer
):Uses an Nginx image to serve the built application.
Copies the build artifacts from the
installer
stage into Nginx’s web root.
Now build the Docker image with the following command:
docker build -t imageName:tagname .
Run the container by following command
docker run -dp 3000:3000 imageName:tagname
To enter in the container use this command
docker exec -it containername sh or docker exec -it containerid sh
Resources I used ->