deploy-docker-production

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker Next.js Local Deploy Skill

Docker Next.js 本地部署技能

Purpose

用途

Use this skill to build and run a Next.js application with Docker using a versioned image tag.
This skill must not hard-code the project name. Infer the image name from the current repository unless the user provides a specific name.
使用此技能通过带版本号的镜像标签,借助Docker构建并运行Next.js应用。
本技能不得硬编码项目名称。除非用户提供特定名称,否则从当前仓库推断镜像名称。

Project Name Resolution

项目名称解析

Before building, determine these values:
text
PROJECT_NAME=<resolved project name>
IMAGE_NAME=<resolved image name>
IMAGE_VERSION=<resolved version>
IMAGE_TAG=${IMAGE_NAME}:${IMAGE_VERSION}
CONTAINER_NAME=<resolved container name>
ENV_FILE=<resolved env file>
HOST_PORT=<resolved host port>
CONTAINER_PORT=<resolved container port>
Resolution rules:
  1. If the user provides an image name, use it.
  2. Otherwise, read
    package.json
    and use the
    name
    field.
  3. If
    package.json
    has no
    name
    , use the current folder name.
  4. Normalize the image name:
    • lowercase
    • replace spaces and underscores with hyphens
    • remove invalid Docker image name characters
  5. If the user provides a version, use it.
  6. Otherwise, default to
    1.0.0
    .
  7. If the user provides a container name, use it.
  8. Otherwise, use the normalized image name as the container name.
  9. If the user provides an env file, use it.
  10. Otherwise, default to
    .env.production
    .
  11. If the user provides ports, use them.
  12. Otherwise, default to
    HOST_PORT=4000
    and
    CONTAINER_PORT=3000
    .
Example:
text
package.json name: nextjs-skill-app-workshop

PROJECT_NAME=nextjs-skill-app-workshop
IMAGE_NAME=nextjs-skill-app-workshop
IMAGE_VERSION=1.0.0
IMAGE_TAG=nextjs-skill-app-workshop:1.0.0
CONTAINER_NAME=nextjs-skill-app-workshop
ENV_FILE=.env.production
HOST_PORT=4000
CONTAINER_PORT=3000
构建前,确定以下值:
text
PROJECT_NAME=<resolved project name>
IMAGE_NAME=<resolved image name>
IMAGE_VERSION=<resolved version>
IMAGE_TAG=${IMAGE_NAME}:${IMAGE_VERSION}
CONTAINER_NAME=<resolved container name>
ENV_FILE=<resolved env file>
HOST_PORT=<resolved host port>
CONTAINER_PORT=<resolved container port>
解析规则:
  1. 如果用户提供镜像名称,则使用该名称。
  2. 否则,读取
    package.json
    并使用
    name
    字段。
  3. 如果
    package.json
    没有
    name
    字段,则使用当前文件夹名称。
  4. 标准化镜像名称:
    • 转为小写
    • 将空格和下划线替换为连字符
    • 移除Docker镜像名称中的无效字符
  5. 如果用户提供版本号,则使用该版本。
  6. 否则,默认使用
    1.0.0
  7. 如果用户提供容器名称,则使用该名称。
  8. 否则,使用标准化后的镜像名称作为容器名称。
  9. 如果用户提供环境文件,则使用该文件。
  10. 否则,默认使用
    .env.production
  11. 如果用户提供端口,则使用该端口。
  12. 否则,默认设置
    HOST_PORT=4000
    CONTAINER_PORT=3000
示例:
text
package.json name: nextjs-skill-app-workshop

PROJECT_NAME=nextjs-skill-app-workshop
IMAGE_NAME=nextjs-skill-app-workshop
IMAGE_VERSION=1.0.0
IMAGE_TAG=nextjs-skill-app-workshop:1.0.0
CONTAINER_NAME=nextjs-skill-app-workshop
ENV_FILE=.env.production
HOST_PORT=4000
CONTAINER_PORT=3000

Safety Rules

安全规则

Before running any build or run command:
  1. Confirm the current working directory is the project root.
  2. Confirm
    Dockerfile
    exists.
  3. Confirm the env file exists.
  4. Confirm Docker is installed and running.
  5. Check whether the requested image tag already exists locally.
  6. Check whether the requested container name already exists.
  7. Never print secrets from the env file.
  8. Do not use the
    latest
    tag unless the user explicitly asks.
  9. Do not run destructive cleanup commands such as:
    • docker system prune
    • docker image prune
    • docker volume prune
  10. Do not stop, remove, or replace an existing container without explicit confirmation from the user.
  11. Do not rebuild an existing version tag unless the user clearly confirms.
运行任何构建或运行命令前:
  1. 确认当前工作目录为项目根目录。
  2. 确认
    Dockerfile
    存在。
  3. 确认环境文件存在。
  4. 确认Docker已安装并正在运行。
  5. 检查请求的镜像标签是否已在本地存在。
  6. 检查请求的容器名称是否已存在。
  7. 绝不要打印环境文件中的机密信息。
  8. 除非用户明确要求,否则不要使用
    latest
    标签。
  9. 不要运行破坏性的清理命令,例如:
    • docker system prune
    • docker image prune
    • docker volume prune
  10. 未经用户明确确认,不要停止、移除或替换现有容器。
  11. 除非用户明确确认,否则不要重新构建已存在的版本标签。

