liveBook · Manning

Dockerfile

As explained in Road to Kubernetes

A Dockerfile is a text-based configuration file that specifies how to build a container image.

Installing software inside a running container does not save those changes to its image. To make the setup repeatable, write a Dockerfile: a file of instructions that a container builder uses to create a new image. That image can then be run or shared. section 5.1.4 section 5.2 section 1.4

A first Dockerfile needs only a few instructions:

  • FROM chooses a prebuilt image to start from. section 5.2.1
  • RUN executes a build-time command, such as installing software. section 5.2.1
  • CMD sets the command to run when the container starts. section 5.2.1
Listing 5.2 Your first Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y nginx
CMD ["nginx", "-g", "daemon off;"]

The short NGINX Dockerfile starts from Ubuntu, installs NGINX, and sets its startup command. section 5.2.1

Build the NGINX image

From the Dockerfile's directory, run `docker build -t rk8s-nginx -f Dockerfile .`. The `-f` flag selects the Dockerfile; `-t` names the resulting image. section 5.2.1

Application Dockerfiles follow the same pattern with different contents. A Python example starts from `python:3.11.4`, copies source into `/app`, installs dependencies, and sets a server command. A Node.js example starts from `node:18.17.0`, copies source and package files, runs `npm install`, and starts an entrypoint script. section 5.3.2 section 5.4.2

Different environments can use different files: for example, `docker build -f Dockerfile.prod -t my-container:prod .` selects a production Dockerfile. A successful build is not proof that the application will run correctly, so build and run the image before relying on it. section 7.2.1 section 1.5

How Road to Kubernetes covers it

How deep it goes
Works through core directives and image builds, while leaving intricate Dockerfile design aside.
Examples
Uses an Ubuntu-and-NGINX image, then Python and Node.js application Dockerfiles.
What it assumes
Introduces Dockerfile syntax from the basics while using familiar shell commands and application dependencies.
What you can do after
Build it from scratch with Dockerfile directives, then build and run a tagged application image using Docker.
How it connects
Connects Dockerfiles to container images, builders, registries, and runtimes.

Also explained in

Compare how 18 books explain Dockerfile →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.