0 / 11 lessons — 0%
Lesson 03 / 11
Images vs. containers — core commands
An image is a stack of read-only layers plus metadata — think "recipe." A container is a running (or stopped) instance made from that recipe, with one thin writable layer on top — think "the dish, on a plate, right now."
# pull an image from a registry (default: Docker Hub) docker pull nginx:1.27 # run it, mapping host port 8080 -> container port 80 docker run -d --name web -p 8080:80 nginx:1.27 # what's running? what's everything, running or not? docker ps docker ps -a # peek at logs, or get a shell inside docker logs -f web docker exec -it web bash # stop, start again, remove docker stop web docker start web docker rm web # images: list, remove, see what's eating disk docker images docker rmi nginx:1.27 docker system df
| Flag | Meaning |
|---|---|
-d | detached — run in the background |
-it | interactive + allocate a TTY |
-p host:container | publish a port |
-v | mount a volume or bind mount |
--rm | auto-remove the container on exit |
-e KEY=val | set an environment variable |
Muscle memory shortcut: if you only remember three commands, make them
docker ps (what's alive), docker logs -f (what's it saying), docker exec -it ... bash (let me look inside). That trio solves most "why isn't this working" moments.Try it yourselfRun the nginx command above, then open
http://localhost:8080 in a browser. You're looking at a web server running inside a box that didn't exist ten seconds ago.