Preflight Checks

预检检查

Run these checks first:
bash
pwd
ls -la

test -f package.json && echo "OK: package.json found" || echo "WARN: package.json not found"
test -f Dockerfile && echo "OK: Dockerfile found" || echo "ERROR: Dockerfile not found"
test -f "$ENV_FILE" && echo "OK: env file found: $ENV_FILE" || echo "ERROR: env file not found: $ENV_FILE"

docker --version
docker info >/dev/null && echo "OK: Docker daemon is running" || echo "ERROR: Docker daemon is not running"
If
Dockerfile
is missing, stop and report the issue.
If the env file is missing, stop and report the issue. Do not create a placeholder env file unless the user asks.
If Docker daemon is not running, stop and tell the user to start Docker Desktop or Docker Engine.
首先运行以下检查:
bash
pwd
ls -la

test -f package.json && echo "OK: package.json found" || echo "WARN: package.json not found"
test -f Dockerfile && echo "OK: Dockerfile found" || echo "ERROR: Dockerfile not found"
test -f "$ENV_FILE" && echo "OK: env file found: $ENV_FILE" || echo "ERROR: env file not found: $ENV_FILE"

docker --version
docker info >/dev/null && echo "OK: Docker daemon is running" || echo "ERROR: Docker daemon is not running"
如果缺少
Dockerfile
,停止操作并报告问题。
如果缺少环境文件,停止操作并报告问题。除非用户要求,否则不要创建占位环境文件。
如果Docker守护进程未运行,停止操作并告知用户启动Docker Desktop或Docker Engine。

Suggested Helper Commands

建议的辅助命令

To infer the image name from
package.json
:
bash
node -p "require('./package.json').name"
If
package.json
does not exist or has no
name
, use the current directory name:
bash
basename "$PWD"
To normalize a project name manually, use lowercase kebab-case.
Example:
text
My Next App -> my-next-app
nextjs_skill_app -> nextjs-skill-app
package.json
推断镜像名称:
bash
node -p "require('./package.json').name"
如果
package.json
不存在或没有
name
字段,使用当前目录名称:
bash
basename "$PWD"
要手动标准化项目名称,使用小写短横线命名法(kebab-case)。
示例:
text
My Next App -> my-next-app
nextjs_skill_app -> nextjs-skill-app

Version Tag Check

版本标签检查

Before building, check whether the requested image tag already exists:
bash
docker image inspect "$IMAGE_TAG" >/dev/null 2>&1 \
  && echo "EXISTS: $IMAGE_TAG" \
  || echo "AVAILABLE: $IMAGE_TAG"
Also list existing local versions for this image:
bash
docker images "$IMAGE_NAME" --format "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedSince}}\t{{.Size}}"
If the requested image tag already exists:
  1. Do not run
    docker build
    .
  2. Report that the version already exists locally.
  3. Suggest the next patch version, such as
    1.0.1
    .
  4. Ask the user whether to use a new version or explicitly rebuild the same tag.
Only rebuild an existing tag if the user clearly confirms.
构建前,检查请求的镜像标签是否已存在:
bash
docker image inspect "$IMAGE_TAG" >/dev/null 2>&1 \
  && echo "EXISTS: $IMAGE_TAG" \
  || echo "AVAILABLE: $IMAGE_TAG"
同时列出该镜像的现有本地版本:
bash
docker images "$IMAGE_NAME" --format "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedSince}}\t{{.Size}}"
如果请求的镜像标签已存在:
  1. 不要运行
    docker build
  2. 报告该版本已在本地存在。
  3. 建议使用下一个补丁版本,例如
    1.0.1
  4. 询问用户是否要使用新版本或明确重新构建相同标签。
仅当用户明确确认时,才重新构建现有标签。

Container Name Check

容器名称检查

Before running the container, check whether the container name already exists:
bash
docker ps -a --filter "name=^/${CONTAINER_NAME}$" \
  --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"
If a container with the same name already exists:
  1. Do not run another container with the same name.
  2. Report the existing container status.
  3. Ask the user whether to stop/remove it or use a different container name.
  4. Do not remove it automatically.
运行容器前,检查容器名称是否已存在:
bash
docker ps -a --filter "name=^/${CONTAINER_NAME}$" \
  --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"
如果同名容器已存在:
  1. 不要运行另一个同名容器。
  2. 报告现有容器的状态。
  3. 询问用户是否要停止/移除它,或使用不同的容器名称。
  4. 不要自动移除它。

Build Command

构建命令

If the image tag does not exist, run:
bash
docker build -t "$IMAGE_TAG" .
After build, verify the image exists:
bash
docker image inspect "$IMAGE_TAG" >/dev/null \
  && echo "OK: image built successfully: $IMAGE_TAG" \
  || echo "ERROR: image not found after build: $IMAGE_TAG"
