Understanding Docker: What, Why, and How

Pragya SaraswatPragya Saraswat
6 min read

🐳 What do you need docker?

Docker helps you run apps in lightweight, isolated environments called containers. This makes it easier to build, ship, and run software reliably across different machines or systems.


🤔 What it can do?

It can containerize apps
Run each service with its own
Dependencies in separate containers

Explanation:
Docker lets you package your app and everything it needs into a single container. You can run multiple services (like frontend, backend, database) in different containers, each with its own set of tools, libraries, and dependencies. This avoids conflicts and makes everything clean and modular.


📦 Containers

Containers are completely isolated environments as in they can have their own processes, own services, own networking interfaces, own mounts just like virtual machines except they all share the same os kernels.

Explanation:
Containers feel like small virtual machines — they run their own apps, services, and even have their own IPs and storage paths. But they don’t carry the whole operating system like VMs do. They all use the same OS kernel, which makes them much faster and lighter.


🧠 Operating system concept

Operating system is responsible for interacting under line hardware. while the os kernel remains the same . it is the software above it that makes the operating system different.

Explanation:
Every OS talks to your hardware using its kernel — this part is the same across containerized environments. What differs is the user-level software (like shells, commands, tools), which is why Ubuntu and Alpine can feel different, even if they share the same kernel under Docker.


📷 Docker image

Docker image - An image is a package or template , used to create one or more containers.

Explanation:
A Docker image is like a ready-to-use blueprint of your app. It contains everything your app needs to run: code, libraries, environment variables, etc. You use images to spin up containers.


🚢 Docker container

Docker container - Containers are running instances of images that are isolated and have their own environments , set of processes.

Explanation:
When you start a Docker image, it becomes a running container. The container is your live app — isolated, clean, and doing its own thing without interfering with other containers.


▶️ Docker run

Docker run - It start a container . if there is no image it will pull from docker hub and then run the container but it will only happen to the first time . for the subsequent time , the same image will be used.

Explanation:
This is the most commonly used command to start a container. Docker checks if the image exists locally; if not, it downloads it from Docker Hub. After that, it uses the same local image for next runs.


🧾 Docker ps

Docker ps - list all running containers

Explanation:
This shows you only the containers that are actively running at the moment.


Docker ps -a - all running & previously stopped containers.

Explanation:
This one shows you the full container history — including ones that have stopped or exited.


Docker stop - stop a container
Docker stop

Explanation:
This command gracefully stops a running container by sending it a SIGTERM signal.


Docker rm - to remove the stopped conatiner permanently

Explanation:
Once a container is stopped, it still exists on your system. This command cleans it up for good.


📋 Docker images

Docker images - to list all available images

Explanation:
This shows all the images you’ve downloaded or created — your collection of app blueprints.


Docker rmi - to remove particular image. (make sure to delete all dependent containers to remove image)

Explanation:
This deletes an image. You’ll need to remove any containers using this image first.


Docker pull want to simply download the image

Explanation:
Use this if you just want to download an image without running it yet.


🧪 Docker run ubuntu

Docker run ubuntu - containers are not meant to host os . containers are meant to run a specific task or a process such as to host an instance of a web server , application server or db.

Once the task is completed , the container exits.

The container only live as lon as the process inside it is alive. The app inside the container is stopped or crashed then the container exits.

Ubuntu is used as a base image for other apps.

Explanation:
Even though you can run Ubuntu inside a container, the point of containers is not to simulate full operating systems. It’s to run tasks — like serving a website or running a background worker. If the main task inside a container finishes or crashes, the container shuts down automatically.


🔧 Exec command

Exec command - execute a command
Docker exec cat /etc/hosts

Explanation:
This lets you run a command inside a running container — super useful for debugging or checking logs.


📎 Run - attach & Detach

Docker run <>
Docker run -d <>

Explanation:

  • Without -d, the container runs in the foreground (attached mode).

  • With -d, the container runs in the background (detached mode), letting you keep using the terminal.


🏷️ Run - tag

Docker run redis (default tag is latest)
Docker run redis:4.0 (here redis:4.0 is a tag)

Explanation:
You can specify which version (tag) of an image you want. If you don’t, Docker uses the latest version by default.


📥 Run - STDIN

(docker dosent listen to standard input by default even you are attached to irs console), it doesnt have terminal to read input from you.

Docker run -i <> , -i is for input parameter
Docker run -it <> , here -it is attached to a terminal or an interactive mode.

Explanation:

  • -i: keeps STDIN open, so the container can read input.

  • -t: gives you a pseudo-TTY (like a terminal).

  • Together, -it lets you interact with the container, as if you were inside a shell.


🌍 Run - port mapping

Docker run -p 90:5000 <>

Explanation:
This maps your host machine’s port 90 to the container’s port 5000, so you can access whatever service is running inside the container from your browser or tools.


💾 Run - volume mapping

Docker run -v /opt/datadir:/var/lib/mysql mysql

Explanation:
This connects your local folder /opt/datadir to the container’s MySQL data folder. Useful for saving data even if the container gets deleted.


🕵️‍♂️ Inspect container

Docker inspect <>

Explanation:
This shows you everything about a container — its environment, IP, mounted volumes, image source, and more.


📑 Container logs

Docker logs <>

Explanation:
Check the output (logs) of a running or even stopped container — super helpful for debugging issues.


🔍 Docker run ubuntu cat /etc/release

Docker run ubuntu cat /etc/release

Explanation:
This command quickly tells you the operating system release info from within an Ubuntu container.


0
Subscribe to my newsletter

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

Written by

Pragya Saraswat
Pragya Saraswat