(Day 12) Task : Dockerfile Components & different commands :-

Aditya SharmaAditya Sharma
4 min read

What is a Dockerfile?

  • A Dockerfile is a simple text file with a set of instructions to build a Docker image.

  • It defines what goes inside the Docker image — like OS, libraries, dependencies, app code, environment variables, etc.

Components of a Dockerfile :-

Here are the most commonly used instructions:

1. FROM :

  • Purpose: Specifies the base image you want to use.

  • Example:

      FROM ubuntu
    
  • Notes: Every Dockerfile must start with a FROM instruction.

2. MAINTAINER :

  • Purpose: Specifies the author , Owner or maintainer of the image.

  • Example:

      MAINTAINER ="yourname@example.com"
    

3. RUN :

  • Purpose: Executes commands inside the container during image building.

  • Example:

      RUN echo "Aditya Sharma" >/tmp/testfile
    
  • Notes:

    • Each RUN creates a new image layer.

    • Best to combine commands with && to reduce layers.

4. CMD :

  • Purpose: Executes Commands but during Container Creation.

  • Example:

      CMD ["echo", "Hello World"]
    
  • Notes:

    • Only the last CMD in the Dockerfile is used.

    • Overridable at runtime (docker run).

5. ENTRYPOINT :

  • Purpose: Similar to CMD, but fixed — can't easily be overridden this has higher priority over CMD , first commands will be executed by entry point only.

  • Example:

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

6. COPY :

  • Purpose: Copy files/folders from local machine into the image we need to provide source & destination.

  • Example:

      COPY ./app /usr/src/app
    

7. ADD :

  • Purpose: Like COPY, but also:

    • Can unzip archives automatically.

    • Can download from URL (but not recommended — better to use curl/wget).

  • Example:

      ADD app.zip /usr/src/app
    

8. WORKDIR :

  • Purpose: Sets the working directory for a container instead of going to container.

  • Example:

      WORKDIR /usr/src/app
    
  • Notes:

    • No need to use cd in RUN — just set WORKDIR.

9. EXPOSE :

  • Purpose: Tells Docker the port the container will listen on.

  • Example:

      EXPOSE 8080
    

    Notes:

    • This does not publish the port automatically, it’s just documentation.

10. ENV :

  • Purpose: Set environment variables inside the container.

  • Example:

      ENV=production
    

Important Docker Commands :-

Now let's look at important Docker commands you must know:

Working with Containers

  • Run Container :

      docker run -it --name <container-name> <image-name> /bin/bash
    
    • -d runs in detached (background) mode.

    • -p maps host port to container port.

  • List running Containers :

      docker ps
    
  • List All Containers (running + stopped) :

      docker ps -a
    
  • Stop Container :

      docker stop <container-name>
    
  • Remove Container :

      docker rm <container-name>
    
  • View Container Logs :

      docker logs <container-name>
    
  • Execute Command Inside Running Container :

      docker exec -it <container-name>
    
    • -it makes it interactive.

    • bash opens shell inside container.

Steps to Create a Container Using a Dockerfile :-

  1. Create the Dockerfile :
    • Using touch command OR using a text editor (like vim, nano, or Visual Studio Code).

          touch Dockerfile
              # OR
          vim Dockerfile
              # OR
          nano Dockerfile
      

2. Add Instructions to Dockerfile :

  • Open the Dockerfile in your editor.

  • Add basic instructions.

    • For example:

        FROM ubuntu
        RUN echo "Aditya sharma" >/tmp/testfile
      
  1. Create an Image from a Dockerfile :

    What Does "Build an Image" Mean?

    • Building an image means converting your Dockerfile into a ready-to-run Docker image.

    • Docker reads each instruction in the Dockerfile (like FROM, RUN, COPY) and creates an image layer step-by-step.

    • The final result is a custom image you can use to run containers.

Use the docker build Command :

  • Command Syntax:

      docker build -t <image-name> .
      docker ps -a                          # To check all containers status
    
    • docker build: Command to build an image.

    • -t: Tag the image with a name and optionally a version/tag.

    • . (dot): Current directory (Docker will search for Dockerfile here).

  1. Create and Run a Container from an Image :

    Use the docker run Command

    • Command syntax:

        docker run -it --name <container-name> <image-name> /bin/bash 
        cat /tmp/testfile
      
    • Note : In same docker file if instructions changes then new images can be formed easily.

Codes :

0
Subscribe to my newsletter

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

Written by

Aditya Sharma
Aditya Sharma

DevOps Enthusiast | Python | Chef | Docker | GitHub | Linux | Shell Scripting | CI/CD & Cloud Learner | AWS