dockerfile
SolidDockerfile 编写最佳实践
Data & Documents 1 stars
0 forks Updated today Apache-2.0
Install
Quality Score: 79/100
Stars 20%
Recency 20%
Frontmatter 20%
Documentation 15%
Issue Health 10%
License 10%
Description 5%
Skill Content
# Dockerfile 编写
## 概述
Dockerfile 最佳实践、安全扫描等技能。
## 指令详解
### FROM
```dockerfile
# 基础镜像
FROM ubuntu:22.04
FROM node:18-alpine
FROM python:3.11-slim
# 多阶段构建
FROM node:18 AS builder
FROM nginx:alpine AS production
# 使用 ARG 动态指定
ARG BASE_IMAGE=node:18-alpine
FROM ${BASE_IMAGE}
```
### WORKDIR
```dockerfile
# 设置工作目录(推荐使用绝对路径)
WORKDIR /app
WORKDIR /home/node/app
# 多次使用会切换目录
WORKDIR /app
WORKDIR src
# 当前目录: /app/src
```
### COPY 与 ADD
```dockerfile
# COPY(推荐)
COPY package.json ./
COPY src/ ./src/
COPY --chown=node:node . .
# 多文件复制
COPY package.json package-lock.json ./
# ADD(支持 URL 和解压)
ADD https://example.com/file.tar.gz /app/
ADD archive.tar.gz /app/ # 自动解压
# 推荐:优先使用 COPY,除非需要 ADD 的特殊功能
```
### RUN
```dockerfile
# Shell 形式
RUN apt-get update && apt-get install -y curl
# Exec 形式
RUN ["apt-get", "update"]
# 最佳实践:合并命令减少层数
RUN apt-get update && \
apt-get install -y --no-install-recommends \
curl \
wget \
git && \
rm -rf /var/lib/apt/lists/*
```
### CMD 与 ENTRYPOINT
```dockerfile
# CMD - 默认命令(可被覆盖)
CMD ["node", "app.js"]
CMD ["npm", "start"]
# ENTRYPOINT - 入口点(不易被覆盖)
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["postgres"]
# 组合使用
ENTRYPOINT ["python"]
CMD ["app.py"]
# 运行: python app.py
# docker run myimage other.py -> python other.py
```
### ENV 与 ARG
```dockerfile
# ENV - 运行时环境变量
ENV NODE_ENV=production
ENV PORT=3000 HOST=0.0.0.0
# ARG - 构建时参数
ARG VERSION=1.0
ARG BUILD_DATE
# ARG 转 ENV
ARG APP_VERSION
ENV APP_VERSION=${APP_VERSIO...
Details
- Author
- ryukyagamilight
- Repository
- ryukyagamilight/terminal-skills
- Created
- 5 months ago
- Last Updated
- today
- Language
- N/A
- License
- Apache-2.0
Integrates with
Similar Skills
Semantically similar based on skill content — not just same category
Data & Documents Listed
dockerfile
Use when writing or editing a Dockerfile/Containerfile (or any container image build) — covers cache-friendly layer ordering and common gotchas.
0 Updated yesterday
mickzijdel Data & Documents Listed
dockerfile
Generate an optimized Dockerfile — multi-stage build, security hardening, small image size.
7 Updated 3 days ago
KhaiTrang1995 DevOps & Infrastructure Listed
docker-patterns
Docker and container best practices for writing efficient, secure, production-ready images. Use this skill whenever you are writing or editing a Dockerfile or docker-compose.yml, configuring devcontainers, debugging a failing container build, shrinking image size, speeding up build times, or hardening a container against security vulnerabilities. If Docker is in scope, load this skill first.
0 Updated yesterday
sardonyx0827