Learn
WebsiteLoginFree Trial
  • Incident Management
    • What is Incident Management? Beginner's Guide
    • Severity Levels
    • How to calculate MTTR and Other Common Incident Recovery Metrics
    • On-Call
    • SLA vs SLO vs SLI: What's The Difference?
    • Data Aggregation and Aggregators
  • DevOps
    • Best DevOps Tools for Each Phase of the DevOps Lifecycle
      • Best DevOps Planning Tools
      • Best DevOps Coding Tools
      • Best DevOps Build Tools
      • Best DevOps Testing Tools
      • Best DevOps Release Tools
      • Best DevOps Deployment Tools
      • Best DevOps Operations Tools
      • Best DevOps Monitroing Tools
    • What is DevOps?
      • Best CI/CD Tools
      • DevOps Infrastructure and Automation
      • What is a DevOps Pipeline?
      • DevOps Vs. Agile
      • Top 25 DevOps Interview Questions
      • What Are the Benefits of DevOps?
      • What is CI/CD?
      • What is a DevOps Engineer?
      • What is DevSecOps?
    • What is Observability?
      • USE and RED Method
    • What is Site Reliability Engineering (SRE)?
      • Four Golden Signals: SRE Monitoring
      • What is A Canary Deployment?
      • What is Blue-Green Deployment?
  • Docker
    • Overview
    • Dockerfile
    • Images
    • Containers
    • Storage
    • Network
    • Compose
    • Swarm
    • Resources
  • prometheus
    • Overview
    • Data Model
    • Metric Types
    • PromQL
      • Series Selection
      • Counter Rates & Increases
    • Pushgateway
    • Alertmanager
    • Remote Storage
Powered by GitBook
On this page
  • What is a Dockerfile?
  • Common Keywords and Instructions
  • Example Dockerfile
  • Building an Image From a Dockerfile
  • Best Practices for Dockerfile

Was this helpful?

  1. Docker

Dockerfile

A Dockerfile is the blueprint for building Docker images, providing a declarative and reproducible way to define the environment and dependencies for containerized applications.

PreviousOverviewNextImages

Last updated 10 months ago

Was this helpful?

What is a Dockerfile?

A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an . A Dockerfile adheres to a specific format and set of instructions, which you can find in the .

Common Keywords and Instructions

Dockerfile allows users to specify various commands to build the image and configure the environment. Some common instruction keywords are:

  • FROM - The FROM instruction creates a new build stage from a base image. It's usually one of the first lines in a Dockerfile.

  • WORKDIR -WORKDIR sets the working directory for any subsequent RUN, CMD, ENTRYPOINT, COPY, and ADD instructions in the Dockerfile. It simplifies file path references within the Dockerfile and improves readability.

  • COPY - The COPY instruction in Dockerfile copies files and directories from the host machine to the image filesystem. It is commonly used to add application code, configuration files, and dependencies to the image.

  • RUN - The RUN command in a Dockerfile is used to execute commands during the image build process. When you include a RUN instruction in your Dockerfile, Docker will execute the specified command within the container's filesystem at build time.

  • CMD - The CMD command in a Dockerfile is used to specify the default command to run when a container based on the image starts. Unlike the RUN command, which executes commands during the image build process, the CMD command sets the default command that will be executed when the container is launched.

  • EXPOSE - The EXPOSE keyword in a Dockerfile is used to document which ports a container listens on during runtime. It does not actually publish the port or make it accessible from outside the container. Instead, it serves as a form of documentation for developers, administrators, and container orchestration tools to understand which ports are intended to be used by the application running inside the container.

  • ENV -ENV sets environment variables within the container. Environment variables can be used to pass configuration settings, specify runtime parameters, or customize the behavior of applications running in the container.

A full list of instruction keywords can be found in the .

Example Dockerfile

The following example shows a Dockerfile that containerizes a application.

Dockerfile
# syntax=docker/dockerfile:1

FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
CMD ["node", "src/index.js"]
EXPOSE 3000

Building an Image From a Dockerfile

To build an image from a Dockerfile, use the docker build command followed by the path to the directory containing the Dockerfile. Docker builds the image layer by layer, executing each instruction in the Dockerfile and caching intermediate layers for faster subsequent builds.

Tagging Dockerfile builds provides a way to version and identify images, making managing and distributing them easier across different environments. Tags typically consist of an image name and version number or identifier.

docker build -t <image_name>:<tag> <path_to_Dockerfile_directory>

Best Practices for Dockerfile

  • Use Minimal Base Images: Start with a minimal base image to reduce image size and minimize dependencies.

  • Optimize Layers: Combine related commands into a single RUN instruction to reduce the number of layers and improve build performance.

  • Leverage Caching: Utilize layer caching to speed up build times by caching intermediate layers during subsequent builds.

  • Cleanup: Remove unnecessary files and dependencies after installing packages to reduce image size and improve security.

  • Security: Regularly update base images and dependencies to patch security vulnerabilities and ensure the integrity of the image.

The official Docker documentation provides for Dockerfiles.

image
Dockerfile reference
container
Dockerfile reference
NodeJS
extensive best practices