Skip to content

Dockerfile Guide

PotatoScript edited this page Feb 16, 2025 · 1 revision

Dockerfile Guide Cheat Sheet

Overview

A Dockerfile is a text document that contains all the instructions to assemble an image. It defines the environment, dependencies, and commands required to build a containerized application. Below are the essential components and commands for writing a professional Dockerfile.


Basic Dockerfile Structure

# Base image
FROM <base_image>:<tag>

# Maintainer information (optional)
LABEL maintainer="<your_email>"

# Set working directory inside the container
WORKDIR /app

# Copy files from the host to the container
COPY <source_path> <destination_path>

# Install dependencies
RUN <command_to_install_dependencies>

# Expose a port
EXPOSE <port_number>

# Define environment variables
ENV <key>=<value>

# Specify the default command to run when the container starts
CMD ["<executable>", "<arg1>", "<arg2>"]

# OR specify a command to build the image (use only one of CMD or ENTRYPOINT)
ENTRYPOINT ["<executable>", "<arg1>"]

Dockerfile Instructions

FROM

  • Specifies the base image.
  • Example:
    FROM node:16

WORKDIR

  • Sets the working directory for the container.
  • Example:
    WORKDIR /usr/src/app

COPY

  • Copies files from the host system to the container.
  • Example:
    COPY package.json ./

RUN

  • Executes commands during the build process (e.g., installing dependencies).
  • Example:
    RUN apt-get update && apt-get install -y python3

EXPOSE

  • Exposes a port to be used by the container.
  • Example:
    EXPOSE 3000

CMD

  • Specifies the default command to run when the container starts.
  • Example:
    CMD ["npm", "start"]

ENTRYPOINT

  • Similar to CMD but used to configure the container as an executable.
  • Example:
    ENTRYPOINT ["python3", "app.py"]

ENV

  • Sets environment variables.
  • Example:
    ENV NODE_ENV=production

LABEL

  • Adds metadata to the image.
  • Example:
    LABEL version="1.0" description="My Dockerized App"

Multi-Stage Build Example

Use multi-stage builds to reduce the size of the final image:

# Stage 1: Build the application
FROM node:16 AS builder
WORKDIR /app
COPY package.json ./
RUN npm install
COPY . ./
RUN npm run build

# Stage 2: Serve the application
FROM nginx:alpine
COPY --from=builder /app/build /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Best Practices for Writing Dockerfiles

  1. Minimize Image Layers: Combine multiple RUN instructions to reduce image layers.
    RUN apt-get update && apt-get install -y \
        curl \
        git && \
        apt-get clean
  2. Use Official Base Images: Always use well-maintained and official base images for security and reliability.
  3. Specify Exact Versions: Pin dependencies to specific versions to ensure consistency.
  4. Use .dockerignore: Exclude unnecessary files from the build context to reduce the image size. Example .dockerignore:
    node_modules
    .git
    *.log
    
  5. Leverage Multi-Stage Builds: Optimize image size by separating build and runtime stages.
  6. Keep It Clean: Remove unnecessary files or packages after installation.
    RUN apt-get update && apt-get install -y curl && \
        rm -rf /var/lib/apt/lists/*

Example Dockerfile for a Node.js App

# Use the official Node.js image
FROM node:16

# Set the working directory
WORKDIR /usr/src/app

# Copy package files and install dependencies
COPY package*.json ./
RUN npm install

# Copy the source code
COPY . .

# Expose the application port
EXPOSE 3000

# Define the default command
CMD ["npm", "start"]

Build and Run with Dockerfile

  1. Build the image:
    docker build -t my-app .
  2. Run the container:
    docker run -d -p 3000:3000 my-app

Clone this wiki locally