热门角色不仅是灵感来源,更是你的效率助手。通过精挑细选的角色提示词,你可以快速生成高质量内容、提升创作灵感,并找到最契合你需求的解决方案。让创作更轻松,让价值更直接!
我们根据不同用户需求,持续更新角色库,让你总能找到合适的灵感入口。
本提示词专为DevOps场景设计,能够根据用户提供的应用程序类型、依赖管理方式、构建环境和部署配置等关键参数,生成专业、高效且符合最佳实践的Dockerfile文件。通过系统化的分析流程,确保生成的Dockerfile具备良好的安全性、性能优化和可维护性,适用于各种容器化部署需求,帮助开发者和运维人员快速构建标准化的应用镜像。
# syntax = docker/dockerfile:1.7
# Multi-stage build for a production-grade Node.js API service
# Requirements satisfied:
# - Non-root user for the app process
# - Healthcheck included
# - Minimal final image via multi-stage and dev-deps pruning
# - No secrets baked into image
ARG NODE_VERSION=20
############################
# 1) Dependencies (with dev)
############################
FROM node:${NODE_VERSION}-alpine AS deps
WORKDIR /app
# Speed up installs and keep reproducibility
RUN npm set fund false && npm set audit false
# Leverage build cache
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci
############################
# 2) Build (optional)
############################
FROM node:${NODE_VERSION}-alpine AS builder
WORKDIR /app
ENV NODE_ENV=development
# Bring in deps (including dev) from previous stage
COPY --from=deps /app/node_modules ./node_modules
# Copy source
COPY . .
# Optional build: if "build" script exists it will run; otherwise no-op.
# Also ensure /app/dist exists so later COPY does not fail if build output is absent.
RUN mkdir -p /app/dist && \
(npm run -s build >/dev/null 2>&1 || echo "No build step detected; skipping")
# A tiny healthcheck script that does not require shell or curl
# It checks GET http://127.0.0.1:${PORT:-3000}/healthz (configurable via HEALTHCHECK_PATH)
RUN printf "%s\n" \
"'use strict';" \
"const http = require('node:http');" \
"const timeout = 2000;" \
"const port = process.env.PORT || 3000;" \
"const path = process.env.HEALTHCHECK_PATH || '/healthz';" \
"const req = http.request({host:'127.0.0.1', port, path, method:'GET', timeout}, res => {" \
" res.resume();" \
" process.exit(res.statusCode && res.statusCode < 400 ? 0 : 1);" \
"});" \
"req.on('timeout', () => { req.destroy(new Error('timeout')); });" \
"req.on('error', () => process.exit(1));" \
"req.end();" \
> /app/healthcheck.js
##########################################
# 3) Production dependencies (no dev deps)
##########################################
FROM node:${NODE_VERSION}-alpine AS prod-deps
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci --omit=dev --ignore-scripts
############################
# 4) Runtime (minimal, non-root)
############################
FROM node:${NODE_VERSION}-alpine AS runtime
# Install a minimal init to handle PID 1 signals and zombie reaping
RUN apk add --no-cache tini
# Create app dir owned by non-root "node" user provided by base image
WORKDIR /app
# Copy build artifacts (if any) and healthcheck script from builder
# Ensure proper ownership for non-root runtime user
COPY --from=builder --chown=node:node /app/dist ./dist
COPY --from=builder --chown=node:node /app/healthcheck.js ./healthcheck.js
# Copy application sources (for apps running from source or dist)
# Rely on .dockerignore to exclude unnecessary files at build time
COPY --chown=node:node . .
# Bring in production-only node_modules
COPY --from=prod-deps --chown=node:node /app/node_modules ./node_modules
# Runtime environment
ENV NODE_ENV=production
ENV PORT=3000
# You may override to match your service's ready endpoint
ENV HEALTHCHECK_PATH=/healthz
# Non-root runtime
USER node
# Healthcheck without requiring a shell/curl
HEALTHCHECK --interval=30s --timeout=3s --start-period=15s --retries=3 \
CMD ["node", "/app/healthcheck.js"]
# Expose non-privileged port
EXPOSE 3000
# Use tini as entrypoint, then run the app with Node
ENTRYPOINT ["/sbin/tini","--"]
# Default command executes your package.json `"main"` via "node ."
# If your app uses dist output, ensure "main" points to it, or override CMD accordingly.
CMD ["node","."]
提示:
如需进一步定制(Node 版本固定到具体小版本、使用 distroless 进一步缩小镜像、或添加特定系统依赖),可告知我你的依赖与运行方式,我会给出对应优化版本。
# Python batch-job image optimized for production-grade testing environment
# - Multi-stage build produces a minimal runtime image
# - Non-root user
# - Healthcheck included
# - No sensitive data baked in
########################################
# Builder stage: build wheels offline
########################################
FROM python:3.12-slim AS builder
ARG BUILD_ENV=testing
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1 \
PYTHONDONTWRITEBYTECODE=1
# System deps needed to compile many Python packages
RUN apt-get update && apt-get install -y --no-install-recommends \
build-essential \
gcc \
python3-dev \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /tmp/build
COPY requirements.txt .
# Build all deps as wheels (no cache stored in layer)
RUN pip wheel --no-deps --wheel-dir /wheels -r requirements.txt
########################################
# Runtime stage: minimal, non-root
########################################
FROM python:3.12-slim AS runtime
# OCI labels
LABEL org.opencontainers.image.title="python-batch-job" \
org.opencontainers.image.description="Production-ready Python batch job image (testing env) with non-root user and healthcheck" \
org.opencontainers.image.source="." \
org.opencontainers.image.licenses="Apache-2.0"
# Base env
ENV APP_ENV=testing \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PATH="/opt/venv/bin:${PATH}"
# Install lightweight init and CA certs for TLS
RUN apt-get update && apt-get install -y --no-install-recommends \
tini \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
# Create non-root user
RUN groupadd -g 10001 app && useradd -m -s /usr/sbin/nologin -u 10001 -g 10001 app
# Create virtualenv and install deps from built wheels
RUN python -m venv /opt/venv
COPY --from=builder /wheels /wheels
COPY --from=builder /tmp/build/requirements.txt /tmp/requirements.txt
RUN pip install --no-index --find-links=/wheels -r /tmp/requirements.txt \
&& rm -rf /wheels /tmp/requirements.txt
# App directory
WORKDIR /app
# Copy your source code into the image; ensure you have a proper .dockerignore
COPY --chown=app:app . /app
# Healthcheck (customizable)
# - If /app/healthcheck.py exists: runs it
# - Else if HEALTHCHECK_CMD is set: runs that shell command
# - Else: sanity-checks Python and optional /app/main.py presence
COPY --chown=app:app <<'SH' /opt/healthcheck.sh
#!/bin/sh
set -eu
# prefer a user-provided healthcheck script if present
if [ -f /app/healthcheck.py ]; then
exec python /app/healthcheck.py
fi
# allow override via env
if [ -n "${HEALTHCHECK_CMD:-}" ]; then
# shellcheck disable=SC2086
sh -c "${HEALTHCHECK_CMD}"
exit $?
fi
# default: verify interpreter and app entry (if file exists)
if [ -f /app/main.py ]; then
python - <<'PY'
import importlib.util, sys
spec = importlib.util.spec_from_file_location("app_main", "/app/main.py")
sys.exit(0 if spec is not None else 1)
PY
else
python -c "import sys; sys.exit(0)"
fi
SH
RUN chmod +x /opt/healthcheck.sh
# Optional runtime write dir for batch outputs/logs if needed
RUN mkdir -p /data && chown -R app:app /data
# Drop privileges
USER app:app
# Healthcheck for long-running batch workers.
# For short-lived one-shot jobs, you can disable at runtime with: docker run --no-healthcheck ...
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 CMD ["/opt/healthcheck.sh"]
# Use tini as ENTRYPOINT for proper signal handling and zombie reaping
ENTRYPOINT ["/usr/bin/tini", "-g", "--"]
# Default command: print hint. Override with your actual batch command.
CMD ["python", "-c", "print('Container ready. Override CMD to run your batch job, e.g., python /app/main.py')"]
构建镜像
运行一条一次性批处理(覆盖默认 CMD)
指定自定义健康检查命令(长跑任务/worker 场景)
对于短生命周期的一次性任务(容器很快退出),健康检查没有意义,可在运行时禁用
Kubernetes Job 示例(简要)
覆盖入口命令
使用自定义 healthcheck.py
如需进一步定制(特定系统库、GPU/加速、分布式执行、CI 缓存策略等),请告知你的依赖详情与运行模式。
# syntax=docker/dockerfile:1.7
# -------- Build stage --------
ARG GO_VERSION=1.22
FROM golang:${GO_VERSION}-alpine AS builder
# Build-time args (customize as needed)
ARG MAIN_PACKAGE=.
ARG APP_NAME=service
WORKDIR /src
# Git is needed for go mod with VCS dependencies
RUN apk add --no-cache git
# Leverage layer caching for dependencies
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download && go mod verify
# Copy the rest of the source
COPY . .
# Build a static binary for minimal runtime
ARG TARGETOS
ARG TARGETARCH
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH:-amd64} \
go build -trimpath -ldflags="-s -w" -o /out/${APP_NAME} ${MAIN_PACKAGE}
# -------- Runtime stage --------
ARG ALPINE_VERSION=3.20
FROM alpine:${ALPINE_VERSION} AS runtime
# Create non-root user and install minimal runtime tools
# - tini: proper signal handling and zombie reaping
# - ca-certificates: TLS for outbound calls if needed
RUN addgroup -S app && adduser -S -G app -u 10001 app \
&& apk add --no-cache ca-certificates tini
WORKDIR /app
# Copy binary from builder
ARG APP_NAME=service
COPY --from=builder /out/${APP_NAME} /app/${APP_NAME}
RUN chown -R app:app /app && chmod 0755 /app/${APP_NAME}
# OCI metadata
ARG VERSION=0.0.0
ARG REVISION=unknown
ARG CREATED=unknown
LABEL org.opencontainers.image.title="${APP_NAME}" \
org.opencontainers.image.description="Go microservice container image (staging)" \
org.opencontainers.image.version="${VERSION}" \
org.opencontainers.image.revision="${REVISION}" \
org.opencontainers.image.created="${CREATED}"
# Runtime env
ENV APP_ENV=staging \
TZ=UTC
# Expose service port (change if your service uses a different one)
EXPOSE 8080
# Healthcheck: expects GET /healthz on port 8080
# BusyBox wget is present in Alpine by default.
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD wget -qO- http://127.0.0.1:8080/healthz >/dev/null || exit 1
# Run as non-root
USER app:app
# Init and command
ENTRYPOINT ["/sbin/tini","--"]
CMD ["/app/service"]
目录准备
构建镜像(开启 BuildKit 与缓存)
运行容器(建议的安全与资源设置)
运行前提
推荐 .dockerignore(示例)
健康检查失败
私有模块拉取失败(构建期)
二进制启动报错 “no such file or directory”
需要 CGO 或系统依赖
端口被占用或服务不通
如果你能提供应用的主包路径、实际监听端口与健康检查路径,我可以进一步为你定制化 Dockerfile 的 CMD/EXPOSE/HEALTHCHECK。
用最少的信息,快速生成可直接上线的 Dockerfile 与配套说明,帮助后端、DevOps 与全栈团队在几分钟内完成标准化容器化:自动匹配不同语言与依赖方式,默认内置安全与性能最佳实践(非特权运行、健康检查、最小镜像、不落地敏感信息),并输出构建命令、注意事项、优化建议和排障指南。典型收益:镜像体积减少30%–70%,构建更快、风险更低、协作更顺畅,适用于新项目启动、存量应用改造与CI/CD接入前的规范统一。
在迭代高频的微服务场景中,几分钟拿到可用Dockerfile,统一镜像规范、固化健康检查与权限策略,显著减少回滚与返工。
无需深入研究容器细节,填写框架与依赖即可得到可运行镜像与启动命令,把时间留给业务功能与性能优化。
获得安全基线与探活策略模板,快速定位端口占用、权限不足、依赖缺失等常见故障,提升故障恢复与值班效率。
将模板生成的提示词复制粘贴到您常用的 Chat 应用(如 ChatGPT、Claude 等),即可直接对话使用,无需额外开发。适合个人快速体验和轻量使用场景。
把提示词模板转化为 API,您的程序可任意修改模板参数,通过接口直接调用,轻松实现自动化与批量处理。适合开发者集成与业务系统嵌入。
在 MCP client 中配置对应的 server 地址,让您的 AI 应用自动调用提示词模板。适合高级用户和团队协作,让提示词在不同 AI 工具间无缝衔接。
半价获取高级提示词-优惠即将到期