deploy-docker-production
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocker 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:
-
If the user provides an image name, use it.
-
Otherwise, readand use the
package.jsonfield.name -
Ifhas no
package.json, use the current folder name.name -
Normalize the image name:
- lowercase
- replace spaces and underscores with hyphens
- remove invalid Docker image name characters
-
If the user provides a version, use it.
-
Otherwise, default to.
1.0.0 -
If the user provides a container name, use it.
-
Otherwise, use the normalized image name as the container name.
-
If the user provides an env file, use it.
-
Otherwise, default to.
.env.production -
If the user provides ports, use them.
-
Otherwise, default toand
HOST_PORT=4000.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>解析规则:
-
如果用户提供镜像名称,则使用该名称。
-
否则,读取并使用
package.json字段。name -
如果没有
package.json字段,则使用当前文件夹名称。name -
标准化镜像名称:
- 转为小写
- 将空格和下划线替换为连字符
- 移除Docker镜像名称中的无效字符
-
如果用户提供版本号,则使用该版本。
-
否则,默认使用。
1.0.0 -
如果用户提供容器名称,则使用该名称。
-
否则,使用标准化后的镜像名称作为容器名称。
-
如果用户提供环境文件,则使用该文件。
-
否则,默认使用。
.env.production -
如果用户提供端口,则使用该端口。
-
否则,默认设置和
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=3000Safety Rules
安全规则
Before running any build or run command:
-
Confirm the current working directory is the project root.
-
Confirmexists.
Dockerfile -
Confirm the env file exists.
-
Confirm Docker is installed and running.
-
Check whether the requested image tag already exists locally.
-
Check whether the requested container name already exists.
-
Never print secrets from the env file.
-
Do not use thetag unless the user explicitly asks.
latest -
Do not run destructive cleanup commands such as:
docker system prunedocker image prunedocker volume prune
-
Do not stop, remove, or replace an existing container without explicit confirmation from the user.
-
Do not rebuild an existing version tag unless the user clearly confirms.
运行任何构建或运行命令前:
-
确认当前工作目录为项目根目录。
-
确认存在。
Dockerfile -
确认环境文件存在。
-
确认Docker已安装并正在运行。
-
检查请求的镜像标签是否已在本地存在。
-
检查请求的容器名称是否已存在。
-
绝不要打印环境文件中的机密信息。
-
除非用户明确要求,否则不要使用标签。
latest -
不要运行破坏性的清理命令,例如:
docker system prunedocker image prunedocker volume prune
-
未经用户明确确认,不要停止、移除或替换现有容器。
-
除非用户明确确认,否则不要重新构建已存在的版本标签。
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 is missing, stop and report the issue.
DockerfileIf 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.jsonbash
node -p "require('./package.json').name"If does not exist or has no , use the current directory name:
package.jsonnamebash
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.jsonbash
node -p "require('./package.json').name"如果不存在或没有字段,使用当前目录名称:
package.jsonnamebash
basename "$PWD"要手动标准化项目名称,使用小写短横线命名法(kebab-case)。
示例:
text
My Next App -> my-next-app
nextjs_skill_app -> nextjs-skill-appVersion 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:
- Do not run .
docker build - Report that the version already exists locally.
- Suggest the next patch version, such as .
1.0.1 - 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}}"如果请求的镜像标签已存在:
- 不要运行。
docker build - 报告该版本已在本地存在。
- 建议使用下一个补丁版本,例如。
1.0.1 - 询问用户是否要使用新版本或明确重新构建相同标签。
仅当用户明确确认时,才重新构建现有标签。
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:
- Do not run another container with the same name.
- Report the existing container status.
- Ask the user whether to stop/remove it or use a different container name.
- Do not remove it automatically.
运行容器前,检查容器名称是否已存在:
bash
docker ps -a --filter "name=^/${CONTAINER_NAME}$" \
--format "table {{.Names}}\t{{.Image}}\t{{.Status}}\t{{.Ports}}"如果同名容器已存在:
- 不要运行另一个同名容器。
- 报告现有容器的状态。
- 询问用户是否要停止/移除它,或使用不同的容器名称。
- 不要自动移除它。
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:
- Container status should be .
Up - Port mapping should include .
${HOST_PORT}->${CONTAINER_PORT} - Logs should not show fatal errors.
- should return an HTTP response such as
curl -I,200,301,302, or307.308
If fails, inspect logs and report the likely cause.
curl运行容器后,通过以下命令验证:
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}"预期结果:
- 容器状态应为。
Up - 端口映射应包含。
${HOST_PORT}->${CONTAINER_PORT} - 日志不应显示致命错误。
- 应返回HTTP响应,例如
curl -I、200、301、302或307。308
如果失败,检查日志并报告可能的原因。
curlCommon 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_PORTEnv 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错误或端口冲突导致流程停止,请明确说明阻塞问题和最安全的下一步命令。