Docker In Depth

Docker is a platform that allows you to build, run, and manage applications in isolated environments called containers.
π§± Why use Docker?
Imagine you have an app. It needs:
Ubuntu Linux
Python 3.10
Some dependencies (like Flask, NumPy, etc.)
You give the app to your friend, but it doesn't work on their system because their environment is different.
π Docker solves this!
It packages everything your app needs (OS, code, libraries, configs) into one container so it runs the same everywhere β on your machine, a server, or the cloud.
π§ What is a Container?
A container is a lightweight, standalone package that includes:
Code
Runtime
System tools
Libraries
Settings
It's like a virtual machine, but faster and uses less memory.
ποΈ What is a Docker Image?
A Docker Image is a blueprint of your container.
You create containers from images.
Example:
If a container is a running car, the image is the car design (or plan) to build it.
βοΈ Key Docker Components
Component | Description |
Dockerfile | A file with step-by-step instructions to build an image |
Image | A snapshot of an application and its environment |
Container | A running instance of an image |
Docker Engine | The software that runs and manages containers |
Docker Hub | A cloud-based registry to store/share Docker images |
π¦ Docker Basic Commands
β 1. π§Ύ Check Docker Version & Info
Command | Meaning |
docker --version | Shows Docker version installed |
docker info | Shows detailed Docker setup info |
docker help | Lists all available Docker commands |
β 2. π¦ Docker Images (Managing Base Blueprints)
Command | Meaning |
docker images | List all images on your system |
docker pull <image> | Download image from Docker Hub (you already did this!) |
docker rmi <image_id> | Remove an image |
π‘ Try:
bashCopyEditdocker images
docker pull python
docker rmi python # Removes the image
β 3. π Run Containers
Command | Meaning |
docker run <image> | Run a container from image (non-interactive) |
docker run -it <image> | Run a container interactively with terminal |
docker run -d <image> | Run container in detached (background) mode |
docker run --name mycontainer <image> | Give your container a custom name |
docker run -p 8080:80 <image> | Map ports (host:container) |
docker run -v /host:/container <image> | Mount volume from your host |
π‘ Try:
bashCopyEditdocker run -it python
docker run --name test-python -it python
docker run -d --name mynginx -p 8080:80 nginx
β 4. π Working with Containers
Command | Meaning |
docker ps | Show running containers |
docker ps -a | Show all containers (even stopped) |
docker start <container> | Start a stopped container |
docker stop <container> | Stop a running container |
docker restart <container> | Restart a container |
docker rm <container> | Delete a container |
π‘ Try:
bashCopyEditdocker ps
docker ps -a
docker stop mynginx
docker rm mynginx
β 5. π Files, Volumes & Workdir
Command | Meaning |
docker run -v $(pwd):/app -w /app python | Run container, mount current directory, and set working directory |
docker cp <container>:<path> <host_path> | Copy files from container to host |
docker cp <host_path> <container>:<path> | Copy files from host to container |
π‘ Try:
bashCopyEditdocker run -it -v $(pwd):/code -w /code python
β 6. π§Ό Cleanup Commands
Command | Meaning |
docker rm <container_id> | Remove container |
docker rmi <image_id> | Remove image |
docker container prune | Delete all stopped containers |
docker image prune | Delete unused images |
docker system prune | Clean up all: containers, volumes, images (CAREFUL!) |
π‘ Try:
bashCopyEditdocker container prune
β 7. π Dockerfile Related
You can build custom images using a Dockerfile.
Command | Meaning |
docker build -t myimage . | Build an image from Dockerfile in current folder |
docker run myimage | Run container from your custom image |
β 8. π§ Executing Inside Container
Command | Meaning |
docker exec -it <container> bash | Open a terminal inside a running container |
docker logs <container> | See logs (for servers) |
docker top <container> | See processes inside a container |
π‘ Try:
bashCopyEditdocker exec -it test-python bash
β 9. π·οΈ Tagging and Pushing Images (if using Docker Hub)
Command | Meaning |
docker tag <image_id> username/myimage:tag | Tag your image |
docker login | Log in to Docker Hub |
docker push username/myimage:tag | Push your image to Docker Hub |
π§ͺ Quick Practical Flow You Can Try
bashCopyEdit# 1. Pull image
docker pull python
# 2. Run container interactively
docker run -it --name try-python python
# 3. Exit and check containers
exit
docker ps -a
# 4. Restart container
docker start -ai try-python
# 5. Remove container and image
docker stop try-python
docker rm try-python
docker rmi python
Would you like me to give you a task or project to try on your Amazon Linux using these commands (e.g., run a Python script, serve a website, or create a Dockerfile)?
You said:
sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 9592f5595f2b 9 days ago 192MB python latest 1c1dd43eba26 3 weeks ago 1.02GB but i didnt made them 3 weeka ago?
ChatGPT said:
Excellent observation β and you're absolutely right to question that.
Letβs clarify what that βCREATEDβ column in docker images
really means π
π§ What does βCREATEDβ mean in docker images
?
That column does not mean:
When you pulled the image
When you created the image locally
β It actually means:
When the original image was created by the maintainer and uploaded to Docker Hub.
π¦ Example in Your Case:
Repository | Tag | Created |
python | latest | 3 weeks ago β This is when the Docker Hub official Python image was updated and pushed to the registry. |
nginx | latest | 9 days ago β The official Nginx team uploaded this version 9 days ago. |
So when you pulled it today, Docker fetched the latest version that was created by the official maintainers some time ago.
π If You Want to Know When You Pulled an Image:
Docker does not show this directly, but you can check using:
Option 1: Look at Docker events (recent)
bashCopyEditdocker system events
Option 2: List containers youβve run
bashCopyEditdocker ps -a
If you ran a container from python
or nginx
, the CREATED
time in that output shows when you ran it, which can help infer when you pulled it.
β Summary
Column | Meaning |
CREATED (in docker images ) | Date the image was built by the maintainer, not when you pulled it |
CREATED (in docker ps -a ) | When you created and ran the container |
π§° Docker Compose β Managing Multi-Container Apps
Docker Compose is a tool to define and manage multi-container applications using a single file: docker-compose.yml
.
π§ Why use Compose?
Easily link services (like app + database)
Define ports, volumes, networks
One command to bring everything up or down
β Project: Static Website + MySQL using Compose
π Folder Structure:
docker-compose-practice/
βββ docker-compose.yml
βββ web/
βββ index.html
π index.html
<h1>π Hello from Docker Compose!</h1>
π§ docker-compose.yml
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./web:/usr/share/nginx/html:ro
db:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root123
MYSQL_DATABASE: testdb
MYSQL_USER: myuser
MYSQL_PASSWORD: mypass
βΆοΈ Run the project:
docker-compose up -d
Visit:
http://localhost:8080
β Youβll see your custom HTML content.
π Stop everything:
docker-compose down
ποΈ Docker Swarm β Scalable Container Orchestration
Docker Swarm is Dockerβs native tool for clustering and orchestration. It turns a group of machines into one virtual Docker host.
π When to use Swarm?
You want high availability
You want to scale your app
You want load balancing, rolling updates, and self-healing
π§ Swarm Key Concepts
Term | Meaning |
Swarm | A cluster of Docker hosts |
Node | One machine in the cluster (manager/worker) |
Service | A containerized task distributed across nodes |
Task | A running container inside a service |
Manager | Makes decisions and schedules tasks |
Worker | Executes tasks sent by managers |
π§ Docker Swarm Practical
π§± Step 1: Initialize Swarm
docker swarm init
Output includes a join-token
to add worker nodes.
βΆοΈ Step 2: Create a service
docker service create --name myweb -p 8080:80 nginx
Swarm schedules and runs this as a service.
π Step 3: Scale the service
docker service scale myweb=5
Runs 5 instances of Nginx across nodes.
π Step 4: View Swarm status
docker node ls
docker service ls
docker service ps myweb
π οΈ Step 5: Remove the service
docker service rm myweb
π§ Docker Compose vs Docker Swarm
Feature | Docker Compose | Docker Swarm |
Use case | Local Dev, Testing | Clusters, Production |
Clustering | β | β |
Load balancing | β | β |
Scale containers | Manual | Easy with scale |
Failover | β | β |
Deploy in cloud easily | β | β |
π Conclusion
With Docker, Docker Compose, and Docker Swarm, you can go from a simple static website to a production-grade, auto-scalable service running across multiple servers β all using container technology.
Subscribe to my newsletter
Read articles from Poonam Vetal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Poonam Vetal
Poonam Vetal
I am student from Pune institute of computer technology !π