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
| Feature | Docker Image | Docker Container |
|---|---|---|
| Definition | Read-only template used to create containers | Running instance of a Docker image |
| State | Static (does not change) | Dynamic (changes during runtime) |
| Purpose | Used to build containers | Used to run applications |
| Mutability | Immutable (cannot be modified) | Mutable (can be modified) |
| Storage | Stored on disk | Runs in memory + disk |
| Creation | Created using Dockerfile | Created using docker run |
| Lifecycle | Exists until deleted | Starts, stops, restarts |
| Reusability | Can be reused multiple times | Usually short-lived |
| Read/Write Layer | No write layer | Has a writable layer |
| Example | nginx:latest | Running 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
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
🔍 Explanation:
-
FROM ubuntu→ Base image -
CMD→ Command that runs when container starts
▶️ Build & Run
🐳 Dockerfile Important Instructions (Complete Table)
| Instruction | Purpose | Runs At | Example |
|---|---|---|---|
| FROM | Base image define karta hai | Build time | FROM ubuntu:22.04 |
| RUN | Commands execute karta hai (install, update) | Build time | RUN apt-get update |
| COPY | Local files image me copy karta hai | Build time | COPY app.js /app/ |
| ADD | COPY + extra features (URL, extract) | Build time | ADD app.tar.gz /app/ |
| CMD | Container start hone par default command | Run time | CMD ["node","app.js"] |
| ENTRYPOINT | Fixed command set karta hai | Run time | ENTRYPOINT ["nginx"] |
| EXPOSE | Container port document karta hai | Build time | EXPOSE 80 |
| ENV | Environment variables set karta hai | Build time / Run time | ENV APP_ENV=prod |
| WORKDIR | Working directory set karta hai | Build time | WORKDIR /app |
| USER | Container kis user se run hoga | Run time | USER node |
| VOLUME | Persistent data ke liye volume | Run time | VOLUME /data |
| LABEL | Image metadata add karta hai | Build time | LABEL author="DevOps" |
| ARG | Build-time variables define karta hai | Build time | ARG VERSION=1.0 |
| HEALTHCHECK | Container health check karta hai | Run time | HEALTHCHECK CMD curl localhost |
| SHELL | Default shell change karta hai | Build time | SHELL ["/bin/bash","-c"] |
| ONBUILD | Child image ke liye trigger | Build time | ONBUILD COPY . /app |
| STOPSIGNAL | Container stop signal define karta hai | Run time | STOPSIGNAL SIGTERM |
Comments
Post a Comment