What is a Dockerfile?
A Dockerfile is a text file containing instructions that Docker uses to automatically build a container image.
Key Topics
- • Image instructions
- • Build process
- • Container images
- • Declarative configuration
Learn how to create Dockerfiles and build custom Docker images. Understand base images, build instructions, application setup, environment variables, ports, startup commands, and multi-stage builds.
Dockerfile Learning Path
Dockerfile Fundamentals
Base Images
COPY & RUN
Environment Configuration
CMD & ENTRYPOINT
Multi-Stage Builds
Introduction
A Dockerfile is a text file containing instructions that Docker uses to automatically build a container image.
Each instruction defines part of the image build process, such as selecting a base image, copying application files, installing dependencies, configuring the environment, and defining the default command.
Docker processes Dockerfile instructions in order to create an image that can later be used to run containers.
Core Concepts
Learn the essential concepts required to create, understand, and maintain Dockerfiles.
A Dockerfile is a text file containing instructions that Docker uses to automatically build a container image.
Key Topics
The FROM instruction defines the base image that provides the starting environment for the container image.
Key Topics
Dockerfiles use instructions to copy files, install dependencies, configure environments, and define application startup behavior.
Key Topics
CMD and ENTRYPOINT define how containers created from an image start and which commands are executed.
Key Topics
The build context contains the files and directories available to the Docker build process.
Key Topics
Multiple FROM instructions can create separate build stages and help copy only required application artifacts into a final image.
Key Topics
Dockerfile Instructions
Dockerfiles support instructions for defining the base image, copying files, running build commands, setting environment variables, configuring ports, and defining container startup behavior.
FROMSets the base image and starts a new build stage.
WORKDIRSets the working directory for subsequent instructions.
COPYCopies files and directories from the build context into the image.
RUNExecutes commands during the image build process.
ENVSets environment variables for subsequent instructions and the container environment.
EXPOSEDocuments the network port that the application expects to use.
CMDDefines the default command or arguments used when a container starts.
ENTRYPOINTSpecifies the default executable for a container.
Practical Example
This example demonstrates a simple Dockerfile for a Node.js application. It selects a base image, defines a working directory, copies package files, installs dependencies, copies application files, documents the application port, and defines the startup command.
The exact Dockerfile for your project may differ depending on your programming language, framework, dependencies, and deployment requirements.
Example Dockerfile
FROM node:22-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["npm", "start"]
Building Images
Use Docker commands to build images from Dockerfiles, assign image names and tags, inspect images, and run containers.
docker build -t my-app .Build a Docker image from the Dockerfile in the current directory.
docker build -t my-app:v1 .Build an image and assign a specific tag.
docker build -f Dockerfile.dev -t my-app .Build an image using a specific Dockerfile.
docker image lsList Docker images available on the local system.
docker run my-appCreate and start a container using the built image.
docker image inspect my-appDisplay detailed metadata and configuration information about an image.
Advanced Concept
A Dockerfile can contain multiple FROM instructions. Each FROM instruction starts a new build stage.
Files can be copied from one stage into another usingCOPY --fromwhich is useful when separating build tools from the final runtime image.
Multi-Stage Example
FROM node:22 AS build WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build FROM node:22-alpine WORKDIR /app COPY --from=build /app ./ CMD ["npm", "start"]
Learning Roadmap
Follow this step-by-step roadmap to build practical Dockerfile and container image skills.
Learn how Docker images provide the foundation for creating containers.
Understand how Dockerfile instructions are written and processed during image builds.
Use the FROM instruction to select an appropriate starting image.
Use COPY to include your application source code and required files.
Use RUN instructions to install packages and prepare the application environment.
Build your Docker image, run a container, and verify that the application works correctly.
Continue Learning
Next Step
Practice writing Dockerfiles, building images, running containers, and continue learning Docker Compose, Kubernetes, and DevOps tools.