Alpine e bokella Docker e hahela Python makhetlo a 50 butle, 'me litšoantšo li boima ka makhetlo a 2

Alpine e bokella Docker e hahela Python makhetlo a 50 butle, 'me litšoantšo li boima ka makhetlo a 2

Alpine Linux hangata e khothaletsoa joalo ka setšoantšo sa motheo bakeng sa Docker. U bolelloa hore ho sebelisa Alpine ho tla etsa hore meaho ea hau e be nyane mme ts'ebetso ea hau ea kaho e potlake.

Empa haeba u sebelisa Alpine Linux bakeng sa lits'ebetso tsa Python, joale e:

  • E etsa hore moralo oa hau o be butle haholo
  • E holisa litšoantšo tsa hau
  • Ho itshenyetsa nako
  • 'Me qetellong e ka baka liphoso nakong ea ho sebetsa


Ha re shebeng hore na ke hobaneng ha Alpine e khothaletsoa, ​​​​empa ke hobane'ng ha u ntse u sa lokela ho e sebelisa le Python.

Ke hobane'ng ha batho ba khothalletsa Alpine?

Ha re nke hore re hloka gcc e le karolo ea setšoantšo sa rona mme re batla ho bapisa Alpine Linux vs Ubuntu 18.04 mabapi le lebelo la ho aha le boholo ba setšoantšo sa ho qetela.

Taba ea pele, ha re khoasolle litšoantšo tse peli ebe re bapisa boholo ba tsona:

$ 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

Joalokaha u ka bona, setšoantšo sa motheo bakeng sa Alpine se nyane haholo. Ha re leke hona joale ho kenya gcc mme re qale ka 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/*

Ho ngola Dockerfile e phethahetseng ho feta boholo ba sengoloa sena.

Ha re lekanye lebelo la kopano:

$ 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

Re pheta se tšoanang bakeng sa Alpine (Dockerfile):

FROM alpine
RUN apk add --update gcc

Rea bokana, sheba nako le boholo ba kopano:

$ 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

Joalokaha ho tšepisitsoe, litšoantšo tse thehiloeng Alpine li bokelloa ka potlako 'me li nyenyane: metsotsoana ea 15 ho e-na le 30 le boholo ba setšoantšo ke 105MB khahlanong le 150MB. E ntle haholo!

Empa haeba re fetohela ho aheng kopo ea Python, ntho e 'ngoe le e' ngoe ha e monate hakaalo.

Setšoantšo sa Python

Lisebelisoa tsa Python hangata li sebelisa li-pandas le matplotlib. Ka hona, khetho e le 'ngoe ke ho nka setšoantšo sa semmuso se thehiloeng ho Debian u sebelisa Dockerfile ena:

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

Ha re e bokelle:

$ 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

Re fumana setšoantšo sa 363MB ka boholo.
Na re tla phela betere ka Alpine? Ha re leke:

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

Ho ntse ho tsoela pele

Alpine ha e tšehetse mabili

Haeba u sheba mohaho, o thehiloeng ho Debian, u tla bona hore o khoasolla matplotlib-3.1.2-cp38-cp38-manylinux1_x86_64.whl.

Ena ke binary bakeng sa lebili. Alpine e khoasolla mehloli ea `matplotlib-3.1.2.tar.gz` kaha ha e tšehetse maemo mabili.

Hobaneng? Liphatlalatso tse ngata tsa Linux li sebelisa mofuta oa GNU (glibc) oa laeborari e tloaelehileng ea C, eo ha e le hantle e hlokoang ke lenaneo le leng le le leng le ngotsoeng ka C, ho kenyeletsoa Python. Empa Alpine e sebelisa `musl`, 'me kaha li-binary tseo li etselitsoe `glibc`, ha se khetho feela.

Ka hona, haeba u sebelisa Alpine, u lokela ho bokella khoutu eohle e ngotsoeng ka C ka har'a sephutheloana se seng le se seng sa Python.

Oh, e, u tla tlameha ho batla lethathamo la lintho tsohle tse joalo tse itšetlehileng ka uena tse lokelang ho bokelloa.
Tabeng ena, re fumana sena:

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

'Me nako ea ho haha ​​e nka ...

... Metsotso e 25 metsotsoana e 57! 'Me boholo ba setšoantšo ke 851MB.

Litšoantšo tse thehiloeng ho Alpine li nka nako e telele ho hahoa, li kholoanyane ka boholo, 'me u ntse u hloka ho batla lintho tsohle tse itšetlehileng ka tsona. Ehlile u ka fokotsa boholo ba kopano u sebelisa meaho e methati e mengata empa hoo ho bolela hore ho na le mosebetsi o mongata o lokelang ho etsoa.

Ha se tsohle!

Alpine e ka baka likokoana-hloko tse sa lebelloang nakong ea ho sebetsa

  • Ka khopolo, musl e lumellana le glibc, empa ha e le hantle liphapang li ka baka mathata a mangata. 'Me haeba li le teng, mohlomong li tla ba tse sa thabiseng. Mathata a mang a ka hlahang ke ana:
  • Alpine e na le boholo bo fokolang ba khoele ka mokhoa o ikhethileng, e ka lebisang ho liphoso tsa Python
  • Basebelisi ba bang ba fumane seo Lisebelisoa tsa Python li lieha ka lebaka la tsela eo musl a fanang ka mohopolo (e fapaneng le glibc).
  • E mong oa basebelisi e fumane phoso ha e fometa letsatsi

Ehlile liphoso tsena li se li lokisitsoe, empa ke mang ea tsebang hore na ho tla ba le tse kae hape.

Se ke oa sebelisa litšoantšo tsa Alpine bakeng sa Python

Haeba u sa batle ho khathatseha ka meaho e meholo le e telele, ho batla litšepiso le liphoso tse ka bang teng, u se ke oa sebelisa Alpine Linux joalo ka setšoantšo sa motheo. Ho khetha setšoantšo se setle sa motheo.

Source: www.habr.com

Eketsa ka tlhaloso