拋磚引玉nginx
2 . 精簡爲美docker
# v1 FROM nginx:1.15-alpine RUN echo "hello" RUN echo "demo best practise" ENTRYPOINT [ "/bin/sh" ]
# v2 FROM nginx:1.15-alpine RUN echo "hello" RUN echo "demo best practise 02" ENTRYPOINT [ "/bin/sh" ]
全新構建緩存
# docker build -t demo:0.0.1 . Sending build context to Docker daemon 2.048kB Step 1/4 : FROM nginx:1.15-alpine ---> 9a2868cac230 Step 2/4 : RUN echo "hello" ---> Running in d301b4b3ed55 hello Removing intermediate container d301b4b3ed55 ---> 6dd2a7773bbc Step 3/4 : RUN echo "demo best practise" ---> Running in e3084037668e demo best practise Removing intermediate container e3084037668e ---> 4588ecf9837a Step 4/4 : ENTRYPOINT [ "/bin/sh" ] ---> Running in d63f460347ff Removing intermediate container d63f460347ff ---> 77b52d828f21 Successfully built 77b52d828f21 Successfully tagged demo:0.0.1
Dockerfile 與 1st build 徹底一致, 命令僅修改 build tag , 從 0.0.1 到 0.0.2架構
# docker build -t demo:0.0.2 .Sending build context to Docker daemon 4.096kB Step 1/4 : FROM nginx:1.15-alpine ---> 9a2868cac230 Step 2/4 : RUN echo "hello" ---> Using cache ---> 6dd2a7773bbc Step 3/4 : RUN echo "demo best practise" ---> Using cache ---> 4588ecf9837a Step 4/4 : ENTRYPOINT [ "/bin/sh" ] ---> Using cache ---> 77b52d828f21 Successfully built 77b52d828f21 Successfully tagged demo:0.0.2
能夠看到,ide
此次, 咱們將第三層 RUN echo "demo best practise" 變動爲 RUN echo "demo best practise 02"優化
docker build -t demo:0.0.3 . Sending build context to Docker daemon 4.608kB Step 1/4 : FROM nginx:1.15-alpine ---> 9a2868cac230 Step 2/4 : RUN echo "hello" ---> Using cache ---> 6dd2a7773bbc Step 3/4 : RUN echo "demo best practise 02" ---> Running in c55f94e217bd demo best practise 02Removing intermediate container c55f94e217bd ---> 46992ea04f49 Step 4/4 : ENTRYPOINT [ "/bin/sh" ] ---> Running in f176830cf445 Removing intermediate container f176830cf445 ---> 2e2043b7f3cb Successfully built 2e2043b7f3cb Successfully tagged demo:0.0.3
能夠看到 ,ui
雖然第四層的操做沒有變動,可是因爲上層的鏡像已經變化了,因此第四層自己也發生了變化。操作系統
注意: 每層在 build 的時候都是依賴於上冊 ---> Running in f176830cf445。code
第四次構建, 此次使用 --no-cache 不使用緩存, 模擬在另外一臺電腦上進行 build 。對象
# docker build -t demo:0.0.4 --no-cache . Sending build context to Docker daemon 5.632kB Step 1/4 : FROM nginx:1.15-alpine ---> 9a2868cac230 Step 2/4 : RUN echo "hello" ---> Running in 7ecbed95c4cdhello Removing intermediate container 7ecbed95c4cd ---> a1c998781f2e Step 3/4 : RUN echo "demo best practise 02" ---> Running in e90dae9440c2 demo best practise 02Removing intermediate container e90dae9440c2 ---> 09bf3b4238b8 Step 4/4 : ENTRYPOINT [ "/bin/sh" ] ---> Running in 2ec19670cb14 Removing intermediate container 2ec19670cb14 ---> 9a552fa08f73 Successfully built 9a552fa08f73 Successfully tagged demo:0.0.4
能夠看到,
Dockerfile 之外的功夫
緩存層
FROM
基礎層, 以複用在生產場景中的基礎鏡像。Dockerfile 之內的事項
ADD
和 COPY
時, 儘可能不要以 文件夾
做爲對象,由於文件夾中變化因子更多。儘量的將變化頻發的文件放在最下層。.dockerignore
忽略不須要的文件,以保證 context
的簡潔。