Docker Part 2- Image Management

 



                                                                  Docker Image Management 

A docker image is a read only blue print used to create containers. It contains the application code, dependencies and environment required to run the containers.


                                             Image vs Container 

FeatureDocker ImageDocker Container
DefinitionRead-only template used to create containers Running instance of a Docker image
StateStatic (does not change)Dynamic (changes during runtime)
PurposeUsed to build containersUsed to run applications
MutabilityImmutable (cannot be modified)Mutable (can be modified)
StorageStored on diskRuns in memory + disk
CreationCreated using DockerfileCreated using docker run
LifecycleExists until deletedStarts, stops, restarts
ReusabilityCan be reused multiple timesUsually short-lived
Read/Write Layer  No write layerHas a writable layer
Examplenginx:latestRunning nginx server

 

                                                               Commands 

1. docker images --> list docker image 

2. docker pull image name --> pull (download) image 

3. docker build -t image name --> Build docker image 

4. docker rmi image id --> remove docker image 

5. docker rmi -f  image id --> remove the docker image in the system 

6. docker tag httpd httpd:v1 --> tag docker image 

7. docker inspect image name --> show all metadata image 

8. docker history image name --> show image history 

9. docker save -o myimage.tar image name --> save the image in .tar file


Types of Image Creation

1. DockerFile Method 

2. Commit Method


Docker file :- A docker file is a text file that contains a set of  instruction used to build a Docker Image.

It defines everything your image should have - like base image, software package, environment variables, coping files and command to run.

When you run "docker build", docker reads ths docker file line-by-line and creates a custom, repeatable and automated image.


🧩 Why Dockerfile is used?

  • To create custom Docker images

  • To make application deployment repeatable

  • To automate image creation

  • To follow DevOps best practices


📄 Very Small Example Dockerfile (Beginner Friendly)

Example: Simple “Hello Docker” Image


FROM ubuntu
CMD ["echo", "Hello Docker"]

🔍 Explanation:

  • FROM ubuntu → Base image

  • CMD → Command that runs when container starts


▶️ Build & Run

docker build -t hello-docker . docker run hello-docker

Output:

Hello Docker

📝 One-Line Summary (Blog Highlight)

A Dockerfile is a script that defines how a Docker image is built and how the container runs.


🐳 Dockerfile Important Instructions (Complete Table)

InstructionPurposeRuns AtExample
FROMBase image define karta haiBuild timeFROM ubuntu:22.04
RUNCommands execute karta hai (install, update)Build timeRUN apt-get update
COPYLocal files image me copy karta haiBuild timeCOPY app.js /app/
ADDCOPY + extra features (URL, extract)Build timeADD app.tar.gz /app/
CMDContainer start hone par default commandRun timeCMD ["node","app.js"]
ENTRYPOINTFixed command set karta haiRun timeENTRYPOINT ["nginx"]
EXPOSEContainer port document karta haiBuild timeEXPOSE 80
ENVEnvironment variables set karta haiBuild time / Run timeENV APP_ENV=prod
WORKDIRWorking directory set karta haiBuild timeWORKDIR /app
USERContainer kis user se run hogaRun timeUSER node
VOLUMEPersistent data ke liye volumeRun timeVOLUME /data
LABELImage metadata add karta haiBuild timeLABEL author="DevOps"
ARGBuild-time variables define karta haiBuild timeARG VERSION=1.0
HEALTHCHECKContainer health check karta haiRun timeHEALTHCHECK CMD curl localhost
SHELLDefault shell change karta haiBuild timeSHELL ["/bin/bash","-c"]
ONBUILDChild image ke liye triggerBuild timeONBUILD COPY . /app
STOPSIGNALContainer stop signal define karta haiRun timeSTOPSIGNAL SIGTERM



Difference between Copy and ADD in Dockerfile?
Copy and ADD both copy files from the host to the container. The difference is, Copy is simple and only copies local files or directories, while ADD can also download files from a URL and automatically extract .tar archives.

Best practice: always use COPY for simple coping and use ADD only when extra feature are needed.


Difference between CMD and ENTRYPOINT in Dockerfile?
ENTRYPOINT sets the main command , CMD sets default arguments. CMD is easily overridden at runtime, ENTRYPOINT is not, use ENTRYPOINT for fixed commands and CMD for default.




Comments

Popular posts from this blog

User Management in Linux – Complete Notes for Beginners

DevOps Roadmap 2026 – Your Complete Beginner-to-Expert Guide

What is Public Subnet in AWS? Step-by-Step Explanation with Example