生产容器构建文件编写与验证
结合仓库配置生成多阶段容器构建文件及忽略规则,并验证运行权限、健康端点与退出行为。
完整提示词
<context>
Most Dockerfiles work; few are good in production. What separates them: a multi-stage build so compilers, dev dependencies and source maps stay out of the final image; copying only the dependency manifests and lockfile before installing, so the dependency layer is cached until the lockfile changes; BuildKit cache mounts for package manager caches; a `.dockerignore` that keeps `.git`, `node_modules`, local env files and build output out of the context. Base images should be pinned to a specific version, ideally with a digest, and chosen deliberately: Alpine uses musl, which breaks or slows many Python wheels and some native Node modules; slim Debian-based images avoid that; distroless images have no shell, which is safer but changes how you debug. Secrets passed as `ARG` or `ENV` stay readable in the image history; build-time secrets belong in `--mount=type=secret`. The process should run as a non-root user with a numeric UID (Kubernetes `runAsNonRoot` checks the number). `CMD` and `ENTRYPOINT` in exec form let PID 1 receive `SIGTERM`; shell form or a wrapper script without `exec` means the container is killed after the grace period instead of shutting down cleanly. `HEALTHCHECK` is used by Docker and Compose but ignored by Kubernetes, which uses probes.
</context>
<task>
Write a production Dockerfile for {{app}}.
{{#notes}}
Notes:
{{notes}}
{{/notes}}
1. Read the repository: language and version files, lockfiles, build and start commands, environment variables the app reads, ports, files it writes at runtime, existing Dockerfiles or Compose files and CI image builds. Follow existing conventions.
2. Choose the base images for build and runtime stages with reasons, pinned to a version (and a digest if the team pins digests). Match the language version the project declares.
3. Write the build stage: copy manifests and lockfile, install dependencies with a cache mount, copy source, build. Use secret mounts for private registry credentials.
4. Write the runtime stage: copy only the built output and production dependencies, create or use a non-root user with a numeric UID, set ownership only where the app writes, set `WORKDIR`, `EXPOSE` and production environment defaults, and start the app in exec form (add an init such as `tini` if the app spawns child processes or does not handle signals).
5. Add a `HEALTHCHECK` if the target uses it, and OCI labels for source and version if the team uses them.
6. Write or update `.dockerignore`.
7. If Docker is available, build the image, run it, hit the health or main endpoint, check it runs as non-root, stop it and confirm it exits promptly on `SIGTERM`, and report the image size. Run `hadolint` if available.
</task>
<constraints>
- Never put secrets, tokens or `.env` files into the image or into `ARG`/`ENV`.
- Do not use `latest` or unpinned base image tags.
- Do not install debugging tools or package manager caches in the runtime stage.
- Do not change the app's code or start command beyond what the container needs; list anything that should change as a follow-up.
{{> guardrails/verify-before-done}}
{{> guardrails/scope-discipline}}
</constraints>
<output_format>
## Dockerfile
Path and full contents, with short comments on non-obvious lines.
## dockerignore
Full contents.
## Decisions
One bullet each for base images, user, signal handling, caching, secrets and health check.
## Verification
Build, run, endpoint check, user check, stop time, image size and hadolint result, or "not run" with the commands to run.
</output_format>可替换内容
- {{app}}
- {{#notes}}
- {{notes}}
- {{/notes}}
- {{> guardrails/verify-before-done}}
- {{> guardrails/scope-discipline}}
分类与标签
编程开发、容器部署、多阶段构建、镜像优化、非特权运行