/Docker Containers & Images
Concept Detail

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 is latest.

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:

CommandDescription
docker run -it imageCreate + start with interactive TTY
docker run -d imageDetached (background) mode
docker run --name n imageAssign a name
docker run -p 8080:80 imagePublish port 80 → host 8080
docker run --rm imageAuto-remove container on exit
docker ps / ps -aList running / all containers
docker stop / killGraceful SIGTERM / immediate SIGKILL
docker exec -it c cmdRun command in running container
docker logs cView stdout/stderr
docker inspect cFull JSON metadata
docker cp src c:/dstCopy files to/from container

Practice Linked Questions


easy

Q1. What is the difference between `docker ps` and `docker ps -a`?


Select one answer before revealing.

easy

Q2. What do the `-i` and `-t` flags do when combined in `docker run -it ubuntu bash`?


Select one answer before revealing.

easy

Q3. A container is started with `docker run -d nginx`. What does the `-d` flag do?


Select one answer before revealing.

medium

Q4. What is the difference between `docker stop` and `docker kill`?


Select one answer before revealing.

medium

Q5. You run `docker run --rm alpine echo hello`. What happens to the container after the command completes?


Select one answer before revealing.

easy

Q6. How do you run a command inside an already-running container without stopping it?


Select one answer before revealing.

medium

Q7. What does the Copy-on-Write (CoW) mechanism mean for Docker containers?


Select one answer before revealing.

easy

Q8. What command lists all Docker images available on the local host?


Select one answer before revealing.

hard

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.

easy

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.

medium

Q11. What does `docker system prune` do?


Select one answer before revealing.

medium

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.

easy

Q13. Which command would you use to view the stdout and stderr logs of a running container named "webserver"?


Select one answer before revealing.

easy

Q14. Which command shows real-time resource usage statistics (CPU, memory, network I/O) for all running containers?


Select one answer before revealing.

medium

Q15. What is the purpose of `docker inspect <container>`?


Select one answer before revealing.

medium

Q16. When should you use `docker commit` and why is it generally discouraged in production?


Select one answer before revealing.