Hereโs a step-by-step tutorial for Dockerfile, including explanations and examples for each major command.
Dockerfile Tutorial
A Dockerfile is a text file containing instructions to build a Docker image. Each instruction creates a new layer in the image. Below are the most commonly used Dockerfile commands, their descriptions, and usage examples.
1. FROM
Description:
Specifies the base image to use. This must be the first command in the Dockerfile.
Example:
FROM ubuntu:22.04
Code language: CSS (css)
This starts your build from the official Ubuntu 22.04 image.
2. MAINTAINER
Description:
Optionally defines the author or maintainer of the image (deprecated in favor of LABEL).
Example:
MAINTAINER Rajesh Kumar <rajesh@example.com>
Code language: HTML, XML (xml)
Modern alternative:
LABEL maintainer="Rajesh Kumar <rajesh@example.com>"
Code language: HTML, XML (xml)
3. ADD
Description:
Copies files/directories from the host into the image. Can also extract local tar archives.
Example:
ADD myapp.tar.gz /opt/
ADD config/settings.json /etc/myapp/
4. COPY
Description:
(Copy is very similar to ADD but doesnโt do auto-extraction or remote URLs; prefer COPY unless you need ADD features.)
Example:
COPY . /app/
5. RUN
Description:
Executes commands in a new layer during build, e.g., install packages.
Example:
RUN apt-get update && apt-get install -y nginx
RUN mkdir /app
Code language: JavaScript (javascript)
6. CMD
Description:
Sets the default command to run when a container is started from the image (can be overridden).
Example:
CMD ["nginx", "-g", "daemon off;"]
Code language: CSS (css)
7. ENTRYPOINT
Description:
Sets a command that will always run (even if arguments are passed to docker run).
Allows for combining with CMD for default args.
Example:
ENTRYPOINT ["python3", "app.py"]
CMD ["--help"]
Code language: CSS (css)
8. ENV
Description:
Sets environment variables in the image.
Example:
ENV APP_ENV=production
ENV PATH="/opt/myapp/bin:${PATH}"
Code language: JavaScript (javascript)
9. EXPOSE
Description:
Documents which port(s) the app inside the container uses (does not actually publish the port).
Example:
EXPOSE 80
EXPOSE 443
10. USER
Description:
Sets the default user to run commands as in the image.
Example:
USER www-data
11. VOLUME
Description:
Defines a mount point for external volumes or persistent data.
Example:
VOLUME ["/data"]
Code language: CSS (css)
12. WORKDIR
Description:
Sets the working directory for RUN, CMD, ENTRYPOINT, COPY, and ADD.
Example:
WORKDIR /app
13. ONBUILD
Description:
Adds a trigger instruction to be executed when the image is used as the base for another build.
Example:
ONBUILD COPY . /app/src
Useful for images intended to be used as parent images (like language or framework base images).
Example: Complete Dockerfile
FROM python:3.11-slim
LABEL maintainer="Rajesh Kumar <rajesh@example.com>"
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
ENV FLASK_ENV=production
EXPOSE 5000
CMD ["python", "app.py"]
Code language: JavaScript (javascript)
How to Build & Run
- Build the image:
docker build -t my-flask-app . - Run the container:
docker run -p 5000:5000 my-flask-app
Quick Reference Table
| Command | Description |
|---|---|
| FROM | Sets base image (first line) |
| MAINTAINER | Maintainer info (deprecated, use LABEL) |
| LABEL | Set metadata info |
| ADD | Copy files/dirs & extract archives |
| COPY | Copy files/dirs (no extract) |
| RUN | Execute commands at build time |
| CMD | Default command at container start |
| ENTRYPOINT | Main entry command (always runs) |
| ENV | Set environment variables |
| EXPOSE | Declare ports to be exposed |
| USER | Set default user |
| VOLUME | Define mount point for volumes |
| WORKDIR | Set default working directory |
| ONBUILD | Add trigger instructions for child builds |
Summary
- Use
FROMas the first command. - Use
COPYfor simple file copy,ADDif you need to extract archives. - Use
RUNfor installing dependencies or system setup. - Use
WORKDIRto organize working directories. - Use
CMDorENTRYPOINTfor startup commands. - Use
ENV,EXPOSE,USER,VOLUMEas needed for best practices.
Example
Absolutely! Hereโs a beginner-friendly Dockerfile using the Ubuntu image, demonstrating all key Dockerfile instructions. This example will build and run successfully. It will output a simple message showing which instructions were used and that everything works.
Directory Structure
Put these two files in your build directory:
.
โโโ Dockerfile
โโโ hello.txt
โโโ script.sh
Code language: CSS (css)
hello.txtโ a simple text file (echo "Hello, this is a demo file." > hello.txt)script.shโ a script to show everything works:#!/bin/bash echo "======== Script Running ========" echo "Hello from inside the container!" echo "This file was copied using COPY." echo "Argument to script: $1" echo "APP_HOME is set to: $APP_HOME" echo "Now displaying contents of /opt/demoapp/hello.txt:" cat /opt/demoapp/hello.txt echo "Listing the shared volume directory:" ls /opt/demoapp/data echo "======== End of Script ========"
Make script executable:chmod +x script.sh
Dockerfile (All Instructions Used, Clean & Ready for Students)
# 1. FROM - Always first
FROM ubuntu:22.04
# 2. LABEL (modern replacement for MAINTAINER)
LABEL maintainer="Student <student@example.com>"
# 3. ENV - Set environment variables
ENV APP_HOME=/opt/demoapp \
APP_ENV=development
# 4. ADD - Add a file (could be used for local archives too)
ADD hello.txt /opt/demoapp/hello.txt
# 5. COPY - Copy your script into the container
COPY script.sh /opt/demoapp/script.sh
# 6. RUN - Install a package and prepare directories
RUN apt-get update && \
apt-get install -y tree && \
mkdir -p /opt/demoapp/data
# 7. WORKDIR - Set the working directory
WORKDIR /opt/demoapp
# 8. VOLUME - Create a mount point for data (persists data outside container)
VOLUME ["/opt/demoapp/data"]
# 9. EXPOSE - Document the port (not used by this demo, but included)
EXPOSE 8080
# 10. USER - Create and switch to a non-root user (safer for students)
RUN useradd -ms /bin/bash studentuser
USER studentuser
# 11. ENTRYPOINT - Script that always runs
ENTRYPOINT ["/bin/bash", "/opt/demoapp/script.sh"]
# 12. CMD - Default argument to the script
CMD ["from-cmd"]
# 13. ONBUILD - Will only trigger if this image is used as a base in another Dockerfile
ONBUILD RUN echo "This message runs only if someone builds FROM this image!"
# END OF DOCKERFILE
Code language: PHP (php)
How to Build and Run
# Make sure script.sh is executable
chmod +x script.sh
# Build the image (from the directory containing Dockerfile, script.sh, hello.txt)
docker build -t ubuntu-dockerfile-demo .
# Run the container
docker run --rm -v $(pwd)/shared:/opt/demoapp/data ubuntu-dockerfile-demo
Code language: PHP (php)
- This will output the script message, read the file added with
ADD, show the shared volume, and display all environment variable usage.
What Students Learn
- How to use each main Dockerfile instruction
- How to create a non-root user for better practice
- How to mount a volume and see files persist
- How to pass and use environment variables
- How to run a script with CMD arguments
Reference
- https://www.devopsschool.com/blog/example-and-sample-programs-of-dockerfile/
- https://www.devopsschool.com/blog/understanding-dockerfiles-instructions-options-of-docker/
- https://www.devopsschool.com/blog/dockerfile-lab-exercise-1/
- https://www.devopsschool.com/blog/how-to-create-a-image-using-dockerfile/
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals
Great and easy-to-follow tutorial! The explanation of Dockerfile instructions and their options is clear and practical. Itโs a helpful resource for beginners who want to understand how Docker images are built and how different instructions work together.