TechyPilots Info
TechyPilots.INFO
Home/Tutorials/Docker/Dockerfile
Docker & Containers

Dockerfile Tutorial

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.

Level: Beginner
Topics: 6 Core Areas

Dockerfile Learning Path

Essential Dockerfile Skills

Dockerfile Fundamentals

Base Images

COPY & RUN

Environment Configuration

CMD & ENTRYPOINT

Multi-Stage Builds

Introduction

What is a Dockerfile?

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

Dockerfile fundamentals

Learn the essential concepts required to create, understand, and maintain Dockerfiles.

01

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
02

Base Images

The FROM instruction defines the base image that provides the starting environment for the container image.

Key Topics

  • FROM instruction
  • Official images
  • Base operating systems
  • Runtime images
03

Build Instructions

Dockerfiles use instructions to copy files, install dependencies, configure environments, and define application startup behavior.

Key Topics

  • RUN
  • COPY
  • WORKDIR
  • ENV
04

Application Startup

CMD and ENTRYPOINT define how containers created from an image start and which commands are executed.

Key Topics

  • CMD
  • ENTRYPOINT
  • Shell form
  • Exec form
05

Build Context

The build context contains the files and directories available to the Docker build process.

Key Topics

  • Build context
  • .dockerignore
  • COPY
  • Build files
06

Multi-Stage Builds

Multiple FROM instructions can create separate build stages and help copy only required application artifacts into a final image.

Key Topics

  • Build stages
  • AS aliases
  • COPY --from
  • Smaller images

Dockerfile Instructions

Common 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.

FROM

Sets the base image and starts a new build stage.

WORKDIR

Sets the working directory for subsequent instructions.

COPY

Copies files and directories from the build context into the image.

RUN

Executes commands during the image build process.

ENV

Sets environment variables for subsequent instructions and the container environment.

EXPOSE

Documents the network port that the application expects to use.

CMD

Defines the default command or arguments used when a container starts.

ENTRYPOINT

Specifies the default executable for a container.

Practical Example

Create your first Dockerfile

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

Essential Docker build commands

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 ls

List Docker images available on the local system.

docker run my-app

Create and start a container using the built image.

docker image inspect my-app

Display detailed metadata and configuration information about an image.

Advanced Concept

Multi-stage Docker builds

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

How to learn Dockerfiles

Follow this step-by-step roadmap to build practical Dockerfile and container image skills.

Step 1

Understand Docker Images

Learn how Docker images provide the foundation for creating containers.

Step 2

Learn Dockerfile Syntax

Understand how Dockerfile instructions are written and processed during image builds.

Step 3

Choose a Base Image

Use the FROM instruction to select an appropriate starting image.

Step 4

Copy Application Files

Use COPY to include your application source code and required files.

Step 5

Install Dependencies

Use RUN instructions to install packages and prepare the application environment.

Step 6

Build and Test

Build your Docker image, run a container, and verify that the application works correctly.

Next Step

Start building Docker images

Practice writing Dockerfiles, building images, running containers, and continue learning Docker Compose, Kubernetes, and DevOps tools.