liveBook · Manning

Dockerfile

As explained in Machine Learning Platform Engineering

A Dockerfile is a text file containing instructions for building a Docker image and configuring the application environment inside it.

A Dockerfile is a reproducible packaging plan for an application: it defines the base image, filesystem layers, dependencies, configuration, exposed port, and startup command. This turns a Python service into an executable image that can run consistently across deployment environments. section 3.2.2

A practical Dockerfile commonly assembles an application in this order:

  • Select a base image with FROM and establish the application directory with WORKDIR. section 3.2.2
  • Install operating-system and Python dependencies with RUN, copying requirements before application files. section 3.2.2
  • Set runtime configuration with EXPOSE, ENV, and build-time ARG values. section 3.2.2
  • Make the startup script executable and define it with ENTRYPOINT. section 3.2.2
Listing 3.2 An example Dockerfile for Python applications
FROM python:3.10-slim-buster    #1
WORKDIR /app    #2
RUN apt-get update && \
    apt-get install --no-install-recommends -y \
                       build-essential \
                       && apt-get clean && rm -rf /tmp/* /var/tmp/*
COPY requirements.txt /app/requirements.txt    #3
RUN pip3 install --upgrade pip     #3
RUN pip3 install --no-cache-dir -r requirements.txt     #3
COPY . /app    #4
EXPOSE 8083
ENV PYTHONPATH="/app"    #5
ARG environment     #5
ENV ENVIRONMENT $environment     #5
RUN chmod +x /app/entrypoint.sh    #6
ENTRYPOINT ["/app/entrypoint.sh"]     #6

Use the complete Python service Dockerfile as a concrete template for layering dependencies, configuration, and startup behavior. section 3.2.2

Build, run, and publish a service image

Build a tagged image with `docker build . -t hello-joker:v1`, run it with `docker run -it -p 8081:8083 hello-joker:v1`, and verify the service through the published host port. Tags such as `v1` distinguish deployable versions; after logging in, tag and push the image to a container registry. section 3.2.3

For Kubeflow notebooks, a multistage Dockerfile can combine a base image, code-server or Jupyter runtime, dependencies, and compatibility configuration. Optimize its layers and image size before organizational deployment. section 7.1.5

How Machine Learning Platform Engineering covers it

How deep it goes
Explains Dockerfile instructions, image layers, build arguments, startup behavior, image publishing, and multistage notebook construction.
Examples
Uses a Python FastAPI Dockerfile, an Uvicorn entrypoint, Docker build and run commands, and a custom Kubeflow notebook image.
What it assumes
Assumes a Python application with requirements and startup files, while introducing Docker packaging, image construction, and container execution.
What you can do after
Build and run tagged Docker images, publish them to a container registry, and assemble custom Kubeflow notebook images.
How it connects
Connects Dockerfile to Docker images, containers, container registries, Python dependencies, FastAPI, Uvicorn, and Kubeflow notebooks.

Also explained in

Compare how 18 books explain Dockerfile →

Related concepts

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