Skip to content
devops2026-07-253 min read

What Is a Dockerfile

A Dockerfile is a text file containing a series of instructions for building a Docker image. The Docker engine builds the image layer by layer following the instructions.

Basic Structure

# Base image
FROM node:18-alpine

# Working directory
WORKDIR /app

# Copy dependency files
COPY package*.json ./

# Install dependencies
RUN npm ci --production

# Copy source code
COPY . .

# Expose port
EXPOSE 3000

# Start command
CMD ["npm", "start"]

Why You Need a Dockerfile Generator

  1. Lower learning curve: No need to memorize all instruction syntax
  2. Reduce config errors: Auto-generate best practice configurations
  3. Faster containerization: Quickly create Docker configs for projects
  4. Standardize conventions: Team uses unified Dockerfile templates
  5. Multi-language support: Generate appropriate configs for different stacks
  6. Built-in best practices: Auto-apply security and performance optimizations

Dockerfile Common Instructions

| Instruction | Description | Example | |-------------|-------------|---------| | FROM | Base image | FROM node:18-alpine | | WORKDIR | Set working directory | WORKDIR /app | | COPY | Copy files to image | COPY . . | | RUN | Execute command | RUN npm install | | EXPOSE | Declare port | EXPOSE 3000 | | CMD | Container start command | CMD ["npm", "start"] | | ENV | Set environment variable | ENV NODE_ENV=production | | ARG | Build argument | ARG VERSION=1.0 | | ENTRYPOINT | Entry point | ENTRYPOINT ["node"] | | ADD | Copy and extract | ADD archive.tar.gz /app |

Multi-Stage Builds

Multi-stage builds significantly reduce final image size:

# Stage 1: Build
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

# Stage 2: Production
FROM node:18-alpine AS production
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]

Image Optimization Tips

| Tip | Description | Effect | |-----|-------------|--------| | Use Alpine base | alpine vs ubuntu | Image: ~100MB → ~5MB | | Multi-stage builds | Separate build and runtime | Remove build tools | | .dockerignore | Exclude unnecessary files | Reduce build context | | Reduce layers | Merge RUN commands | Fewer image layers | | Leverage cache | COPY deps first | Faster rebuilds | | Pin versions | node:18.19.0 vs node:latest | Reproducible builds |

FAQ

What's the Relationship Between Dockerfile and docker-compose?

A Dockerfile defines how to build a single image. docker-compose defines how to orchestrate multi-container applications. A docker-compose file can reference images built from multiple Dockerfiles.

Why Is My Image So Large?

Common causes: using full base images (e.g., node instead of node-alpine), not using multi-stage builds, COPYing node_modules and other large files. Use docker images to check sizes and optimize incrementally.

How to Store Passwords and Secrets Safely?

Use Docker Secrets or environment variables, never hardcode in Dockerfile. Production: use docker secret or Kubernetes Secrets. Development: use .env files (never commit to Git).


This article is brought to you by DevToolkit Pro. More developer tools at the homepage.


ad