liveBook · Manning

Docker build

As explained in Data Pipelines with Apache Airflow, Second Edition

docker build is a Docker CLI command that constructs an image from a Dockerfile and its build context.

docker build constructs a Docker image from a Dockerfile and a build context. Docker reads the Dockerfile, finds referenced files such as scripts and dependency manifests, executes its instructions, and assigns the resulting image a tag. section 11.2.3

A typical image-building workflow is:

  • Create a Dockerfile beginning with a base image, then add dependencies, application files, environment settings, and startup instructions. section 11.2.3
  • Place the Dockerfile and files referenced by COPY or ADD in the build context, commonly the current directory. section 11.2.3
  • Build and tag the image with a command such as docker build --tag manning-airflow/wttr-example . section 11.2.3
  • Test the result with docker run, checking that the packaged command responds as expected. section 11.2.3

Packaging a task image

For a task that fetches movie ratings, first turn the Python logic into a command-line script, declare dependencies such as click and requests, copy the script into a Python base image, and build it with docker build -t manning-airflow/movielens-fetch .. Running docker run --rm manning-airflow/movielens-fetch fetch-ratings --help verifies the available options before using the image with DockerOperator. section 11.4.2

Listing 11.3 Dockerfile for fetching weather from the wttr.in API
FROM python:3.12-slim                  #1

COPY requirements.txt /tmp/requirements.txt      #2
RUN pip install -r /tmp/requirements.txt

COPY scripts/fetch_weather.py /usr/local/bin/fetch-weather   #3
RUN chmod +x /usr/local/bin/fetch-weather

ENTRYPOINT [ "/usr/local/bin/fetch-weather" ]                #4
CMD [ "--help" ]                      #5

Inspect a complete Dockerfile showing a Python base image, dependency installation, script copying, executable permissions, ENTRYPOINT, and default arguments. section 11.2.3

How Data Pipelines with Apache Airflow, Second Edition covers it

How deep it goes
Explains Dockerfile instructions, build context, tagging, build output, and runtime testing, but does not detail advanced build configuration.
Examples
Uses weather-fetching and MovieLens ratings images, with tagged builds and docker run help checks.
What it assumes
Assumes a task script and known dependencies, while demonstrating Dockerfile construction and CLI packaging from those inputs.
What you can do after
Build it from scratch by writing a Dockerfile, supplying a context, tagging the image, and testing its packaged command.
How it connects
Connects Dockerfile, build context, Docker image, container, CLI script, requirements.txt, and DockerOperator.

Also explained in

Compare how 4 books explain docker build →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Pipelines with Apache Airflow, Second Edition. The section links point to the parts of the book it draws on.