Skip to content

Chapter 3 of 7

Building Images with Dockerfile

A Dockerfile is a plain-text file containing a series of instructions that Docker uses to build an image automatically. The build is triggered with docker build -t name:tag ., where the trailing dot specifies the current directory as the build context. Each instruction in the Dockerfile creates a new layer in the resulting image, and the order of those instructions matters for both correctness and efficiency.

The Dockerfile always begins with a FROM instruction, which selects a base image that everything else is built on top of, such as ubuntu:20.04. From there, RUN executes shell commands during the build (for example, RUN apt-get update) and each RUN creates a new image layer. WORKDIR sets the working directory used by subsequent instructions and creates it if it does not already exist, while CMD provides default arguments for the command that will run when a container starts. ENTRYPOINT goes a step further by configuring the container's main executable, with any CMD values passed as arguments to that executable. USER switches to a non-root user for the following instructions, which is a key security practice, and HEALTHCHECK defines a command that Docker runs periodically inside the container to report whether the service is actually healthy.

Several techniques help produce smaller, faster, more secure images. Multi-stage builds use multiple FROM statements so that compilers and build tools can live in an early stage while only the final artifacts are copied into a slim runtime stage. Layer caching means Docker reuses any layer whose inputs have not changed, so it pays to place stable, rarely-modified instructions early and to combine related shell commands onto a single RUN line. A .dockerignore file works like a .gitignore, excluding files such as node_modules from the build context. Together with choosing minimal base images, these practices reduce image size and improve build performance.

All chapters
  1. 1Foundations of Containers and Docker
  2. 2Docker Architecture, Installation, and Core CLI
  3. 3Building Images with Dockerfile
  4. 4Running and Managing Containers
  5. 5Data Persistence and Networking
  6. 6Docker Compose and Orchestration with Swarm
  7. 7Security and Troubleshooting

Drill it

Reading is not remembering. These come from the Docker And Containers deck:

Q

What is a container in computing?

A container is a lightweight, standalone, executable package that includes everything needed to run a piece of software, such as code, runtime, libraries, and d...

Q

What is containerization?

Containerization is a virtualization method that packages an application and its dependencies into a container, enabling consistent deployment across different...

Q

What is Docker?

Docker is an open-source platform that automates the deployment, scaling, and management of applications inside containers using containerization technology.

Q

What is the difference between a container and a virtual machine?

Containers share the host OS kernel and are lightweight, starting in seconds, while virtual machines include a full guest OS, making them heavier and slower to...