Source: blog.csdn.net/update7
Docker profile
Docker is a platform for software developers and system administrators to build, run, and share applications using containers. A container is a process that runs in a separate environment on its own file system, built using a Docker image. The image contains everything you need to run your application (compiled code, dependencies, libraries, and so on). The image is defined using a Dockerfile file.
The terms dockerization or containerization are commonly used to define the process of creating a Docker container.
Containers are popular because they have the following advantages:
- Flexibility: Even the most complex applications can be containerized.
- Lightweight: Containers share host cores, making them far more efficient than virtual machines.
- Portability: you can compile locally and run anywhere.
- Loose coupling: Containers wrap themselves so that one container can be replaced or upgraded without breaking another.
- Security: Processes are tightly restricted and isolated by the container without any configuration by the user.
In this article, I’ll focus on how to optimize Docker images to make them lightweight.
The optimization process
Let’s start with an example where we built and containerized a React application. 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
Copy the code
If we build a basic Dockerfile (as shown below), we end up with a 1.16GB 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
Copy the code
Step 1 Optimization: Use a lightweight base image
In the Docker Hub (public Docker repository), there are several images available for download, each with different characteristics and sizes.
In general, images based on Alpine or BusyBox are very small compared to images based on other Linux distributions, such as Ubuntu. This is because the Alpine mirror and others like it are optimized to contain the minimum number of required packages. In the image below, you can see the size comparison between Ubuntu, Alpine, Node, and Alpine based Node images.
By modifying the Dockerfile and using Alpine as the base image, our image ends up being 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
Copy the code
Step 2 optimization: Multi-stage build
With a multi-phase build, we can use multiple base images in a Dockerfile and copy compiled artifacts, configuration files, and so on from one phase to another so that we can discard unnecessary stuff.
In this case, we need compiled code to deploy the React application. We don’t need source files, the node_modules directory, package.json files, etc.
By changing the Dockerfile to the following, we end up with an image size of 91.5MB. Keep in mind that the image from phase 1 (lines 1-4) is not automatically deleted, Docker keeps it in the cache, and if we perform the same phase in another image build process, we can make the image build faster. So you must manually delete the first stage mirror.
FROM node:10-alpine AS build WORKDIR /app COPY app /app RUN npm install && npm run build FROM node:10-alpineWORKDIR /app RUN npm install -g webserver.local COPY --from=build /app/build ./build EXPOSE 3000 CMD webserver.local -d ./buildCopy the code
Now we have a Dockerfile that 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 serving web pages (HTML, CSS and JavaScript files, images, and so on). The best choice is to use a service like Nginx or Apache.
In this case, I’ll use Nginx. In addition, Nginx series of interview questions and answers are all sorted out, wechat search Java technology stack, sent in the background: interview, can be read online.
By changing the Dockerfile to the following, our image ends up being 22.4MB in size, and if we run the container, we can see that the 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;"]
Copy the code
Recent hot articles recommended:
1.1,000+ Java Interview Questions and Answers (2021)
2. Don’t use if/ else on full screen again, try strategy mode, it smells good!!
3. Oh, my gosh! What new syntax is xx ≠ null in Java?
4.Spring Boot 2.5 is a blockbuster release, and dark mode is exploding!
5. “Java Development Manual (Songshan version)” the latest release, quick download!
Feel good, don’t forget to click on + forward oh!