Docker In Depth

Poonam VetalPoonam Vetal
9 min read

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

ComponentDescription
DockerfileA file with step-by-step instructions to build an image
ImageA snapshot of an application and its environment
ContainerA running instance of an image
Docker EngineThe software that runs and manages containers
Docker HubA cloud-based registry to store/share Docker images

πŸ“¦ Docker Basic Commands

βœ… 1. 🧾 Check Docker Version & Info

CommandMeaning
docker --versionShows Docker version installed
docker infoShows detailed Docker setup info
docker helpLists all available Docker commands

βœ… 2. πŸ“¦ Docker Images (Managing Base Blueprints)

CommandMeaning
docker imagesList 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

CommandMeaning
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

CommandMeaning
docker psShow running containers
docker ps -aShow 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

CommandMeaning
docker run -v $(pwd):/app -w /app pythonRun 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

CommandMeaning
docker rm <container_id>Remove container
docker rmi <image_id>Remove image
docker container pruneDelete all stopped containers
docker image pruneDelete unused images
docker system pruneClean up all: containers, volumes, images (CAREFUL!)

πŸ’‘ Try:

bashCopyEditdocker container prune

You can build custom images using a Dockerfile.

CommandMeaning
docker build -t myimage .Build an image from Dockerfile in current folder
docker run myimageRun container from your custom image

βœ… 8. 🧠 Executing Inside Container

CommandMeaning
docker exec -it <container> bashOpen 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)

CommandMeaning
docker tag <image_id> username/myimage:tagTag your image
docker loginLog in to Docker Hub
docker push username/myimage:tagPush 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:

RepositoryTagCreated
pythonlatest3 weeks ago β†’ This is when the Docker Hub official Python image was updated and pushed to the registry.
nginxlatest9 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

ColumnMeaning
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

TermMeaning
SwarmA cluster of Docker hosts
NodeOne machine in the cluster (manager/worker)
ServiceA containerized task distributed across nodes
TaskA running container inside a service
ManagerMakes decisions and schedules tasks
WorkerExecutes 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

FeatureDocker ComposeDocker Swarm
Use caseLocal Dev, TestingClusters, Production
ClusteringβŒβœ…
Load balancingβŒβœ…
Scale containersManualEasy 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.

0
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 !πŸŽ“