Docker Containers & Images
Difficulty: easy
Overview
Images and containers are the two core runtime objects in Docker.
Images:
- Read-only, layered filesystem snapshots built from a Dockerfile.
- Each instruction that modifies the filesystem creates a new layer.
- Layers are content-addressed and shared across images on the same host.
- Naming format:
[registry/][namespace/]name[:tag]— default tag islatest.
Containers:
- A running (or stopped) instance of an image.
- Adds a thin writable layer on top of the read-only image layers (overlay2 storage driver).
- Container state machine: created → running → paused → stopped → removed.
- The writable layer is discarded on removal — use volumes for persistent data.
Copy-on-Write (CoW):
When a container modifies a file from an image layer, the file is copied into the container's writable layer first. The original image layer remains unchanged, allowing multiple containers to share it.
Essential Commands:
| Command | Description |
|---|---|
docker run -it image | Create + start with interactive TTY |
docker run -d image | Detached (background) mode |
docker run --name n image | Assign a name |
docker run -p 8080:80 image | Publish port 80 → host 8080 |
docker run --rm image | Auto-remove container on exit |
docker ps / ps -a | List running / all containers |
docker stop / kill | Graceful SIGTERM / immediate SIGKILL |
docker exec -it c cmd | Run command in running container |
docker logs c | View stdout/stderr |
docker inspect c | Full JSON metadata |
docker cp src c:/dst | Copy files to/from container |
Practice Linked Questions
Q1. What is the difference between `docker ps` and `docker ps -a`?
Select one answer before revealing.
Q2. What do the `-i` and `-t` flags do when combined in `docker run -it ubuntu bash`?
Select one answer before revealing.
Q3. A container is started with `docker run -d nginx`. What does the `-d` flag do?
Select one answer before revealing.
Q4. What is the difference between `docker stop` and `docker kill`?
Select one answer before revealing.
Q5. You run `docker run --rm alpine echo hello`. What happens to the container after the command completes?
Select one answer before revealing.
Q6. How do you run a command inside an already-running container without stopping it?
Select one answer before revealing.
Q7. What does the Copy-on-Write (CoW) mechanism mean for Docker containers?
Select one answer before revealing.
Q8. What command lists all Docker images available on the local host?
Select one answer before revealing.
Q9. A developer wants to set a memory limit of 512MB on a running container. Which `docker run` flag should they use?
Select one answer before revealing.
Q10. Which of the following `docker run` flags are commonly used together to assign a name and publish a port? (Select all that apply — more than one answer may be correct.)
Select one answer before revealing.
Q11. What does `docker system prune` do?
Select one answer before revealing.
Q12. A container running a web server must always restart on failure, even after a Docker daemon restart. Which restart policy should be used?
Select one answer before revealing.
Q13. Which command would you use to view the stdout and stderr logs of a running container named "webserver"?
Select one answer before revealing.
Q14. Which command shows real-time resource usage statistics (CPU, memory, network I/O) for all running containers?
Select one answer before revealing.
Q15. What is the purpose of `docker inspect <container>`?
Select one answer before revealing.
Q16. When should you use `docker commit` and why is it generally discouraged in production?
Select one answer before revealing.