Alpine compiles Docker tsim rau Python 50 zaug qeeb dua, thiab cov duab yog 2 zaug hnyav dua

Alpine compiles Docker tsim rau Python 50 zaug qeeb dua, thiab cov duab yog 2 zaug hnyav dua

Alpine Linux feem ntau pom zoo ua lub hauv paus duab rau Docker. Koj tau hais tias siv Alpine yuav ua rau koj qhov tsim me me thiab koj cov txheej txheem tsim sai dua.

Tab sis yog tias koj siv Alpine Linux rau Python daim ntawv thov, ces nws:

  • Ua rau koj tsim kom qeeb qeeb
  • Ua kom koj cov duab loj dua
  • nkim koj lub sij hawm
  • Thiab thaum kawg nws tuaj yeem ua yuam kev hauv runtime


Cia peb saib yog vim li cas Alpine tau pom zoo, tab sis vim li cas koj tseem yuav tsum tsis txhob siv nws nrog Python.

Vim li cas tib neeg pom zoo Alpine?

Cia peb xav tias peb xav tau gcc ua ib feem ntawm peb cov duab thiab peb xav piv Alpine Linux vs Ubuntu 18.04 hais txog kev tsim nrawm thiab qhov kawg duab loj.

Ua ntej, cia peb rub tawm ob daim duab thiab sib piv lawv qhov ntau thiab tsawg:

$ docker pull --quiet ubuntu:18.04
docker.io/library/ubuntu:18.04
$ docker pull --quiet alpine
docker.io/library/alpine:latest
$ docker image ls ubuntu:18.04
REPOSITORY          TAG        IMAGE ID         SIZE
ubuntu              18.04      ccc6e87d482b     64.2MB
$ docker image ls alpine
REPOSITORY          TAG        IMAGE ID         SIZE
alpine              latest     e7d92cdc71fe     5.59MB

Raws li koj tuaj yeem pom, lub hauv paus duab rau Alpine yog me dua. Tam sim no cia peb sim rau nruab gcc thiab pib nrog Ubuntu:

