liveBook · Manning

Dockerfile

As explained in Kubernetes for Developers

A Dockerfile is a text file containing instructions for building a Docker container image.

A new developer needs your application, its language runtime, and the right dependencies. A Dockerfile puts the steps for assembling that environment in one place instead of leaving them in setup notes or someone’s memory. Docker uses those instructions to build a container image that others can build and run. section 2.0 section 2.1.3

Think of a Dockerfile as a short procedure for preparing an image. Start from a base image, then add what the application needs. The resulting container keeps its dependencies separate from other applications on the same host, making local development and deployment more reproducible. section 1.1.1 section 2.1.3

Listing 2.2 Chapter02/2.1.3_Dockerfile/Dockerfile
FROM ubuntu                      #1
RUN apt-get update               #2
RUN apt-get install -y python3   #2
COPY . /app                      #3
WORKDIR /app                     #4

This Python example starts with Ubuntu, installs Python 3, copies the application into /app, and sets /app as the working directory. section 2.1.3

From the Dockerfile’s directory, `docker build . -t hello` builds the image. The example runs it with `docker run hello python3 hello.py`. Building the image and running the program are separate steps; the run command supplies the Python command in this example. section 2.1.3

For a compiled application, copying source files is not enough unless they have already been compiled. A Dockerfile can perform that work during the build. The Java example starts from OpenJDK, copies the source, sets a working directory, compiles with `RUN javac Hello.java`, and specifies `CMD java Hello` for execution. Multistage builds can separate the environment used to build a program from the one used to run it. section 2.1.7 section 2.4

Listing 2.12 Chapter02/2.1.7_CompiledCode/Dockerfile
FROM openjdk
COPY . /app
WORKDIR /app
RUN javac Hello.java   #1
CMD java Hello
#1 The compile command

Compare this Java Dockerfile with the Python one: its RUN instruction compiles source code, while CMD names the command used to run the application. section 2.1.7

How Kubernetes for Developers covers it

How deep it goes
Works through Python image construction and Java compilation, then briefly identifies multistage builds without detailing their Dockerfile instructions.
Examples
Uses Ubuntu and Python 3 for one Dockerfile and OpenJDK with javac for another, alongside build-and-run commands.
What it assumes
Starts with a simple Python script and introduces the Dockerfile and base image before building the container image.
What you can do after
Build it from scratch by choosing a base image, adding the application and dependencies, and building the resulting image with Docker.
How it connects
Connects container images, base images, dependency isolation, compiled applications, multistage builds, and Kubernetes deployment.

Also explained in

Compare how 18 books explain Dockerfile →

Related concepts

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