Decoding Docker ๐Ÿ‹ : Breakdown Docker File๐Ÿ“

15 Apr, 2025 ยท 8 min read

Decoding Docker ๐Ÿ‹ : Breakdown Docker File๐Ÿ“
"Learn Docker on AWS EC2 Instance - Ubuntu"

๐Ÿ‘‰ Quick Intro ๐Ÿ‘€

Yo devs!

  • So If you havenโ€™t yet messed with Docker basics - Images, Containers, Networks and Volumes, check out this cool intro I wrote
  • Else now you must be wondering โ€œHow do I build my own image for my project?โ€
  • Well, the answer is simple: Dockerfile.
  • Letโ€™s break it down fully - all in proper detail, but in our Indian style. No boring gyaan here! ๐Ÿ˜œ

๐Ÿ‘‰ What is Dockerfile ๐Ÿ—’๏ธ

  • It is basically a recipe (a text file) with step-by-step instructions for Docker on how to build your custom image for your project.
  • Think of it as setting up a new laptop from scratchโ€”installing necessary applications and libraries, pulling your code, running the code, and more. A Dockerfile automates all of that for you.
  • Its like saying โ€œOye Docker, yeh steps follow kar aur meri image bana de!โ€ ๐Ÿ’ช

๐Ÿ‘‰ File Structure & Format ๐Ÿข

  • A Dockerfile is packed with multiple instructions, each lined up one after other for different tasks! ๐ŸŽ‰
  • The first and must-have instruction is FROM (we'll dive into this in the sections below).
  • The correct format for writing an instruction is shown below:
1# Comment (describe the purpose)
2COMMAND arguments
  • The COMMAND doesn't have to be in uppercase, but it's a common practice to make it stand out from the arguments.
  • Here's a basic Dockerfile example for a Node.js app, just to give you a sneak peek of what it looks like! Get ready to dive deeper into it in the sections below โ€” it's going to be awesome! ๐Ÿ˜ƒ
1# Use Node.js version 18 as the base image
2FROM node:18
3
4# Set the working directory inside the container to /app
5WORKDIR /app
6
7# Copy all files from current directory on host to working directory in container
8COPY . .
9
10# Install dependencies using npm
11RUN npm install
12
13# Command to run when the container starts
14CMD [\"node\", \"index.js\"]

๐Ÿ‘‰ Image Building ๐Ÿ—๏ธ

  1. Build Context

    • To build your Docker image, just run the below command, docker build -t <image_name> <dir> and make sure you point it to the directory where your Dockerfile is hanging out.
    • The .dockerignore file comes in handy when you want to skip over some files in your Docker image context directory.


  2. Layers ๐Ÿ—‚๏ธ

    • Each instruction in a Dockerfile adds a new layer. It's like stacking blocks: each step makes a container, runs the instruction, and saves the result as a new layer.
    • These layers pile up, and the final image is a mix of all these layers.
    • When you run an image and spin up a container, a new writable layer, called the container layer, gets added on top of the existing layers.
    • Container layer is where all the action happens โ€” any changes you make, like adding new files, tweaking existing ones, or deleting stuff, all get handled here.


  3. Cache ๐Ÿ’พ

    • Docker keeps intermediate images to speed up future builds.
    • When you rebuild an image, Docker uses cached layers if nothing's changed.
    • "Using cache" means Docker is reusing layers it already made, instead of creating new ones.
    • Caching makes the build process much faster for the parts of your Dockerfile that stay the same.

๐Ÿ‘‰ Instructions โœ…

  1. FROM

    • Sets the Base Image for the next steps.
    1FROM [--platform=<platform>] <image> [AS <name>]
    2
    3FROM [--platform=<platform>] <image>[:<tag>] [AS <name>]
    4
    5FROM [--platform=<platform>] <image>[@<digest>] [AS <name>]
    • โ€œBhai, Python environment chahiye mujhe.โ€
    1FROM python:3.11
  2. COPY

    • Copies files and directories from the host machine to the Docker image. ๐Ÿ“ธ
    1# syntax
    2COPY <source> <destination>
    3
    4# example
    5COPY localfile.txt /app/localfile.txt
    6COPY . .
  3. ADD

    • It copies files and directories to the Docker image, downloads files from URLs ๐ŸŒ, and unzips tar files ๐Ÿ“ฆ directly into the destination folder.
    1# syntax
    2ADD <source> <destination>
    3
    4# example
    5ADD anotherfile.txt /app/anotherfile.txt
    6ADD https://example.com/file.txt /app/file.txt
    7ADD archive.tar.gz /app/
  4. RUN

    • Executes commands during build (executable or shell).
    1# shell form, the command is run in a shell,
    2# which by default is /bin/sh -c on Linux or cmd /S /C on Windows
    3RUN <command>
    4
    5# exec form
    6RUN [\"executable\", \"param1\", \"param2\"]
    • โ€œPackage install karna hai? Run it here.โ€
    1# Install python packages
    2RUN pip install -r requirements.txt
  5. CMD

    • Tells Docker what to run when container starts.
    • Dockerfile can only contain one CMD instruction. If you enter more than one CMD, only the last one will be executed.
    • Donโ€™t confuse CMD with RUN !!! (RUN at build-time, CMD at run-time)
    1# exec form, this is the preferred form
    2CMD [\"executable\",\"param1\",\"param2\"]
    3
    4# as default parameters to ENTRYPOINT
    5CMD [\"param1\",\"param2\"]
    6
    7# shell form
    8CMD command param1 param2
    • "Container start hone par HOME env variable ki value print karna hai ?"
    1CMD echo $HOME
    2or
    3CMD [\"sh\", \"-c\", \"echo $HOME\"]
  6. WORKDIR

    • Sets the working directory inside the container and changes the directory for the next commands.
    • So, whatever RUN, CMD, and everything else happens, it executes at the given path.
    • โ€œFrom now on, kaam yahin se hoga.โ€
    1# syntax
    2WORKDIR <directory>
    3
    4# example
    5WORKDIR /usr/src/app
  7. ENTRYPOINT

    • Ensures that a specific command or task always runs when the container starts, regardless of any additional commands you provide.
    1# exec form, which is the preferred form
    2ENTRYPOINT [\"executable\", \"param1\", \"param2\"]
    3
    4# shell form
    5ENTRYPOINT command param1 param2
    • You should include at least one CMD or ENTRYPOINT command in the Dockerfile.
    • If you want to use the container as a program, you need to set ENTRYPOINT.
    • Use CMD to set default arguments for ENTRYPOINT.

    Overriding CMD and ENTRYPOINT

    1FROM ubuntu:latest
    2
    3ENTRYPOINT [\"echo\", \"Default ENTRYPOINT:\"]
    4CMD [\"Hello from CMD\"]
    5
    6# Default behaviour will be like below
    7# Default ENTRYPOINT: Hello from CMD
    • CMD can be overriden by adding more arguments when you run the container docker run <container_name> <arguments>
    1docker run mycontainer \"Overridden CMD\"
    2
    3# CMD overriden output will be like below
    4# Default ENTRYPOINT: Overridden CMD
    • ENTRYPOINT can be overriden by --entrypoint docker run --entrypoint <command> <container_name> <arguments>
    1docker run --entrypoint /bin/echo mycontainer \"Overridden ENTRYPOINT\"
    2
    3# ENTRYPOINT overriden output will be like below
    4# Overridden ENTRYPOINT
  8. LABEL

    • Adds metadata (information) to the image.
    • โ€œYeh image maine banaya hai โ€” haan bhai, credit toh banta hai!โ€ ๐Ÿ˜Ž
    1LABEL maintainer=\"you@example.com\"

  9. SHELL

    • The SHELL instruction lets you switch up the default shell for shell-form commands. On Linux, it usually defaults to [\"/bin/sh\", \"-c\"], and on Windows, it's [\"cmd\", \"/S\", \"/C\"].
    • This comes in handy on Windows, where cmd, powershell, and sh are pretty common.
    • You can use the SHELL instruction more than once, and each time you do, it replaces the previous one and changes all the commands that come after it.
    1SHELL [\"executable\", \"parameters\"]
    • โ€œChalo powershell use karte hainโ€ฆโ€
    1SHELL [\"powershell\", \"-command\"]
    2RUN echo Hello
  10. HEALTHCHECK

    • Checks if your app inside container is still running properly.
    1HEALTHCHECK CMD curl --fail http://localhost:3000 || exit 1
    • โ€œApp down hai ya nahi? Yeh check karega.โ€
  11. VOLUME

    • To persist data outside the container.
    • Container delete hone ke baad bhi data safe rahega.
    1VOLUME <dir>
  12. ENV

    • Sets environment variables.
    • Multiple <key>=<value> variables can be set at the same time using the ENV instruction.
    1# Syntax
    2ENV <key>=<value>
    3
    4# Example
    5ENV APP_NAME=\"MyApp\"
  13. EXPOSE

    • Opens a port.
    • The EXPOSE instruction doesn't really open the port. It's more like a hint for the image creator and container user about which ports should be opened.
    • To open and map ports, use the -p flag with docker run for specific ports, or use the -P flag to open all exposed ports and map them to higher ports.
    • EXPOSE assumes TCP by default. You can also use UDP.
    • Its like saying โ€œOye Docker, listen on this port.โ€ ๐Ÿ˜
    1# Syntax
    2EXPOSE <port>
    3EXPOSE <port> [<port>/<protocol>...]
    4
    5# Example
    6EXPOSE 8080
    7EXPOSE 80/udp

๐Ÿ‘‰ Best Practices & Common Mistakes ๐Ÿ˜‰

Best Practices

  • Use Multi-Stage Builds: Keep your final image slim.
  • .dockerignore is your BFF: Skip unnecessary files. ๐Ÿšซ
1# sample .dockerignore file
2*.pyc
3__pycache__/
4*.log
5config/
6README.md
  • Avoid using latest: Version everything properly. ๐Ÿ”ข
  • Chain RUN commands: Reduce layers and improve caching.
  • Don't run as root: Use USER for safety.

Common Gotchas

  • COPY fails? Check your paths and .dockerignore.
  • CMD not working? Switch to ENTRYPOINT. ๐Ÿ”„
  • Caching issues? Reorder instructions โ€” COPY before RUN if files change often.

๐Ÿ‘‰ Folder Structure ๐Ÿ“‚

1my-project/
2โ”œโ”€โ”€ Dockerfile
3โ”œโ”€โ”€ .dockerignore
4โ”œโ”€โ”€ app/
5โ”‚   โ”œโ”€โ”€ main.py
6โ”‚   โ”œโ”€โ”€ requirements.txt
7โ”‚   โ””โ”€โ”€ utils.py
8โ”œโ”€โ”€ config/
9โ”‚   โ””โ”€โ”€ config.yaml
10โ””โ”€โ”€ README.md

๐Ÿ‘‰ Examples ๐Ÿ˜€

  1. Python

    1# Use the official Python image
    2FROM python:3.9
    3
    4# Set the working directory
    5WORKDIR /app
    6
    7# Copy requirements file and install dependencies
    8COPY requirements.txt .
    9RUN pip install -r requirements.txt
    10
    11# Copy the rest of the application code
    12COPY . .
    13
    14# Set the default command to run the application
    15CMD [\"python\", \"app.py\"]
  2. React

    1# Use the official Node.js image
    2FROM node:14
    3
    4# Set the working directory
    5WORKDIR /app
    6
    7# Copy package.json and package-lock.json and install dependencies
    8COPY package.json package-lock.json ./
    9RUN npm install
    10
    11# Copy the rest of the application code
    12COPY . .
    13
    14# Build the application
    15RUN npm run build
    16
    17# Expose the port the app runs on
    18EXPOSE 3000
    19
    20# Set the default command to run the application
    21CMD [\"npm\", \"start\"]

๐Ÿ‘‰ Conclusion โœ…

You're CRUSHING IT with Docker now! ๐Ÿ”ฅ Upcoming Part 3 - Docker Compose blog SOON! ๐Ÿš€๐Ÿณ Stay tuned - it's gonna be EPIC! #DockerGang โœจ Till thenโ€ฆ

Happy Learning !!!
๐Ÿ˜Š

Heyy ๐Ÿ‘‹

If you have any suggestion / question, please fill out the form here. I will get back to you ASAP.