Images are the basis of the Docker platform and are used to process and maintain Docker containers for project deployment. It is based on instructions that tell the container how to deploy or develop the project. It also contains information about essential project dependencies and installs them through commands.
Docker developers mostly want to reduce the image size because the image size can be huge for running small projects and saving disk space.
This write-up will demonstrate:
How to Create a Docker Image?
To create a new Docker image, users are required to make Dockerfile. Then, utilize it to create a new Docker image. Go through the procedure to see how to create a Docker image.
Step 1: Open Terminal
First, open the “Git Bash” terminal from the Windows Start menu. You can also use your favorite terminal to execute Docker commands:
Step 2: Make New Directory
Next, make a new directory in the “C” drive to create a Docker image:
After creating a directory, navigate to it using the “cd” command:
Step 3: Create Dockerfile
Create and open the Dockerfile in the nano text editor with the help of the mentioned command:
Paste the following instructions in “Dockerfile”. These instructions will execute a simple “golang” program on the web server:
WORKDIR /go/src/app
COPY main.go .
RUN go build -o webserver .
CMD ["./webserver"]
After that, press “CTRL+O” to save the file and “CTRL+X” to exit the editor:
Step 4: Build Docker Image
Now, build the new Docker image using the “docker build” command. The “-t” option is used to create an image by name:
Step 5: View Docker Image Size
After building the Docker image, check out the image size through the given command:
It can be observed that the size of the image is “719MB” for a small and simple project:
How to Reduce the Docker Image Size?
To reduce the Docker image size, we have listed down some famous techniques:
- How to Reduce Docker Image Size Using Docker “–squash”?
- How to Reduce Docker Image Size Using Multistage Build?
How to Reduce Docker Image Size Using Docker “–squash”?
The “docker build –squash” command is used to squash the Docker layer and build the Docker image with minimum or fewer layers. To reduce the size of the Docker image through Docker “–squash”, check out the provided steps.
Step 1: Build Docker Image
Build the Docker image using the “–squash” option to squash some Docker layers and create a new Docker image with fewer layers:
Step 2: View the Image Size
Check the size of the Docker image by executing the “docker image <imagename>” command:
Here, you can see the size of the image is reduced to “714MB”, but still Docker image is too huge for a simple project:
How to Reduce Docker Image Size Using Multistage Build?
The multistage Docker image is another technique to reduce the Docker image size as it gets the dependencies and other major project packages from the builder stage. To reduce the Docker image size, change the Dockerfile into a multistage Dockerfile.
For this purpose, go through the mentioned instructions.
Step 1: Open Dockerfile
First, open the Dockerfile in the nano text editor using the given command:
Step 2: Change Dockerfile to Multistage Dockerfile
Next, change the Dockerfile instruction into multistage through below mentioned commands. The “alpine” is the smallest version of any Docker image. After that, press “CTRL+O” to save the file. In order to exit the nano editor, press “CTRL+X”:
WORKDIR /go/src/app
COPY main.go .
RUN go build -o webserver .
FROM alpine
WORKDIR /app
COPY --from=builder /go/src/app/ /app/
CMD ["./webserver"]
Step 3: Build a Docker Image
Next, build the Docker image using the newly modified multi-stage Dockerfile with the help of the below command:
Again, check the image size using the “docker images” command:
It can be observed that we have successfully reduced the image size to only “12.9MB”:
This post has demonstrated how to reduce the Docker image size.
Conclusion
To reduce the image size, users can either use the Docker “–squash” command or utilize the multistage Dockerfile. To reduce the Docker image size through the Docker “–squash”, utilize the “docker build –squash -t <imagename> .” command. To reduce the size of the image through multistage build, first, modify the Dockerfile to a multistage Dockerfile, then execute the “docker build -t <imagename>” command to create a Docker image. This post has demonstrated how to reduce the Docker image size.