Images & Containers in Practice
The handful of commands you'll use every single day with Docker.
Now that you understand the concepts, let’s build the muscle memory. You only need about ten commands to work confidently with Docker day to day.
🎯 Learning Objectives
By the end of this lesson you will:
- Manage the container lifecycle: run, stop, remove
- Inspect containers with
logsandexec - Manage images: list, pull, remove
- Understand port mapping and volumes at a basic level
The Container Lifecycle
| Command | What it does |
|---|---|
docker run <image> |
Create and start a container |
docker ps |
List running containers |
docker ps -a |
List all containers (including stopped) |
docker stop <id> |
Gracefully stop a container |
docker rm <id> |
Remove a stopped container |
docker run -d --name web -p 8080:80 nginx # start named container
docker ps # see it running
docker stop web # stop it
docker rm web # remove it💡 Name your containers
--name web gives the container a friendly name so you can type docker stop web instead of copying a random ID. Small habit, big time-saver.
Looking Inside a Running Container
Two commands do most of your debugging:
docker logs web # view a container's output
docker logs -f web # follow logs live
docker exec -it web bash # open a shell inside the containerdocker exec -it drops you into a shell inside the container, so you can inspect files, environment variables, and processes as if you were SSH’d into a tiny machine.
Managing Images
| Command | What it does |
|---|---|
docker images |
List local images |
docker pull <image> |
Download an image |
docker rmi <image> |
Remove an image |
docker image prune |
Clean up unused images |
⚠ Disk fills up fast
Images, stopped containers, and unused volumes quietly eat disk space on build servers. docker system prune -a clears the lot — powerful, but it deletes anything not currently in use, so use it deliberately.
Ports and Volumes
Two flags you’ll use constantly:
- Port mapping (
-p host:container) exposes a container’s port to the host so you can reach the app. - Volumes (
-v host_path:container_path) persist data outside the container, so it survives even when the container is removed.
docker run -d \
-p 5432:5432 \
-e POSTGRES_PASSWORD=secret \
-v pgdata:/var/lib/postgresql/data \
--name db postgresHere the database’s files live in a named volume pgdata, so your data isn’t lost when the container restarts.
💡 Containers are disposable, data is not
Treat containers as throwaway. Anything you need to keep — databases, uploads — belongs in a volume, never inside the container’s own filesystem.
🧪 Hands-on Lab
Run, Inspect and Clean Up
- Run an Nginx container named
mywebon port 8080 - View its logs, then open a shell inside it
- Stop and remove it, then confirm it’s gone
🧠 Knowledge Check
Which command opens an interactive shell inside a running container named 'web'?
Where should a database keep its data so it survives the container being removed?
💼 Interview Preparation
A container keeps crashing on startup. How do you debug it?
Summary
You can now manage the full container lifecycle, inspect containers with logs and exec, handle images, and persist data with volumes. Next, you’ll package your own application into an image by writing a Dockerfile.