溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

docker鏡像瘦身行動

發布時間:2020-06-14 14:29:46 來源:網絡 閱讀:4371 作者:yerikyu 欄目:云計算

背景

隨著微服務概念的深入人心,隨著docker開發的持續進行,我們在生產的過程中將會產生大量的docker鏡像,這些鏡像會隨著版本迭代的過程中,這些鏡像將會占用大量的存儲空間,本文將分析影響鏡像大小的因素,隨后提供鏡像瘦身的思路。

Dockerfile、Docker 鏡像和 Docker 容器的關系

不可避免地,我們在docker學習的過程中一定繞不開理解這三者的關系,從研發流程的角度來看來看,Dockerfile 是軟件的原材料,Docker 鏡像是軟件的交付品,而 Docker 容器則可以認為是軟件的運行的狀態。從應用軟件的角度來看,Dockerfile、Docker 鏡像與 Docker 容器分別代表軟件的三個不同階段,Dockerfile 面向開發,Docker 鏡像成為交付標準,Docker 容器則涉及部署與運維,三者缺一不可,合力充當 Docker 體系的基石。
簡單來講,Dockerfile構建出Docker鏡像,通過Docker鏡像運行Docker容器。后續有機會再詳細分析這三者的關系

精簡鏡像

以下是我們精簡鏡像的目的:

  • 更快的構建速度
  • 更小的Docker鏡像大小
  • 更少的Docker鏡像層
  • 充分利用鏡像緩存
  • 增加Dockerfile可讀性
  • 讓Docker容器使用起來更簡單

原理

簡單來講就是:最基礎的鏡像+合并操作指令(減少操作指令)

瘦身行動

我們即將通過一系列的dockerfile開發,對鏡像打包進行一次次的試驗,以盡最大努力的減小鏡像的大小
原始文件
這是一份非常糟糕的Dockerfile文件打包之后,僅作為分析用例

FROM python:3.5.6-slim-stretch
RUN apt-get update
RUN echo python -V 
RUN mkdir /code 
RUN mkdir /code/db
ADD . /code/
WORKDIR /code
RUN pip install -r requirement

運行命令:docker build -t spidermax:test_01 .
將會對鏡像進行打包,docker相關程序會讀取該文件(Dockerfile),以下是鏡像層的部署過程

Sending build context to Docker daemon 289.8kB
Step 1/9 : FROM python:3.5.6-slim-stretch
---> 86669b8e5771
Step 2/9 : RUN apt-get update
---> Using cache
---> 1b77542b82f4
Step 3/9 : RUN echo python -V
---> Using cache
---> de6cc0a916d7
Step 4/9 : RUN mkdir /code
---> Using cache
---> 996828abe04e
Step 5/9 : RUN mkdir /code/db
---> Using cache
---> 2db7c4dfb918
Step 6/9 : ADD . /code/
---> d9d92db45b29
Step 7/9 : WORKDIR /code
---> Running in 5ae405de28fc
Removing intermediate container 5ae405de28fc
---> a7e3696c6a5b
Step 8/9 : RUN pip install -U pip
---> Running in dcf5b22f6f33
Collecting pip
Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip-18.1
Successfully installed pip-19.0.3
Removing intermediate container dcf5b22f6f33
---> 949d89aabe2b
Step 9/9 : RUN pip install -r requirement
---> Running in 697c65e4161a
Collecting Django==2.1.7 (from -r requirement (line 1))
Downloading https://files.pythonhosted.org/packages/c7/87/fbd666c4f87591ae25b7bb374298e8629816e87193c4099d3608ef11fab9/Django-2.1.7-py3-none-any.whl (7.3MB)
Collecting EasyProcess==0.2.5 (from -r requirement (line 2))
Downloading https://files.pythonhosted.org/packages/45/3a/4eecc0c7995a13a64739bbedc0d3691fc574245b7e79cff81905aa0c2b38/EasyProcess-0.2.5.tar.gz
Collecting PyMySQL==0.9.3 (from -r requirement (line 3))
Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
Collecting pytz==2018.9 (from -r requirement (line 4))
Downloading https://files.pythonhosted.org/packages/61/28/1d3920e4d1d50b19bc5d24398a7cd85cc7b9a75a490570d5a30c57622d34/pytz-2018.9-py2.py3-none-any.whl (510kB)
Collecting selenium==3.141.0 (from -r requirement (line 5))
Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
Collecting urllib3==1.24.1 (from -r requirement (line 6))
Downloading https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)
Building wheels for collected packages: EasyProcess
Building wheel for EasyProcess (setup.py): started
Building wheel for EasyProcess (setup.py): finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/41/22/19/af15ef6264c58b625a82641ed7483ad05e258fbd8925505227
Successfully built EasyProcess
Installing collected packages: pytz, Django, EasyProcess, PyMySQL, urllib3, selenium
Successfully installed Django-2.1.7 EasyProcess-0.2.5 PyMySQL-0.9.3 pytz-2018.9 selenium-3.141.0 urllib3-1.24.1
Removing intermediate container 697c65e4161a
---> f59692faac28
Successfully built f59692faac28
Successfully tagged spidermax:test_01

