容器/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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值