liveBook · Manning

Environment variable

As explained in Machine Learning Platform Engineering

An environment variable is a named value supplied through a process environment to configure or provide information to an application.

An environment variable is a named value supplied through a process environment to configure an application without embedding the setting in application code. Production workflows use it across image construction, Kubernetes deployment, and continuous integration. section 3.2.2 section 3.3.5

Use environment variables at different operational boundaries:

  • Set image-level values with Dockerfile ENV; combine it with a build-time ARG when selecting values such as ENVIRONMENT. section 3.2.2
  • Store non-sensitive settings such as database URLs and API endpoints in Kubernetes ConfigMaps, then inject selected keys into container environments. section 3.3.5
  • Keep credentials in Kubernetes Secrets or GitLab CI/CD variables rather than ordinary configuration, while recognizing that base64 encoding is not encryption. section 3.3.5 section 3.4.1

Injecting deployment configuration

A Kubernetes Pod can map DATABASE_URL from the database-url key in test-configmap. The application reads the resulting process variable while the ConfigMap remains separate from its container image and code. section 3.3.5

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

Inspect the Dockerfile pattern that declares PYTHONPATH, accepts an environment build argument, and exposes ENVIRONMENT to the container. section 3.2.2

How Machine Learning Platform Engineering covers it

How deep it goes
Explains declaration, injection, and credential handling through Dockerfile, Pod, ConfigMap, Secret, and GitLab examples.
Examples
Demonstrates ENVIRONMENT, DATABASE_URL, API_KEY, database credentials, and Docker registry credentials.
What it assumes
Assumes basic Dockerfile, Kubernetes, and CI concepts while demonstrating configuration from Docker image to deployed workload.
What you can do after
Operate/configure deployments by injecting settings through Docker, Kubernetes objects, and GitLab CI/CD variables.
How it connects
Connects environment variables to Docker, Dockerfile, Kubernetes, ConfigMap, Secret, and GitLab CI.

Also explained in

Compare how 19 books explain environment variable →

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.