通過docker images查看鏡像大小
docker鏡像瘦身行動
通過docker history我們來觀察鏡像層打包的過程
docker鏡像瘦身行動
這里有若干個missing層,其實是docker打包程序程序自動優化的過程。Docker為了加快鏡像構建速度,也會將每一個鏡像層緩存下來。利用這個特性,可以提升工作效率,不過能用到這個特性卻是有點尷尬:

  • 上一個指令能夠在上一層緩存中找到
  • 緩存中存在一個鏡像層,而且指令和即將打包的指令一模一樣,即使有空格也不行!
  • 如果有類似COPY或者ADD指令,如果他們引用的文件的元數據發生變化,緩存也無效

優化1

接下來我們對相同屬性指令進行合并,比如RUN命令等

FROM python:3.5.6-slim-stretch
RUN apt-get update &&\
    echo python -V &&\
    mkdir /code &&\
    mkdir /code/db
ADD . /code/
WORKDIR /code
RUN pip install -U pip &&\
    pip install -r requirement

以下是編譯日志

docker build -t spidermax:test_02 .
Sending build context to Docker daemon 289.8kB
Step 1/5 : FROM python:3.5.6-slim-stretch
---> 86669b8e5771
Step 2/5 : RUN apt-get update && echo python -V && mkdir /code && mkdir /code/db
---> Running in 2a879b85221f
Get:1 http://security.debian.org/debian-security stretch/updates InRelease [94.3 kB]
Get:2 http://security.debian.org/debian-security stretch/updates/main amd64 Packages [481 kB]
Ign:3 http://deb.debian.org/debian stretch InRelease
Get:4 http://deb.debian.org/debian stretch-updates InRelease [91.0 kB]
Get:5 http://deb.debian.org/debian stretch Release [118 kB]
Get:6 http://deb.debian.org/debian stretch-updates/main amd64 Packages [11.1 kB]
Get:7 http://deb.debian.org/debian stretch Release.gpg [2434 B]
Get:8 http://deb.debian.org/debian stretch/main amd64 Packages [7084 kB]
Fetched 7881 kB in 8s (966 kB/s)
Reading package lists...
python -V
Removing intermediate container 2a879b85221f
---> 5ce22d4611e9
Step 3/5 : ADD . /code/
---> af87134918ca
Step 4/5 : WORKDIR /code
---> Running in 2a5c72670be3
Removing intermediate container 2a5c72670be3
---> 0324df92356d
Step 5/5 : RUN pip install -U pip && pip install -r requirement
---> Running in 439cc1586076
Collecting pip
Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip-18.1
Successfully installed pip-19.0.3
Collecting Django==2.1.7 (from -r requirement (line 1))
Downloading https://files.pythonhosted.org/packages/c7/87/fbd666c4f87591ae25b7bb374298e8629816e87193c4099d3608ef11fab9/Django-2.1.7-py3-none-any.whl (7.3MB)
Collecting EasyProcess==0.2.5 (from -r requirement (line 2))
Downloading https://files.pythonhosted.org/packages/45/3a/4eecc0c7995a13a64739bbedc0d3691fc574245b7e79cff81905aa0c2b38/EasyProcess-0.2.5.tar.gz
Collecting PyMySQL==0.9.3 (from -r requirement (line 3))
Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
Collecting pytz==2018.9 (from -r requirement (line 4))
Downloading https://files.pythonhosted.org/packages/61/28/1d3920e4d1d50b19bc5d24398a7cd85cc7b9a75a490570d5a30c57622d34/pytz-2018.9-py2.py3-none-any.whl (510kB)
Collecting selenium==3.141.0 (from -r requirement (line 5))
Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
Collecting urllib3==1.24.1 (from -r requirement (line 6))
Downloading https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)
Building wheels for collected packages: EasyProcess
Building wheel for EasyProcess (setup.py): started
Building wheel for EasyProcess (setup.py): finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/41/22/19/af15ef6264c58b625a82641ed7483ad05e258fbd8925505227
Successfully built EasyProcess
Installing collected packages: pytz, Django, EasyProcess, PyMySQL, urllib3, selenium
Successfully installed Django-2.1.7 EasyProcess-0.2.5 PyMySQL-0.9.3 pytz-2018.9 selenium-3.141.0 urllib3-1.24.1
Removing intermediate container 439cc1586076
---> d767a8a2d9f2
Successfully built d767a8a2d9f2
Successfully tagged spidermax:test_02

