1.編譯原始碼為可執行檔後再把執行檔放入乾淨的環境
# build stage
FROM golang:1.14.7-alpine3.12 AS builder
LABEL stage=ithome-intermediate
ENV GO111MODULE=on
ADD ./ /go/src/ithome
RUN cd /go/src/ithome && go build -mod vendor
# final stage
FROM alpine:3.11.3
COPY --from=builder /go/src/ithome/ithome /go/src/ithome/.env ./
CMD ["./ithome"]
# 將要被複製的image as builder
FROM nginx:latest AS builder
# 進行複製動作
FROM nginx:latest
COPY --from=builder /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/my-copy-default.conf
RUN ls /etc/nginx/conf.d/
通常目的都是為了讓build的image乾淨又輕量化,而輕量化的優點:
docker push/pull image
的過程會更加快速,因為image size縮小許多!相關來源: https://docs.docker.com/develop/develop-images/multistage-build/
相信大家在輕量化image後,image瘦了不少
但是當你的image內需要時區設定時,在alpine image預設是沒有提供此設定的唷
所以要在dockerfile內加上TimeZone tzdata!
RUN apk add --no-cache tzdata
這樣才有辦法抓到時區設定檔
以上述範例1,來調整的話,就下方這樣子
# build stage
FROM golang:1.14.7-alpine3.12 AS builder
LABEL stage=ithome-intermediate
ENV GO111MODULE=on
ADD ./ /go/src/ithome
RUN cd /go/src/ithome && go build -mod vendor
# final stage
FROM alpine:3.11.3
# add TimeZone tzdata
RUN apk add --no-cache tzdata
COPY --from=builder /go/src/ithome/ithome /go/src/ithome/.env ./
CMD ["./ithome"]
以上範例,大家可以練習一下~多使用就會知道這個功能的美好!
如果沒特殊需求,可以直接讓容器讀取Host的時區設定
給你參考@@
sudo docker run \
--name myAlpine \
-d \
-it \
-v /etc/localtime:/etc/localtime:ro \
-v /etc/timezone:/etc/timezone:ro \
alpine