FROM ubuntu:18.04
RUN apt-get update && 
    apt-get install --no-install-recommends -y gcc && 
    apt-get clean && rm -rf /var/lib/apt/lists/*

Sau qhov zoo meej Dockerfile yog dhau ntawm cov kab lus no.

Cia peb ntsuas qhov sib dhos ceev:

$ time docker build -t ubuntu-gcc -f Dockerfile.ubuntu --quiet .
sha256:b6a3ee33acb83148cd273b0098f4c7eed01a82f47eeb8f5bec775c26d4fe4aae

real    0m29.251s
user    0m0.032s
sys     0m0.026s
$ docker image ls ubuntu-gcc
REPOSITORY   TAG      IMAGE ID      CREATED         SIZE
ubuntu-gcc   latest   b6a3ee33acb8  9 seconds ago   150MB

Peb rov ua tib yam rau Alpine (Dockerfile):

FROM alpine
RUN apk add --update gcc

Peb sib sau ua ke, saib lub sijhawm thiab qhov loj ntawm lub rooj sib txoos:

$ time docker build -t alpine-gcc -f Dockerfile.alpine --quiet .
sha256:efd626923c1478ccde67db28911ef90799710e5b8125cf4ebb2b2ca200ae1ac3

real    0m15.461s
user    0m0.026s
sys     0m0.024s
$ docker image ls alpine-gcc
REPOSITORY   TAG      IMAGE ID       CREATED         SIZE
alpine-gcc   latest   efd626923c14   7 seconds ago   105MB

Raws li tau cog lus tseg, Alpine-raws li cov duab tau sau sai dua thiab me dua: 15 vib nas this tsis yog 30 thiab cov duab loj yog 105MB piv rau 150MB. Nws zoo nkauj heev!

Tab sis yog tias peb hloov mus tsim daim ntawv thov Python, ces txhua yam tsis yog rosy.

Python duab

Cov ntawv thov Python feem ntau siv pandas thiab matplotlib. Yog li ntawd, ib qho kev xaiv yog coj tus nom Debian-raws li duab siv Dockerfile no:

FROM python:3.8-slim
RUN pip install --no-cache-dir matplotlib pandas

Cia peb sau nws:

$ docker build -f Dockerfile.slim -t python-matpan.
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM python:3.8-slim
 ---> 036ea1506a85
Step 2/2 : RUN pip install --no-cache-dir matplotlib pandas
 ---> Running in 13739b2a0917
Collecting matplotlib
  Downloading matplotlib-3.1.2-cp38-cp38-manylinux1_x86_64.whl (13.1 MB)
Collecting pandas
  Downloading pandas-0.25.3-cp38-cp38-manylinux1_x86_64.whl (10.4 MB)
...
Successfully built b98b5dc06690
Successfully tagged python-matpan:latest

real    0m30.297s
user    0m0.043s
sys     0m0.020s

Peb tau txais ib daim duab ntawm 363MB loj.
Peb puas yuav ua tau zoo dua nrog Alpine? Wb sim:

FROM python:3.8-alpine
RUN pip install --no-cache-dir matplotlib pandas

$ docker build -t python-matpan-alpine -f Dockerfile.alpine .                                 
Sending build context to Docker daemon  3.072kB                                               
Step 1/2 : FROM python:3.8-alpine                                                             
 ---> a0ee0c90a0db                                                                            
Step 2/2 : RUN pip install --no-cache-dir matplotlib pandas                                                  
 ---> Running in 6740adad3729                                                                 
Collecting matplotlib                                                                         
  Downloading matplotlib-3.1.2.tar.gz (40.9 MB)                                               
    ERROR: Command errored out with exit status 1:                                            
     command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/
tmp/pip-install-a3olrixa/matplotlib/setup.py'"'"'; __file__='"'"'/tmp/pip-install-a3olrixa/matplotlib/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-a3olrixa/matplotlib/pip-egg-info                              

...
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
The command '/bin/sh -c pip install matplotlib pandas' returned a non-zero code: 1

Dab tsi yog yuav mus?

Alpine tsis txhawb lub log

Yog tias koj saib ntawm qhov tsim, uas yog raws li Debian, koj yuav pom tias nws rub tawm matplotlib-3.1.2-cp38-cp38-manylinux1_x86_64.whl.

Qhov no yog binary rau lub log. Alpine downloads qhov chaw 'matplotlib-3.1.2.tar.gz'vim nws tsis txhawb tus qauv log.

Vim li cas? Feem ntau Linux faib siv GNU version (glibc) ntawm C tus qauv tsev qiv ntawv, uas yog qhov tseeb yuav tsum tau los ntawm txhua qhov kev pab cuam sau hauv C, suav nrog Python. Tab sis Alpine siv 'musl', thiab txij li cov binaries yog tsim los rau 'glibc', lawv tsis yog ib qho kev xaiv.

Yog li ntawd, yog tias koj siv Alpine, koj yuav tsum tau sau tag nrho cov lej sau hauv C hauv txhua pob Python.

Auj, yog, koj yuav tau mus nrhiav cov npe ntawm tag nrho cov kev vam meej uas yuav tsum tau muab tso ua ke koj tus kheej.
Hauv qhov no peb tau txais qhov no:

FROM python:3.8-alpine
RUN apk --update add gcc build-base freetype-dev libpng-dev openblas-dev
RUN pip install --no-cache-dir matplotlib pandas

Thiab lub sijhawm tsim kho yuav siv sijhawm ...

25 feeb 57 seconds! Thiab cov duab loj yog 851MB.

Alpine-raws li cov duab siv sijhawm ntev dua los tsim, lawv loj dua qhov loj, thiab koj tseem yuav tsum tau saib rau txhua qhov kev vam khom. Koj tuaj yeem txo qhov sib dhos loj siv ntau theem tsim tab sis qhov ntawd txhais tau tias yuav tsum tau ua haujlwm ntau dua.

Qhov ntawd tsis yog tag nrho!

Alpine tej zaum yuav ua rau cov kab tsis xav tau hauv lub sijhawm ua haujlwm

  • Hauv txoj kev xav, musl yog sib xws nrog glibc, tab sis hauv kev xyaum, qhov sib txawv tuaj yeem ua rau muaj ntau yam teeb meem. Thiab yog hais tias lawv yog, lawv tej zaum yuav tsis kaj siab. Nov yog qee qhov teeb meem uas yuav tshwm sim:
  • Alpine muaj cov xov me me me los ntawm lub neej ntawd, uas tuaj yeem ua rau yuam kev hauv Python
  • Qee cov neeg siv tau pom qhov ntawd Cov ntawv thov Python qeeb dua vim txoj kev musl faib lub cim xeeb (sib txawv ntawm glibc).
  • Ib ntawm cov neeg siv pom ib qho yuam kev thaum formatting hnub

Muaj tseeb tiag cov kev ua yuam kev no twb raug kho lawm, tab sis leej twg paub yuav ua li cas muaj ntau yam ntxiv.

Tsis txhob siv cov duab Alpine rau Python

Yog tias koj tsis xav thab nrog kev tsim loj thiab ntev, tshawb nrhiav kev vam khom thiab muaj peev xwm ua yuam kev, tsis txhob siv Alpine Linux ua lub hauv paus duab. Xaiv lub hauv paus duab zoo.

Tau qhov twg los: www.hab.com

Ntxiv ib saib