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.

GETPOSThttps://apibox.io.vn/v1/dockerfile-lint
Tiện ích lập trình▶ Dùng thử online

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ênVị tríKiểuBắt buộcMặc định
dockerfilebodystringcóNội dung Dockerfile (hoặc upload file / body văn bản; tối đa 500 KB).
strictbody|querybooleankhôngYêu cầu thêm pin image gốc bằng digest.
ignorebody|querystringkhôngMã quy tắc bỏ qua, cách nhau dấu phẩy, ví dụ healthcheck-missing,apt-pin.
min_severitybody|querystringkhônginfoerror, 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"}'

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

Bắt đầu với 1.000 request miễn phí mỗi tháng. Nhận API key miễn phí →