Docker Community Forums

Share and learn in the Docker community.

Image build question

build

(Vrms) #1

is there any advantage to aim for fewer layers in an image rather then many?

in other words … would:

RUN apt-get install vim
RUN apt-get install curl

… be less ideal then …

RUN apt-get install vim curl

… when building an image from a Dockerfile?


(Vrms) #2

ok, that’s what https://docs.docker.com/engine/userguide/eng-image/dockerfile_best-practices/
has to say in this regards

Minimize the number of layers

You need to find the balance between readability (and thus long-term maintainability) of the Dockerfile and minimizing the number of layers it uses.

Be strategic and cautious about the number of layers you use.

So my example would ideally look like this (according to them)

...
RUN apt-get update && apt-get install -y \
curl \
vim
...

(David Maze) #3

Also remember that, if you want to delete anything, you must delete it in the same layer you create it or it doesn’t save any space. My Dockerfiles frequently look like

RUN apt-get update
 && env DEBIAN_FRONTEND=noninteractive \
    apt-get install --assume-yes --no-recommends \
      curl \
      vim \
 && apt-get clean