Docker
02 / 05

Images & Containers

Images & Containers

Core Concepts

  • Image — read-only template (layers) built from a Dockerfile

  • Container — running instance of an image with a writable layer on top

  • Registry — storage for images (Docker Hub, GHCR, ECR, GCR)

  • Volume — persistent storage managed by Docker, survives container removal

  • Network — bridge/host/overlay — how containers communicate

Essential Commands

# Images
docker pull nginx:1.25                  # pull from registry
docker images                           # list local images
docker image rm nginx:1.25             # remove image
docker image prune -f                  # remove dangling images
docker build -t myapp:1.0 .            # build from Dockerfile in current dir
docker tag myapp:1.0 ghcr.io/user/myapp:1.0  # tag for push
docker push ghcr.io/user/myapp:1.0     # push to registry

# Containers — run
docker run nginx                        # run (foreground)
docker run -d nginx                     # detached (background)
docker run -d -p 8080:80 nginx         # map host:container port
docker run -d --name web nginx         # named container
docker run -d -e NODE_ENV=prod myapp   # env variable
docker run -d -v mydata:/data nginx    # named volume mount
docker run -d -v $(pwd)/config:/app/config:ro nginx  # bind mount read-only
docker run --rm alpine echo "hello"    # auto-remove on exit

# Containers — manage
docker ps                              # running containers
docker ps -a                           # all (including stopped)
docker stop web                        # graceful stop (SIGTERM then SIGKILL)
docker kill web                        # immediate SIGKILL
docker start web                       # restart stopped container
docker restart web                     # stop + start
docker rm web                          # remove stopped container
docker rm -f web                       # force remove running container

# Debugging
docker logs web                        # view stdout/stderr
docker logs -f web                     # follow logs
docker logs --tail 100 web            # last 100 lines
docker exec -it web bash              # interactive shell
docker exec web cat /etc/hosts        # run single command
docker inspect web                    # full JSON metadata
docker stats                           # live resource usage
docker top web                         # processes inside container

Volumes & Bind Mounts

# Named volumes (Docker managed, best for prod data)
docker volume create mydata
docker volume ls
docker volume inspect mydata
docker volume rm mydata
docker volume prune         # remove all unused volumes

# Run with named volume
docker run -d -v mydata:/var/lib/postgresql/data postgres

# Bind mount (host path, good for development)
docker run -d -v /host/path:/container/path myapp
docker run -d -v $(pwd):/app node     # mount current dir

# tmpfs mount (in-memory, not persisted)
docker run -d --tmpfs /tmp myapp

Cleanup

# Remove everything unused (safe)
docker system prune

# Remove everything including volumes
docker system prune --volumes

# Remove all stopped containers
docker container prune

# Remove all unused images
docker image prune -a

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free