Skip to content

Docker packages applications and their dependencies into portable containers. Containers share the host operating system kernel while executing in isolated user space environments. An image is a read-only blueprint, while a container is a running instance of an image with a writable layer.

TaskCommand
Pull imagedocker pull <image>
List imagesdocker image ls
Remove imagedocker rmi <image>

The Docker CLI interacts with the Docker daemon to manage container images and runtime environments. Images are downloaded from registries like Docker Hub and stored locally.

Terminal window
docker pull node:22-alpine
docker image ls
docker rmi node:22-alpine

Tip: Always specify explicit image tags rather than latest to ensure reproducible environments.

TaskCommand
Run containerdocker run <options> <img
List activedocker ps
Stop containerdocker stop <id>

Containers move through distinct states: created, running, paused, stopped, and removed. Using interactive flags allows running ephemeral environments for testing or debugging.

Terminal window
docker run -d \
--name web \
-p 8080:80 \
nginx:alpine

Gotcha: Containers are ephemeral by default. Any data created inside a container is lost when the container is deleted unless mounted to a volume.

InstructionPurpose
FROMSets base image for build stage
COPYCopies files from host to image
RUNExecutes command during build

Dockerfiles use layered caching. Each instruction creates an immutable image layer. Place infrequently changed layers (like dependency installation) before frequently changed source code.

FROM node:22-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

Tip: Multi-stage builds keep final images lean by discarding build toolchains and source files.

CommandAction
Start servicesdocker compose up -d
View logsdocker compose logs -f
Stop servicesdocker stop <id>

Docker Compose defines and runs multi-container applications. Services within the same Compose file join a shared network by default and can resolve each other by service name.

services:
app:
build: .
ports:
- "3000:3000"
environment:
- DB_HOST=db
db:
image: postgres:16-alpine
environment:
- POSTGRES_PASSWORD=secret

Gotcha: docker compose down stops containers but preserves named volumes; use -v flag to purge volume data.

TaskCommand
Exec shelldocker exec -it <id> sh
View logsdocker logs -f <id>
Prune systemdocker system prune -a

Debugging running containers requires checking stdout logs or executing interactive shells. Dangling images, stopped containers, and unused volumes consume disk space quickly.

Terminal window
docker logs --tail 50 -f my-app
docker exec -it my-app sh
docker system prune -af --volumes

Warning: docker system prune -a --volumes irreversibly deletes all stopped containers, unused networks, and unreferenced volumes.

GoalFlag / Setting
Limit CPU--cpus="2.0"
Limit memory--memory="2g"
Read-only root--read-only

AI agents executing unverified code must be run in secure, resource-constrained sandbox containers. Restricting kernel privileges, memory, CPU, and filesystem write access prevents runaway agent processes and security breaches.

Terminal window
docker run --rm -it \
--memory="1g" \
--cpus="1.0" \
--read-only \
--network none \
--cap-drop=ALL \
python:3.12-slim \
python -c "print('agent test')"

Gotcha: Read-only root filesystems require explicit --tmpfs /tmp flags if the agent script or runtime needs temporary scratch space.

Verified 2026-10-18 against Docker Documentation