Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of Docker image optimization?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what is the method of Docker image optimization". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Docker is a platform for software developers and system administrators to build, run, and share applications with containers. A container is a process that runs in a stand-alone environment, running on its own file system, which is built using docker images. The image contains everything you need to run the application (compiled code, dependencies, libraries, and so on). The mirror is defined using a Dockerfile file.

The term dockerization or containerization is often used to define the process of creating an Docker container.

Containers are popular because they have the following advantages:

Flexibility: even the most complex applications can be containerized.

Lightweight: containers share host kernels, making them far more efficient than virtual machines.

Portability: can be compiled locally and run everywhere.

Loose coupling: containers encapsulate themselves, and one container is replaced or upgraded without interrupting other containers.

Security: the container strictly restricts and isolates processes without any configuration by the user.

In this article, I will focus on how to optimize Docker mirrors to make them lightweight.

Let's start with an example where we build a React application and containerize it. After running the npx command and creating the Dockerfile, we have the file structure shown in figure 1.

Npx create-react-app app-template typescript

Figure 1: file structure

If we build a basic Dockerfile (shown below), we end up with a 1.16 GB image:

FROM node:10 WORKDIR / app COPY app / app RUN npm install-g webserver.local RUN npm install & & npm run build EXPOSE 3000 CMD webserver.local-d. / build

Figure 2: the initial size of the mirror is 1.16GB

First step optimization: use lightweight basic mirroring

In Docker Hub (Public Docker Repository), there are several images available for download, each with different features and sizes.

In general, Alpine or BusyBox-based images are very small compared to images based on other Linux distributions, such as Ubuntu. This is because Alpine images and similar images are optimized to contain the fewest necessary packages. In the image below, you can see the size comparison between Ubuntu, Alpine, Node, and Alpine-based Node images.

Figure 3: different sizes of the base image

By modifying Dockerfile and using Alpine as the base image, the final size of our image is 330MB:

FROM node:10-alpine WORKDIR / app COPY app / app RUN npm install-g webserver.local RUN npm install & & npm run build EXPOSE 3000 CMD webserver.local-d. / build

Figure 4: after the first step optimization, the image size is 330MB

The second step of optimization: multi-stage construction

With multi-stage builds, we can use multiple basic images in Dockerfile and copy compiled artifacts, configuration files, and so on from one phase to another, so that we can discard what we don't need.

In this case, what we need to deploy the React application is compiled code, and we don't need source files, node_modules directories, package.json files, and so on.

By modifying the Dockerfile to the following, we end up with an image size of 91.5MB. Keep in mind that the mirror from the first phase (lines 1-4) is not automatically deleted, Docker saves it in cache, and if we perform the same phase in another process of building the mirror, we can make the image build faster. So you have to delete the first phase image manually.

FROM node:10-alpine AS build WORKDIR / app COPY app/ app RUN npm install & & npm run build FROM node:10-alpine WORKDIR / app RUN npm install-g webserver.local COPY-- from=build / app/build. / build EXPOSE 3000 CMD webserver.local-d. / build

Figure 5: the optimized image size in step 2 is 91.5MB.

Now we have a Dockerfile, which has two phases: in the first phase, we compile the project, and in the second phase, we deploy the application on the web server. However, the Node container is not the best choice for providing web services (HTML, CSS and JavaScript files, images, etc.). The best choice is to use services such as Nginx or Apache. In this case, I will use Nginx.

By modifying the Dockerfile to the following, our final image size is 22.4MB, and if we run this container, we can see that the web page works without any problems (figure 7).

FROM node:10-alpine AS build WORKDIR / app COPY app/ app RUN npm install & & npm run build FROM nginx:stable-alpine COPY-- from=build / app/build / usr/share/nginx/html EXPOSE 80 CMD ["nginx", "- g", "daemon off;"]

Figure 6: the optimized image size in step 3 is 22.4MB.

Figure 7: running result of the final container

This is the end of the content of "what is the method of Docker image optimization". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report