雖然在大小和原來沒有啥區別
docker鏡像瘦身行動
但是在鏡像打包上,步驟上少了很多,這樣在一定程度上就縮短鏡像打包的時間,這一點從剛才的打包日志也有所體現,因為test01版本是需要9步,優化1版本是僅需要5步,打包效率提升將近1倍
docker鏡像瘦身行動

優化2

我們所使用的基礎鏡像是python:3.5.6-slim-stretch,該基礎鏡像導致我們每次打包都需要在其之上進行操作,這樣也導致我們的獎項將會變得非常的大,畢竟基礎就已經達到135M之大,雖然說我們不應該重復造輪子,但是這樣的輪子我們要不起啊
docker鏡像瘦身行動
我們不妨重新反思一下我們所使用的鏡像功能,無非就是python的編譯器、pip以及apt功能,至于說另外的django等文件,是我們通過requirement來安裝的,編譯器自帶的若干依賴我們沒怎么用到(這么描述,不是很嚴謹),那我們為什么不干脆自己部署一個python環境,只要有apt,還有什么事情沒有不能做的?
這里推薦使用apline這款鏡像,簡直是惡魔!竟然只有4M左右
docker鏡像瘦身行動
Alpine 的意思是“高山的”,比如 Alpine plants高山植物,Alpine skiing高山滑雪、the alpine resort阿爾卑斯山勝地。
Alpine Linux 網站首頁注明“Small!Simple!Secure!Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox.”概括了以下特點:

  1. 小巧:基于Musl libc和busybox,和busybox一樣小巧,最小的Docker鏡像只有4MB;
  2. 安全:面向安全的輕量發行版;
  3. 簡單:提供APK包管理工具,軟件的搜索、安裝、刪除、升級都非常方便。
  4. 適合容器使用:由于小巧、功能完備,非常適合作為容器的基礎鏡像。
    docker鏡像瘦身行動
    test03版本的dockerfile如下
    FROM alpine:3.7
    RUN apk update && apk upgrade &&\
    apk add python3 &&\
    apk add python3-dev &&\
    python3 -m ensurepip &&\
    if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi &&\
    if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi &&\
    mkdir /code &&  mkdir /code/db
    ADD . /code/
    WORKDIR /code
    RUN pip install -U pip && pip install -r requirement

    因為安裝python3的時候默認會是pip3,為了安裝方便我們需要將pip3修改為pip,此外還有將python3修改為python,方便我們對鏡像的啟動,以下是運行的日志
    docker build -t spidermax:test_03 .
    Sending build context to Docker daemon 290.3kB
    Step 1/5 : FROM alpine:3.7
    ---> 6d1ef012b567
    Step 2/5 : RUN apk update && apk upgrade && apk add python3 && apk add python3-dev && python3 -m ensurepip && if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && mkdir /code && mkdir /code/db
    ---> Running in ef033831c1a9
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
    v3.7.3-6-g3eacd5f9a6 [http://dl-cdn.alpinelinux.org/alpine/v3.7/main]
    v3.7.3-4-g7296a289a6 [http://dl-cdn.alpinelinux.org/alpine/v3.7/community]
    OK: 9049 distinct packages available
    OK: 4 MiB in 13 packages
    (1/11) Installing libbz2 (1.0.6-r6)
    (2/11) Installing expat (2.2.5-r0)
    (3/11) Installing libffi (3.2.1-r4)
    (4/11) Installing gdbm (1.13-r1)
    因為安裝python3的時候默認會是pip3,為了安裝方便我們需要將pip3修改為pip,此外還有將python3修改為python,方便我們對鏡像的啟動,以下是運行的日志

    docker build -t spidermax:test_03 .
    Sending build context to Docker daemon 290.3kB
    Step 1/5 : FROM alpine:3.7
    ---> 6d1ef012b567
    Step 2/5 : RUN apk update && apk upgrade && apk add python3 && apk add python3-dev && python3 -m ensurepip && if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi && if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi && mkdir /code && mkdir /code/db
    ---> Running in ef033831c1a9
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/main/x86_64/APKINDEX.tar.gz
    fetch http://dl-cdn.alpinelinux.org/alpine/v3.7/community/x86_64/APKINDEX.tar.gz
    v3.7.3-6-g3eacd5f9a6 [http://dl-cdn.alpinelinux.org/alpine/v3.7/main]
    v3.7.3-4-g7296a289a6 http://dl-cdn.alpinelinux.org/alpine/v3.7/community Installing xz-libs (5.2.3-r1)
    (6/11) Installing ncurses-terminfo-base (6.0_p20171125-r1)
    (7/11) Installing ncurses-terminfo (6.0_p20171125-r1)
    (8/11) Installing ncurses-libs (6.0_p20171125-r1)
    (9/11) Installing readline (7.0.003-r0)
    (10/11) Installing sqlite-libs (3.25.3-r0)
    (11/11) Installing python3 (3.6.5-r0)
    Executing busybox-1.27.2-r11.trigger
    OK: 66 MiB in 24 packages
    (1/2) Installing pkgconf (1.3.10-r0)
    (2/2) Installing python3-dev (3.6.5-r0)
    Executing busybox-1.27.2-r11.trigger
    OK: 79 MiB in 26 packages
    Requirement already satisfied: setuptools in /usr/lib/python3.6/site-packages
    Requirement already satisfied: pip in /usr/lib/python3.6/site-packages
    Removing intermediate container ef033831c1a9
    ---> 4250724684eb
    Step 3/5 : ADD . /code/
    ---> 8f1a939f8ac1
    Step 4/5 : WORKDIR /code
    ---> Running in 137e7b42decb
    Removing intermediate container 137e7b42decb
    ---> 29586daada18
    Step 5/5 : RUN pip install -U pip && pip install -r requirement
    ---> Running in 36baec208714
    Collecting pip
    Downloading https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl (1.4MB)
    Installing collected packages: pip
    Found existing installation: pip 9.0.3
    Uninstalling pip-9.0.3:
    Successfully uninstalled pip-9.0.3
    Successfully installed pip-19.0.3
    Collecting Django==2.1.7 (from -r requirement (line 1))
    Downloading https://files.pythonhosted.org/packages/c7/87/fbd666c4f87591ae25b7bb374298e8629816e87193c4099d3608ef11fab9/Django-2.1.7-py3-none-any.whl (7.3MB)
    Collecting EasyProcess==0.2.5 (from -r requirement (line 2))
    Downloading https://files.pythonhosted.org/packages/45/3a/4eecc0c7995a13a64739bbedc0d3691fc574245b7e79cff81905aa0c2b38/EasyProcess-0.2.5.tar.gz
    Collecting PyMySQL==0.9.3 (from -r requirement (line 3))
    Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
    Collecting pytz==2018.9 (from -r requirement (line 4))
    Downloading https://files.pythonhosted.org/packages/61/28/1d3920e4d1d50b19bc5d24398a7cd85cc7b9a75a490570d5a30c57622d34/pytz-2018.9-py2.py3-none-any.whl (510kB)
    Collecting selenium==3.141.0 (from -r requirement (line 5))
    Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
    Collecting urllib3==1.24.1 (from -r requirement (line 6))
    Downloading https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)
    Installing collected packages: pytz, Django, EasyProcess, PyMySQL, urllib3, selenium
    Running setup.py install for EasyProcess: started
    Running setup.py install for EasyProcess: finished with status 'done'
    Successfully installed Django-2.1.7 EasyProcess-0.2.5 PyMySQL-0.9.3 pytz-2018.9 selenium-3.141.0 urllib3-1.24.1
    Removing intermediate container 36baec208714
    ---> e59b7105697a
    Successfully built e59b7105697a
    Successfully tagged spidermax:test_03

檢查鏡像大小,會發現這個鏡像和原來相比我們將其縮小了近一倍

docker鏡像瘦身行動
那這樣已經達到目的了嗎?不還不夠!作為工程師,優化永不止步!

優化3

每一次的鏡像打包,docker都會去云端請求,下載相關的軟件,并安裝,那安裝之后的安裝包會怎么處理呢?——還算存留在鏡像中,也就是所謂的緩存,這些是相對應用層來說的,他們只會占用我們存儲空間,并沒有過多的用處,因此需要的就是將這個緩存的內容刪除,實現起來也很簡單的帶上--no-cache即可,如圖見apk操作詳情
docker鏡像瘦身行動
修改之后的腳本為

FROM alpine:3.7
RUN apk update --no-cache && apk upgrade --update-cache --available &&\
    apk add --no-cache python3 &&\
    apk add --no-cache python3-dev &&\
    python3 -m ensurepip &&\
    if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi &&\
    if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi &&\
    rm -rf /var/lib/apk/* &&\
    mkdir /code &&  mkdir /code/db
ADD . /code/
WORKDIR /code
RUN pip install -U pip && pip install -r requirement

這樣的話,會導致一個問題,就是每次打包都需要一次又一次的重新下載資源,這個其實也是一個效率上的矛盾,一個互相博弈的過程,鏡像大小變化
docker鏡像瘦身行動
當然了,我們可刪除apk的緩存當然可以刪除pip的緩存啦,我們可以通過直接刪除pip的安裝包的形式來減小文件的大小,代碼如下:

FROM alpine:3.7
RUN apk update && apk upgrade --update-cache --available &&\
    apk add python3 &&\
    apk add python3-dev &&\
    python3 -m ensurepip &&\
    rm -r /usr/lib/python*/ensurepip &&\
    if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi &&\
    if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi &&\
    rm -rf /var/lib/apk/* &&\
    mkdir /code && mkdir /code/db
ADD . /code/
WORKDIR /code
RUN pip install -U pip && pip install -r requirement && rm -rf ~/.cache/pip

鏡像的變化如下:
docker鏡像瘦身行動
通過我們的一次次優化之后,我們將一個高達203M的鏡像進行了優化,縮小了將近一倍,在這一次次的編譯過程中,也體會到了打包速度一次比一次的提高,這樣才是優化思想的應用,一次又一次的超越自己

修改之后

最后給這個dockerfile標記維護者的信息,表示說這個鏡像是我提供的,如果有問題了需要幫忙解決可以聯系我,也表示我對這個鏡像擁有著作權,雖然我已經將這個鏡像開源了

FROM alpine:3.7
MAINTAINER yerikyu "yerik_shu@139.com"
RUN apk update && apk upgrade --update-cache --available &&\
    apk add python3 &&\
    apk add python3-dev &&\
    python3 -m ensurepip &&\
    rm -r /usr/lib/python*/ensurepip &&\
    if [ ! -e /usr/bin/pip ]; then ln -s pip3 /usr/bin/pip ; fi &&\
    if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi &&\
    rm -rf /var/lib/apk/* &&\
    mkdir /code && mkdir /code/db
ADD . /code/
WORKDIR /code
RUN pip install -U pip && pip install -r requirement && rm -rf ~/.cache/pip

打包結果如下:
docker鏡像瘦身行動

總結

  • 編寫.dockerignore文件(這個用來忽略某些不需要的緩存文件類似gitignore)
  • 容器只運行單個應用(微服務的一個體現)
  • 將多個RUN指令合并為一個
  • 基礎鏡像的標簽不要用latest
  • 每個RUN指令后刪除多余文件
  • 選擇合適的基礎鏡像(alpine版本最好)
  • 設置WORKDIR和CMD
  • 使用ENTRYPOINT (可選)
  • 在entrypoint腳本中使用exec(本次實驗未體現)
  • COPY與ADD優先使用前者
  • 合理調整COPY與RUN的順序
  • 設置默認的環境變量,映射端口和數據卷(這個我是在docker-compose進行設置的,后面有機會再討論)

    參考資料

    docker鏡像層:http://blog.daocloud.io/principle-of-docker-image/
    alpine官方資料:https://alpinelinux.org/

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女