Introduction to Docker...

Jaffar AmanJaffar Aman
4 min read

What is Docker?

Docker is an open-source platform that allows you to build, package, and run applications in lightweight, portable containers.

Instead of installing software, libraries, and configurations separately on every machine, Docker lets you package everything your application needs into a single unit called an image.

When you run that image, it becomes a container — an isolated environment where your app works exactly the same, whether on:

  • Windows

  • macOS

  • Linux

  • Local machine or a cloud server

In short:

Docker ensures your app runs consistently everywhere — removing the “It works on my machine” problem.


Problem Statement — Why Docker?

  1. Different Developer Environments

    • Developer A: Windows, Node.js v18

    • Developer B: macOS, Node.js v20

    • If a tool is Windows-specific, a macOS user can’t run it.

    • Version mismatches can cause crashes or unexpected behavior.

  2. Painful Server Setup

    • Setting up a project locally is fine once,
      but repeating the same setup on a server is time-consuming and error-prone.

    • High chances that the first deployment fails due to environment/configuration differences.

  3. Scaling Challenges

    • In auto-scaling, multiple servers are created.

    • Manually setting up each server is repetitive and inefficient.

➡ Solution: Docker provides a standard container environment so your application runs exactly the same on any machine, regardless of OS or software versions.


What is a Docker Container?

  • A container is an isolated environment that contains your application and all its dependencies.

  • Once you create a container, you can run its copy on any number of machines.

  • Containers are lightweight because they share the host OS kernel instead of running a full OS like virtual machines.


Docker CLI vs Docker Desktop

  1. Docker Daemon

    • The background service that does all the heavy lifting:

      • Creates containers

      • Pulls images

      • Manages scaling

  2. Docker CLI

    • A command-line tool used to send instructions to the Docker Daemon.

    • Example:

        bashCopyEditdocker run -it ubuntu
      
      • If the ubuntu image exists locally → it runs directly.

      • If not → it downloads the image from hub.docker.com.

  3. Docker Desktop

    • A GUI interface that is beginner-friendly.

    • Still runs the same Docker Daemon in the background.


Docker Images vs Containers

  • Image:

    • A blueprint/template that defines the OS, libraries, and dependencies.

    • Example: node:18-alpine (Node.js with a minimal Linux OS)

  • Container:

    • A running instance of an image.

    • Isolated — multiple containers cannot access each other’s data unless configured.

Analogy:

  • Image = Recipe

  • Container = The dish made from that recipe


Custom Image Creation

  • To create your own image, you write a Dockerfile that specifies:

    • Base image

    • Copying application code

    • Installing dependencies

    • Setting startup commands

  • This generates a custom image that works exactly the same across all systems.


Docker Commands You Should Know

Here are some basic but essential Docker commands with explanations:

CommandDescription
docker container lsShows only running containers
docker container ls -aShows all containers (running + stopped)
docker start <container_id>Starts a stopped container
docker stop <container_id>Stops a running container
docker rm <container_id>Deletes a stopped container
docker imagesLists all downloaded images
docker rmi <image_id>Deletes an image from local
docker pull <image_name>Downloads an image from Docker Hub
docker run <image_name>Creates and runs a container from an image
docker run -it <image_name>Runs a container in interactive mode (useful for shells)
docker psAlias for docker container ls
docker exec -it <container_id> bashAccess a running container's shell
docker logs <container_id>Shows logs of a container
docker build -t myimage:1.0 .Builds a custom image from Dockerfile in current directory

💡 Pro Tip:

  • Use docker ps -q to get only container IDs.

  • Use docker stop $(docker ps -q) to stop all running containers at once.

  • Use docker system prune to remove all unused containers, images, and networks (frees up space).

0
Subscribe to my newsletter

Read articles from Jaffar Aman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jaffar Aman
Jaffar Aman