Purpose of specifying several UNIX commands in a single RUN instruction in Dockerfile

后端 未结 2 515
心在旅途
心在旅途 2021-01-28 06:21

I have noticed that many Dockerfiles try to minimize the number of instructions by several UNIX commands in a single RUN instruction. So i

2条回答
  •  孤城傲影
    2021-01-28 07:00

    In addition to the space savings, it's also about correctness

    Consider your first dockerfile (a common mistake when working with debian-like systems which utilize apt):

    FROM ubuntu 
    MAINTAINER demousr@example.com 
    
    RUN apt-get update 
    RUN apt-get install –y nginx 
    CMD ["echo", "Image created"] 
    

    If two or more images follow this pattern, a cache hit could cause the image to be unbuildable due to cached metadata

    • let's say I built an image which looks similar to that ~a few weeks ago
    • now I'm building this image today. there's a cache present up until the RUN apt-get update line
    • the docker build will reuse that cached layer (since the dockerfile and base image are identical) up to the RUN apt-get update
    • when the RUN apt-get install line runs, it will use the cached apt metadata (which is now weeks out of date and likely will error)

提交回复
热议问题