Turn Your Vehicle Into a Smart Earning Asset

While you’re not driving your car or bike, it can still be working for you. MOTOSHARE helps you earn passive income by connecting your vehicle with trusted renters in your city.

🚗 You set the rental price
🔐 Secure bookings with verified renters
📍 Track your vehicle with GPS integration
💰 Start earning within 48 hours

Join as a Partner Today

It’s simple, safe, and rewarding. Your vehicle. Your rules. Your earnings.

Docker Tutorials: Docker Image – Understanding Dockerfiles instructions & options

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

  1. Build the image: docker build -t my-flask-app .
  2. Run the container: docker run -p 5000:5000 my-flask-app

Quick Reference Table

CommandDescription
FROMSets base image (first line)
MAINTAINERMaintainer info (deprecated, use LABEL)
LABELSet metadata info
ADDCopy files/dirs & extract archives
COPYCopy files/dirs (no extract)
RUNExecute commands at build time
CMDDefault command at container start
ENTRYPOINTMain entry command (always runs)
ENVSet environment variables
EXPOSEDeclare ports to be exposed
USERSet default user
VOLUMEDefine mount point for volumes
WORKDIRSet default working directory
ONBUILDAdd 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 or ENTRYPOINT 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

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x