容器/Docker要點記錄

開發日常都會接觸到容器和docker,以下作記錄:

實現原理
Dockerfile
  • 採用和遵循multi-stage builds 做法, 編譯和運行使用不同的基礎鏡像。
  • 承接上一點,由於常規配置下(docker run不加任何參數),docker與宿主機高度隔離,此時docker運行環境的時區timezone等設置往往是空白的, 如FROM alpine:latest, 此時, 若應用邏輯依賴時間或時區, 如某golang程序:
loc, _     = time.LoadLocation("Asia/Shanghai")
t := time.Unix(t1, 0).In(loc)

docker運行環境需要添加依賴:

FROM alpine:latest
RUN apk --no-cache tzdata
  • 對於golang 開發的app,每次構建時均要下載依賴的dockerfile, 可使用goproxy加快依賴下載速度, 而其配置因go版本而異, 如go 1.12 ENV GOPROXY=https://goproxy.cn

  • Docker 的入口程序與Kubernetes的入口程序

    • Dockerfile 用於製作鏡像,可以在文件最後指定ENTRYPOINT作爲鏡像的入口程序,即pid1的進程。
    • Kubernetes用於啓動podyaml中,也可指定CommandArgs作爲Docker啓動後的入口程序, 即pid1的進程。
    • 1號進程退出(儘管入口程序派生了其他後臺進程),則Docker退出。
    • docker是通過docker run啓動的,docker 會完成生命週期,docker進程會退出。
    • docker是通過Kubernetes deployment啓動的,則pod中的docker在執行完入口程序後會退出,導致pod不斷重啓,入口程序反覆執行。
    • 一般做法會在入口程序的最後添加類似shellsleep infinity等語句阻止1號進程退出,使得1號進程派生的後臺進程能持續服務。
    • DockerKubernetes入口程序先後關係:若在dockerfile和k8s中均指定入口程序,則有如下先後關係, 即使用Kubernetes 創建的docker若指定了Command,則該docker鏡像的ENTRYPOINT會被覆蓋, 相當於調用了docker run ... --entrypoint:
    If you do not supply command or args for a Container,
    the defaults defined in the Docker image are used.
    
    If you supply a command but no args for a Container,  only the supplied command is used. 
    The default EntryPoint and the default Cmd defined in the Docker image are ignored.
    
    If you supply only args for a Container, 
    the default Entrypoint defined in the Docker image is run with the args that you supplied.
    
    If you supply a command and args, 
    the default Entrypoint and the default Cmd defined in the Docker image are ignored. Your command is run with your args.
Docker daemon API
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章