Master Dockerfiles: The Ultimate Beginner's Guide to Creating Container Images

Anusha KothaAnusha Kotha
3 min read

Containers have changed how we build, ship, and run applications. At the heart of every container is a Dockerfile — a simple text file that tells Docker how to build an image.

In this blog, I’ll walk you through Dockerfiles with simple explanations and real examples. By the end, you'll know how to create your own Docker images with ease. Let’s dive in! 💡

🌟 What is a Dockerfile?

A Dockerfile is a plain text file with step-by-step instructions that Docker follows to build an image. Each instruction adds a layer to the image. This file automates the image creation process.

🧱 Common Dockerfile Instructions (With Simple Examples)

1. FROM (Base Image)

Sets the starting point for your image.

Example:

FROM ubuntu:20.04

2. COPY (Copy Files)

Copies files from your system to the container.

Example:

COPY index.html .

3. ADD (Copy + More)

Like COPY, but can also:

  • Extract .tar files.

  • Download files from a URL.

Example:

ADD app.tar.gz /app/
ADD https://example.com/config.json /app/

4. WORKDIR (Working Directory)

  • Sets the default folder for following commands.

  • Example:

WORKDIR /usr/src/app

5. RUN (Run Commands During Build)

  • Runs commands when building the image.

  • Example:

RUN apt update -y && apt install -y python3

6. CMD (Default Command)

  • Runs when the container starts.

  • Can be overridden.

  • Example:

CMD ["nginx", "-g", "daemon off;"]

7. ENTRYPOINT (Main Command)

  • Similar to CMD, but not overridden easily.

  • Example:

ENTRYPOINT ["python3", "app.py"]

8. ARG (Build-Time Variables)

  • Used during the image build, not available in the container.

  • Example:

ARG VERSION=1.0
RUN echo "Version: $VERSION"

9. ENV (Environment Variables)

  • Available inside the running container.

  • Example:

ENV APP_ENV=production

10. USER (Set User)

  • Runs processes as a specific user.

  • Example:

USER 1001

11. EXPOSE (Port Declaration)

  • Declares the port the container listens on.

  • Example:

EXPOSE 80

12. LABEL (Metadata)

  • Adds info like author or description.

  • Example:

LABEL maintainer="devops@example.com" description="NGINX sample image"

13. MAINTAINER (Deprecated, Use LABEL Instead)

  • Example:
MAINTAINER devops@example.com

📝 Real-Life Example: Dockerfile for a Python Flask App

FROM python:3.9-slim
LABEL maintainer="user@example.com" description="Simple Flask App"
WORKDIR /app
COPY requirements.txt /app/
RUN pip install -r requirements.txt
COPY . /app/
EXPOSE 5000
ENV FLASK_ENV=production
CMD ["python", "app.py"]

What’s Happening Here:

  • Using Python 3.9 base image.

  • Setting working directory to /app.

  • Installing Python dependencies.

  • Copying app code into the container.

  • Exposing port 5000.

  • Setting an environment variable.

  • Running app.py when the container starts.

🎯 Conclusion

A Dockerfile is your blueprint to build custom container images. Each instruction has a purpose:

  • FROM, COPY, ADD build your image foundation.

  • RUN, CMD, ENTRYPOINT define how it behaves.

  • ENV, ARG, USER configure the environment.

  • EXPOSE, LABEL document important details.

Mastering Dockerfiles is a must-have skill for every DevOps Engineer!

💖 Show Some Love!

If this blog helped you in learning Docker or preparing for interviews, please tap the ❤️ button and leave a comment below! Your support motivates me to share more DevOps content.

Happy Learning! 🚀

0
Subscribe to my newsletter

Read articles from Anusha Kotha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anusha Kotha
Anusha Kotha