my receipe for writing dockerfile

A beginner’s guide to write acceptable dockerfiles

ov

  • use alpine linux or tag with “slim”
    • smaller
  • use multi-stage build
  • combine multiple RUNs in one RUN
    • logically the commands are in a group
  • use .dockerignore
  • use dive, a tool to check how to slim down image size
  • use distroless by google

caching

  • to better utilize caching of the intermediate images, move the parts that are unlikely to change at the top of the dockerfile
    • like ADD, COPY
    • requirements install

inspect

  • view the size of intermediate images
    • docker image history <image>:<tag>
  • dive
  • combine apt-get update and apt-get install, and append rm -rf /var/lib/apt/lists/* in the end
    • state information for each package resource specified in apt’s sources.list
    • see this

gosu

  • when you need to use sudo, use gosu instead
  • commonly, gosu nobody true is used to confirm that the installed gosu is OK
  • typically gosu is later used in entrypoint.sh
  • how to install gosu in dockerfile?
  • commonly used to step from root to non-root user and run the app
    • e.g. exec gosu <username> <command> will run the command as username

ref