-
Notifications
You must be signed in to change notification settings - Fork 0
Dockerfile Guide
PotatoScript edited this page Feb 16, 2025
·
1 revision
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.
# 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>"]- Specifies the base image.
- Example:
FROM node:16
- Sets the working directory for the container.
- Example:
WORKDIR /usr/src/app
- Copies files from the host system to the container.
- Example:
COPY package.json ./
- Executes commands during the build process (e.g., installing dependencies).
- Example:
RUN apt-get update && apt-get install -y python3
- Exposes a port to be used by the container.
- Example:
EXPOSE 3000
- Specifies the default command to run when the container starts.
- Example:
CMD ["npm", "start"]
- Similar to
CMDbut used to configure the container as an executable. - Example:
ENTRYPOINT ["python3", "app.py"]
- Sets environment variables.
- Example:
ENV NODE_ENV=production
- Adds metadata to the image.
- Example:
LABEL version="1.0" description="My Dockerized App"
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;"]-
Minimize Image Layers: Combine multiple
RUNinstructions to reduce image layers.RUN apt-get update && apt-get install -y \ curl \ git && \ apt-get clean - Use Official Base Images: Always use well-maintained and official base images for security and reliability.
- Specify Exact Versions: Pin dependencies to specific versions to ensure consistency.
-
Use
.dockerignore: Exclude unnecessary files from the build context to reduce the image size. Example.dockerignore:node_modules .git *.log - Leverage Multi-Stage Builds: Optimize image size by separating build and runtime stages.
-
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/*
# 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 the image:
docker build -t my-app . - Run the container:
docker run -d -p 3000:3000 my-app