CODE/2022-09-30

Building Docker Images with a Dockerfile

A Docker image packages an application's filesystem and configuration. A Dockerfile describes how to build that image reproducibly from a base image.

A minimal Dockerfile

Given an app.py in the build context:
docker_snippet
Docker · 4 lines|
FROM python:3.12-slim
WORKDIR /app
COPY app.py .
CMD ["python", "app.py"]
Build and run it from the directory containing the Dockerfile and app.py:
bash_snippet
Bash · 2 lines|
docker build -t example-app .
docker run --rm example-app
The final . is the build context. Files referenced by COPY must be available in that context and not excluded by .dockerignore.

Common instructions

  • FROM selects a base image. A Dockerfile can have multiple build stages, each beginning with FROM.
  • WORKDIR sets the working directory for later instructions.
  • COPY adds files from the build context or another stage.
  • RUN executes a command during the image build.
  • CMD provides a default command or arguments when a container starts.
  • ENTRYPOINT configures the executable to run at container startup.
  • LABEL adds metadata; the older MAINTAINER instruction is deprecated.
Some instructions add filesystem layers, while others set image metadata. ARG can appear before the first FROM when used to parameterize the base image.

Practical guidance

Keep the build context small with .dockerignore, choose a maintained base image, pin versions where reproducibility matters, and avoid placing credentials in image layers. Use a non-root runtime user where practical. Multi-stage builds can separate build tools from the final runtime image.
COMPANION ESSAYS / 思想脉络与互照篇目
同卷深读2026-05-22 ·

Installing Python 3 Alongside System Python on CentOS 7

同属「系统工程卷」脉络衍生思考与工程推演推演阅读 →
跨界互照2026-08-22 ·

我的书架:03|金融经管:经济运行、金融体系与企业治理

我的书架:03|金融经管:经济运行、金融体系与企业治理

跨领域对照思辨 · 从「思想随笔」视界互为印证推演阅读 →
RELATED INDEX / 相关阅读索引
柯航hang.ke·© 2014—2026
CC BY-NC 4.0·