如果镜像标签不存在,运行:
bash
docker build -t "$IMAGE_TAG" .
构建完成后,验证镜像是否存在:
bash
docker image inspect "$IMAGE_TAG" >/dev/null \
  && echo "OK: image built successfully: $IMAGE_TAG" \
  || echo "ERROR: image not found after build: $IMAGE_TAG"

Run Command

运行命令

If the image exists and the container name is available, run:
bash
docker run --restart=always -d \
  --name "$CONTAINER_NAME" \
  --env-file "$ENV_FILE" \
  -p "${HOST_PORT}:${CONTAINER_PORT}" \
  "$IMAGE_TAG"
如果镜像存在且容器名称可用,运行:
bash
docker run --restart=always -d \
  --name "$CONTAINER_NAME" \
  --env-file "$ENV_FILE" \
  -p "${HOST_PORT}:${CONTAINER_PORT}" \
  "$IMAGE_TAG"

Post-run Validation

运行后验证

After running the container, validate with:
bash
docker ps --filter "name=^/${CONTAINER_NAME}$" \
  --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"

docker logs --tail=80 "$CONTAINER_NAME"

curl -I "http://localhost:${HOST_PORT}"
Expected result:
  1. Container status should be
    Up
    .
  2. Port mapping should include
    ${HOST_PORT}->${CONTAINER_PORT}
    .
  3. Logs should not show fatal errors.
  4. curl -I
    should return an HTTP response such as
    200
    ,
    301
    ,
    302
    ,
    307
    , or
    308
    .
If
curl
fails, inspect logs and report the likely cause.
运行容器后,通过以下命令验证:
bash
docker ps --filter "name=^/${CONTAINER_NAME}$" \
  --format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"

docker logs --tail=80 "$CONTAINER_NAME"

curl -I "http://localhost:${HOST_PORT}"
预期结果:
  1. 容器状态应为
    Up
  2. 端口映射应包含
    ${HOST_PORT}->${CONTAINER_PORT}
  3. 日志不应显示致命错误。
  4. curl -I
    应返回HTTP响应,例如
    200
    301
    302
    307
    308
如果
curl
失败,检查日志并报告可能的原因。

Common Failure Handling

常见故障处理

Image tag already exists

镜像标签已存在

Report:
text
Image tag already exists locally:
<IMAGE_TAG>

Recommended action:
- Use a new patch version, such as 1.0.1
- Or explicitly confirm rebuilding the same tag
报告:
text
本地已存在该镜像标签:
<IMAGE_TAG>

建议操作:
- 使用新的补丁版本,例如1.0.1
- 或明确确认重新构建相同标签

Container name already exists

容器名称已存在

Report:
text
Container already exists:
<CONTAINER_NAME>

Please confirm whether to stop/remove it, or provide a different container name.
报告:
text
容器已存在:
<CONTAINER_NAME>

请确认是否要停止/移除它,或提供一个不同的容器名称。

Port already in use

端口已被占用

Check:
bash
lsof -i :"$HOST_PORT"
Report the process using the port and ask whether the user wants to change
HOST_PORT
.
检查:
bash
lsof -i :"$HOST_PORT"
报告占用端口的进程,并询问用户是否要修改
HOST_PORT

Env file missing

环境文件缺失

Report:
text
Env file was not found:
<ENV_FILE>

I cannot run the container with --env-file until this file exists.
Do not print or infer secrets.
报告:
text
未找到环境文件:
<ENV_FILE>

在该文件存在之前,我无法使用--env-file参数运行容器。
不要打印或推断机密信息。

Docker daemon not running

Docker守护进程未运行

Report:
text
Docker is installed but the daemon is not running.
Start Docker Desktop or Docker Engine, then run the preflight checks again.
报告:
text
Docker已安装,但守护进程未运行。
请启动Docker Desktop或Docker Engine,然后重新运行预检检查。

Final Response Format

最终响应格式

When finished, report:
text
Result:
- Project: <PROJECT_NAME>
- Image: <IMAGE_TAG>
- Container: <CONTAINER_NAME>
- Env file: <ENV_FILE>
- Port: http://localhost:<HOST_PORT>
- Restart policy: always
- Validation: passed/failed

Commands used:
<list important commands>

Notes:
<any warnings, existing image/container conflicts, or follow-up action>
If the workflow stops because of an existing version tag, missing env file, existing container, Docker error, or port conflict, clearly state the blocking issue and the safest next command.
完成后,报告:
text
结果:
- 项目:<PROJECT_NAME>
- 镜像:<IMAGE_TAG>
- 容器:<CONTAINER_NAME>
- 环境文件:<ENV_FILE>
- 端口:http://localhost:<HOST_PORT>
- 重启策略:always
- 验证:通过/失败

使用的命令:
<列出重要命令>

说明:
<任何警告、现有镜像/容器冲突或后续操作>
如果因现有版本标签、缺失环境文件、现有容器、Docker错误或端口冲突导致流程停止,请明确说明阻塞问题和最安全的下一步命令。