Docker
Continue learning Docker in this advanced guide. Explore branching, merging, and squashing with real-world Git workflow examples.
image:-
A container image a lightweight, standalone, and executable software package that contains:
-
Everything needed to run an application, including:
-
Source code
-
Dependencies
-
Libraries
-
Configuration files
-
Runtime environment (like Python, Node.js, etc.)
-
OS-level files (minimal, like from Ubuntu, Alpine)
-

Think of it like a blueprint for containers.
You build containers from images.
Example
# container file to build a Python app image
FROM python:3.10-slim
COPY app.py /app.py
CMD ["python", "app.py"]
Then run:
container build -t my-python-app .
container run my-python-appKey Commands
container images # List all images
container build . # Build image from container file
container rmi <image> # Remove an image
container pull <image> # Download image from container Hub
Real-Life Analogy
Image = Recipe
Container = Cooked dish made using the recipe
Docker Custom Image
A custom Docker image is one that you create yourself, tailored to your application’s specific needs.
Instead of using a prebuilt image like python:3.10, you define everything — the OS, libraries, dependencies, and your code — using a container file.
Structure
You create a container file, which acts as instructions to build the image.
Example: Create a Custom Image for a Node.js App
1. Your app folder structure:
my-app/
│
├── container file
├── package.json
└── index.js
2. container file content:
# Base image
FROM node:18-alpine# Create app directory
WORKDIR /app# Copy files
COPY package*.json ./
RUN npm installCOPY . .# App runs on port 3000
EXPOSE 3000
# Command to run the app
CMD ["node", "index.js"]
1. docker save – Export Docker Image
container save -o <file-name>.tar <image-name>
Example:
container save -o myapp.tar my-custom-node-app
This creates a file myapp.tar containing the full container image (including all layers).
2. docker load – Import Docker Image
container load -i <file-name>.tar
Example:
container load -i myapp.tarUse Case Example
Let’s say you want to move a custom image from your laptop to another server:
-
Save the image:
bashdo container image save -o myapp.tar my-custom-node-app
-
Copy the file (via USB, scp, etc.) to the target server.
-
Load the image on the server:
bashcontainer load -i myapp.tar
-
Verify it’s available:
bashcontainer image
Docker Registry
A Docker Registry is a storage and distribution system for container images.
It allows you to:
-
Store container images
-
Share them with others
-
Pull them to run containers anywhere
Types of regisry:
| Type | Examples |
|---|---|
| Public Registry | Docker Hub, GitHub Container Registry |
| Private Registry | Your own on-premise or cloud-based registry (like AWS ECR, GCR) |
Most Common: container Hub
-
Official public registry by container
-
Contains official images (e.g.,
ubuntu,nginx,mysql) -
Free & paid plans
Website: Click Here
How It Works
-
Build your image:
bashcontainer build -t username/my-app .
-
Login to container Hub:
bashcontainer login
-
Push your image to the registry:
bashcontainer push username/my-app
-
Pull image on another machine:
bashcontainer pull username/my-app
Registry vs Repository vs Image
| Term | Meaning |
|---|---|
| Registry | Whole storage system |
| Repository | A collection of versions of an image (username/my-app) |
| Image | A specific version (tag) like username/my-app:1.0 |
Private container Registry (Self-Hosted)
Run your own registry locally or in a data center:
container run -d -p 5000:5000 --name registry registry:2
Now push images to localhost:5000/my-image.
Registry Commands Overview
| Command | Description |
|---|---|
docker login |
Authenticate with a registry |
docker push <image> |
Upload image to a registry |
docker pull <image> |
Download image from a registry |
docker tag <image> <repo/image> |
Tag image for pushing |
Analogy
Registry = App Store
Repository = Individual app page
Image = Specific app version (v1.0, v2.0)
Dockerfile
A Dockerfile is a plain text file that contains a set of instructions used by container to build a custom image.

Why Use a Dockerfile?
-
Automate the creation of container images
-
Ensure consistency across environments (dev, test, prod)
-
Easily share your app with its environment
Basic Structure :
# 1. Base Image
FROM node:18
# 2. Set working directory inside container
WORKDIR /app
# 3. Copy files
COPY package*.json ./
RUN npm install
COPY . .
# 4. Expose port (optional but good practice)
EXPOSE 3000
# 5. Command to run the app
CMD ["node", "index.js"] Container file Instructions (Most Common)
| Instruction | Purpose |
|---|---|
FROM |
Sets the base image (e.g., Ubuntu, Python, Node.js) |
WORKDIR |
Sets working directory in the container |
COPY |
Copies files from host into image |
RUN |
Executes commands while building the image |
EXPOSE |
Documents which port the container will use |
CMD |
Sets default command to run container |
Example: Python App container file
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Build Image from container file
container build -t my-python-app .
Run Container from Built Image
container run -p 5000:5000 my-python-app
Real-Life Analogy
container file = Cooking recipe
container image = Prepared food
Container = Serving the dish (running app)
Best Practices
-
Use small base images like
alpinefor minimal size -
Group
COPYandRUNefficiently to avoid extra layers -
Use
container ignoreto exclude unnecessary files -
Pin versions in
FROM,pip,npmetc. for reproducibility