Kiểm tra Dockerfile API
Lint Dockerfile: image gốc không pin hoặc :latest, chạy root, secret trong ENV / ARG, ADD URL, cache apt / apk / pip, curl | sh, lỗi stage và cú pháp.
Gửi Dockerfile (văn bản, file hoặc body thô) và nhận danh sách lỗi kèm số dòng, mức độ (error / warning / info), cách sửa ngắn và điểm 0-100. Bộ phân tích hiểu dòng nối tiếp, chỉ thị # escape=, heredoc của BuildKit, dạng JSON và dạng shell, build nhiều stage.
Các kiểm tra gồm: image gốc không có tag hoặc dùng :latest (yêu cầu pin digest khi strict=1), stage cuối không có USER hoặc USER root, secret ghi thẳng trong ENV / ARG và các dạng token phổ biến ở bất kỳ đâu, ADD từ URL không có --checksum, ADD cho file cục bộ, apt-get thiếu -y, --no-install-recommends, update cùng layer hoặc xoá danh sách gói, apk add thiếu --no-cache, yum / dnf clean, pip --no-cache-dir, gói không pin version, npm install thay vì npm ci, curl | sh, thiếu pipefail, cd thay vì WORKDIR, sudo, chmod 777, COPY . . trước khi cài thư viện (mất cache), nhiều CMD / ENTRYPOINT, CMD dạng shell, EXPOSE sai, tên stage lạ hoặc trùng trong COPY --from, MAINTAINER, thiếu HEALTHCHECK.
Quy tắc do chúng tôi tự viết (không dùng hadolint vì giấy phép GPL; không có ShellCheck nên lệnh RUN được kiểm tra theo heuristic). Lọc bằng ignore (mã quy tắc) và min_severity. Kết quả liệt kê image gốc để tra hạn hỗ trợ với /v1/eol-check; manifest Kubernetes dùng /v1/k8s-validate, workflow CI dùng /v1/ci-validate.
Markdown · OpenAPI · Postman · llms.txt
Tham số
| Tên | Vị trí | Kiểu | Bắt buộc | Mặc định | |
|---|---|---|---|---|---|
dockerfile | body | string | có | Nội dung Dockerfile (hoặc upload file / body văn bản; tối đa 500 KB). | |
strict | body|query | boolean | không | Yêu cầu thêm pin image gốc bằng digest. | |
ignore | body|query | string | không | Mã quy tắc bỏ qua, cách nhau dấu phẩy, ví dụ healthcheck-missing,apt-pin. | |
min_severity | body|query | string | không | info | error, warning hoặc info. |
Ví dụ
curl -X POST "https://apibox.io.vn/v1/dockerfile-lint" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"dockerfile":"FROM node:latest\nWORKDIR /app\nCOPY . .\nRUN npm install\nENV API_TOKEN=s3cr3t-value\nEXPOSE 3000\nCMD node server.js\n"}'const res = await fetch('https://apibox.io.vn/v1/dockerfile-lint', {
method: 'POST',
headers: {
Authorization: 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"dockerfile": "FROM node:latest\nWORKDIR /app\nCOPY . .\nRUN npm install\nENV API_TOKEN=s3cr3t-value\nEXPOSE 3000\nCMD node server.js\n"
}),
});
const data = await res.json();
console.log(data);import requests
res = requests.post(
"https://apibox.io.vn/v1/dockerfile-lint",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"dockerfile": "FROM node:latest\nWORKDIR /app\nCOPY . .\nRUN npm install\nENV API_TOKEN=s3cr3t-value\nEXPOSE 3000\nCMD node server.js\n"},
timeout=30,
)
print(res.json())<?php
$ch = curl_init('https://apibox.io.vn/v1/dockerfile-lint');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => ['Authorization: Bearer YOUR_API_KEY', 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => '{"dockerfile":"FROM node:latest\\nWORKDIR /app\\nCOPY . .\\nRUN npm install\\nENV API_TOKEN=s3cr3t-value\\nEXPOSE 3000\\nCMD node server.js\\n"}',
]);
$response = curl_exec($ch);
print_r(json_decode($response, true));Kết quả trả về
{
"valid": false,
"errors": 1,
"warnings": 2,
"info": 4,
"score": 71,
"instructions": 7,
"stages": [
{
"index": 0,
"name": null,
"base_image": "node:latest",
"line": 1,
"user": null
}
],
"base_images": [
"node:latest"
],
"findings": [
{
"rule": "from-latest",
"severity": "warning",
"line": 1,
"message": "Base image \"node:latest\" uses :latest, which changes without notice; pin a version",
"fix": "FROM node:<version>"
},
{
"rule": "user-missing",
"severity": "warning",
"line": 1,
"message": "The final stage has no USER: the container runs as root",
"fix": "RUN useradd -r -u 10001 app (or adduser -D app on Alpine) and USER app before CMD"
},
{
"rule": "healthcheck-missing",
"severity": "info",
"line": 1,
"message": "No HEALTHCHECK in the final stage (fine on Kubernetes, which uses probes instead)"
},
{
"rule": "copy-all-before-install",
"severity": "info",
"line": 3,
"message": "COPY . before installing dependencies: every source change invalidates the dependency layer cache; copy the manifest / lockfile first, install, then copy the rest"
},
{
"rule": "npm-ci",
"severity": "info",
"line": 4,
"message": "Use npm ci (exact lockfile install) instead of npm install in images"
},
{
"rule": "secret-in-env",
"severity": "error",
"line": 5,
"message": "ENV API_TOKEN holds a literal secret: it is stored in the image layers / history",
"fix": "Use a BuildKit secret: RUN --mount=type=secret,id=api_token …, or inject it at run time"
},
{
"rule": "cmd-shell-form",
"severity": "info",
"line": 7,
"message": "CMD in shell form runs under /bin/sh -c: the process is not PID 1 and does not receive SIGTERM; use the JSON (exec) form",
"fix": "CMD [\"node\",\"server.js\"]"
}
],
"note": "Own rules (no hadolint / ShellCheck code). Check base images for end-of-life with /v1/eol-check and Kubernetes manifests with /v1/k8s-validate."
}
Ứng dụng
- Cho job CI thất bại khi Dockerfile chạy root hoặc lộ secret
- Bot review Dockerfile trong pull request
- Hướng dẫn đội ngũ thực hành tốt khi build image, kèm cách sửa cụ thể
- Rà soát hàng loạt Dockerfile của nhiều repo
Hỏi đáp
Đây có phải hadolint không?
Không. Quy tắc được viết mới hoàn toàn (hadolint là GPL-3.0); bao phủ các kiểm tra phổ biến kiểu hadolint, thêm phát hiện secret và gợi ý thứ tự cache.
Lệnh RUN có được phân tích không?
Có, theo heuristic cho trình quản lý gói, tải file, pipe, cd, sudo và quyền file; không phân tích shell đầy đủ như ShellCheck.
Tắt một quy tắc được không?
Được: truyền mã quy tắc vào ignore (cách nhau dấu phẩy) hoặc nâng min_severity lên warning / error.
API liên quan
Tạo UUID
Sinh tối đa 1.000 UUID v4, UUID v7 theo thời gian, ULID, Nano ID hoặc mã hex ngẫu nhiên mỗi request. Ngẫu nhiên an toàn, hỗ trợ chữ in hoa.
⏱Phân tích biểu thức cron
Kiểm tra biểu thức cron và xem các lần chạy tiếp theo theo múi giờ bất kỳ, kèm mô tả dễ hiểu. Hỗ trợ cú pháp cron 5, 6 và 7 trường.
🕒Giờ & múi giờ
Lấy giờ hiện tại ở mọi múi giờ, đổi giữa các múi giờ và Unix timestamp (giây hoặc mili giây), kèm độ lệch UTC và thứ trong tuần cho tối đa 30 múi giờ.