Installation & First Run
Install Docker, run your first container, and understand the core commands
Skills taught: docker.docker-run, docker.docker-ps, docker.container-lifecycle, docker.pull
Installing Docker
# macOS / Windows:
# Install Docker Desktop from https://docker.com/products/docker-desktop
# It includes: Docker Engine, Docker CLI, Docker Compose, and a GUI
# Ubuntu/Debian Linux:
curl -fsSL https://get.docker.com | sh # official install script
sudo usermod -aG docker $USER # run docker without sudo
newgrp docker # activate without logout
# Verify installation
docker --version
# Docker version 27.0.3, build 7d4bcd8
docker run hello-world # the classic smoke test
Your first real container
# Run an nginx web server in a container
docker run -d -p 8080:80 --name my-nginx nginx
# Breaking it down:
# docker run — create and start a container
# -d — detached mode (runs in background)
# -p 8080:80 — map host port 8080 to container port 80
# --name my-nginx — give it a memorable name
# nginx — the image to use (pulled from Docker Hub if not local)
# Now visit http://localhost:8080 — nginx is serving!
# See running containers
docker ps
# CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
# a3f2c1d4e5b6 nginx "/docker-entrypoint…" 5s ago Up 4s 0.0.0.0:8080->80/tcp
# Stop and remove it
docker stop my-nginx
docker rm my-nginx
# Stop + remove in one step
docker rm -f my-nginx
Essential daily commands
# ── IMAGES ──────────────────────────────────────────────
docker images # list local images
docker pull node:20-alpine # download image without running
docker rmi node:20-alpine # delete an image
docker image prune # delete all dangling (untagged) images
# ── CONTAINERS ──────────────────────────────────────────
docker ps # running containers
docker ps -a # ALL containers (including stopped)
docker start my-app # start a stopped container
docker stop my-app # graceful stop (SIGTERM, 10s grace)
docker kill my-app # immediate stop (SIGKILL)
docker rm my-app # delete a stopped container
docker container prune # delete all stopped containers
# ── INSPECT ─────────────────────────────────────────────
docker logs my-app # view stdout/stderr
docker logs -f my-app # follow (tail -f equivalent)
docker exec -it my-app bash # open shell inside running container
docker inspect my-app # full JSON config dump
docker stats # live CPU/RAM/network usage
# ── CLEANUP (nuclear option) ─────────────────────────────
docker system prune -a # remove everything unused
Tip: The --rm flag is your friend. Add --rm to docker run and the container is automatically deleted when it stops. Perfect for one-off tasks like running a script or testing a command — no cleanup needed: docker run --rm node:20-alpine node -e 'console.log("hello")'
Interactive vs detached mode
# -d (detached): runs in background, returns you to the shell
docker run -d nginx
# -it (interactive + TTY): gives you a shell inside the container
docker run -it ubuntu bash
# Now you're INSIDE the container!
# root@a3f2c1d4:/# ls
# root@a3f2c1d4:/# apt-get install -y curl
# root@a3f2c1d4:/# exit ← container stops when you exit
# Useful for debugging — run a shell in the alpine image
docker run --rm -it alpine sh
# Or exec into an already-running container
docker exec -it my-nginx bash
Frequently Asked Questions
What is Docker Hub?
Docker Hub (hub.docker.com) is the default public registry — a library of pre-built images. When you run 'docker run nginx', Docker looks for nginx locally first, then pulls it from Docker Hub automatically. Official images (nginx, node, postgres, python) are maintained by their respective projects and safe to use as base images.