This is the 27th day of my participation in Gwen Challenge

background

A previous Python project using Docker deployment looked at how to slim down images and put notes together

Python projects slim down

The FastText prediction is based on Python, and the underlying FastText relies on C++ ‘s.so dynamic library, so when installing FastText through PIP, you need to use GCC to compile it

Method 1: Use Python: Latest

Pull python: Latest, which contains the full PIP environment and GCC compilation environment, and can build images and deploy normally, but the only problem is that the size of the image is too large. The resulting image is 1.5 GIGABytes

Approach 2: Multi-stage build Python :3.7.7 and Python: Alpine3.11

Python: Alpine3.11 is the smallest of the official Python images. The first phase uses Python :3.7.7 to compile web.py and fasttext, and then the second build phase copies all files in the site-package directory into Python: Alpine3.11 , but the problem is that alpine version of C++ dynamic library environment is missing a lot, even if it is copied from python:3.7.7 stdc++. So and other libraries, but there is still a problem that symbol cannot be found in the dynamic library, guess it should be alpine version of compatibility problems

Method 3: Multi-stage Build Python :3.7.7- Slim

Dockerfile uses two stages, one for compilation and the other for final image generation, both using Python :3.7.7-slim

The first phase failed because fasttext required support for C++11, but the slim version didn’t compile, so it didn’t work

During development and testing, it is recommended to use a native base image, which is larger but works just like Ubuntu on your host and has access to all the binaries available on the operating system.

Method 4: Multi-stage build python3.7.7 and Python :3.7.7- Slim

Python: 3.7.7-Slim This image contains both the Python environment and the full GCC environment, which is the perfect solution to the slim problem

Install web.py,fasttext, and RUN PIP install Web. py==0.51 && PIP install fasttext && find /usr/local/lib-name '*.pyc' -delete # Python: 3.7.7 - slim # COPY compiled library COPY - from = builder/usr/local/lib/python3.7 / site - packages / usr/local/lib/python3.7 / site - packages RUN mkdir/apps COPY predict. Py/apps # COPY model. The bin/apps WORKDIR/apps CMD ["python", "/apps/predict.py", "9999"]Copy the code

Mirror slimming Note

  1. Each RUN statement creates a new layer, so multiple RUN statements are executed in series using &&
Layer RUN apt-get update && apt-get install vimCopy the code
  1. With a multi-phase build, the files compiled in the first phase are copied directly to the second phase
As build WORKDIR /app COPY package.json index.js./ RUN NPM install node:8 COPY --from=build /app / EXPOSE 3000 CMD ["index.js"]Copy the code
  1. Use Distroless to remove all the baggage from the container

Google has open-source the Distroless image it uses, which contains only the application and its runtime dependencies. Package managers, shells, and other programs found in standard Linux distributions are not included, but you need to climb the wall to download the corresponding image

FROM node:8 as build

WORKDIR /app
COPY package.json index.js ./
RUN npm install

FROM gcr.io/distroless/nodejs

COPY --from=build /app /
EXPOSE 3000
CMD ["index.js"]
Copy the code
  1. Use Alpine as a smaller base image

Alpine Linux is a lightweight, security-oriented Linux distribution based on Musl Libc and Busybox that can be used for business mirroring, which is even smaller than Distroless mirroring

Note that Alpine mirrors without bash, but sh does

FROM node:8 as build

WORKDIR /app
COPY package.json index.js ./
RUN npm install

FROM node:8-alpine

COPY --from=build /app /
EXPOSE 3000
CMD ["npm", "start"]
Copy the code