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
FROM
as the first command. - Use
COPY
for simple file copy,ADD
if you need to extract archives. - Use
RUN
for installing dependencies or system setup. - Use
WORKDIR
to organize working directories. - Use
CMD
orENTRYPOINT
for startup commands. - Use
ENV
,EXPOSE
,USER
,VOLUME
as 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 a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at Cotocus. I share tech blog at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow , and SEO strategies at Wizbrand.
Do you want to learn Quantum Computing?
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at WIZBRAND