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
FROMselects a base image. A Dockerfile can have multiple build stages, each beginning withFROM.
WORKDIRsets the working directory for later instructions.
COPYadds files from the build context or another stage.
RUNexecutes a command during the image build.
CMDprovides a default command or arguments when a container starts.
ENTRYPOINTconfigures the executable to run at container startup.
LABELadds metadata; the olderMAINTAINERinstruction 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.✦ 卷终 · 柯航存卷 ✦